Can object-oriented in C language?

Asked 2 years ago, Updated 2 years ago, 100 views

The word "feeling structure" came out, but I didn't really understand it.

For example, what happens if I use C to represent Ruby's code below?

class HogeModule
  def hoge 
  end
end

class HogeClass
  include HogeModule

  def initialize (name)
    @name = name
  end
end

class FugaClass <HogeClass

  default initialize (name)
    super
  end

  def fuga
    p@name
  end

  def self.fuga
    p'@name'
  end
end

FugaClass.fuga

fuga=FugaClass.new
fuga.fuga

ruby c

2022-09-30 21:25

2 Answers

For example, what happens if I use C to represent Ruby's code below?

C is not the language, but if you dare answer this question, this is what happens.

#include<stdio.h>
# include <stdlib.h>

typeef structure {
} HogeModule;

void
HogeModule_hoge(HogeModule*hm){
}

typeef structure {
  const char*name;
  HogeModule* base;
} HogeClass;

void
HogeClass_initialize(HogeClass*hc, const char*name){
  hc->base=malloc(sizeof(HogeModule)));
  hc->name=name;
}

typeef structure {
  HogeClass*base;
} FugaClass;

void
FugaClass_initialize(FugaClass*fc){
  fc->base=malloc(sizeof(HogeClass)));
  HogeClass_initialize(fc->base, "");
}

void
FugaClass_static_fuga(){
}

void
FugaClass_fuga(FugaClass*fc){
  puts(fc->base->name);
}


int
main(intargc,char*argv[]){
  FugaClass fc;

  FugaClass_static_fuga();

  FugaClass_initialize(&fc);
  FugaClass_fuga(&fc);
  return 0;
}

I don't do memory release or NULL determination because I don't want to.


2022-09-30 21:25

It doesn't match the answer you want and you can't write the code, but
Let me explain object orientation and C language.
Simply put, it is similar to the structure of C and
In C, you can write programs using structures, but
Object orientation is like all languages are structured.
So what happens when you write in C is code like everyone else, and object orientation can be implemented in C language with a lot of functions and structures.


2022-09-30 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.