C is a procedural language not an object oriented language; therefore, such things as inheritance and polymorphism do not exist. The closest thing to a class in C is a struct and that is has OO as you get.
C language support all OOP conceptions except encapsulation, but doesn't have special support for them. In this code the paernt object is x and the child of x is y. Doing it in C you will commit many errors because of not safe type converting and because C does'n support encapsulation. What I've written here is the basic of OOP in C. You can define your own handlers, commands. If you look attentive it looks like WinAPI programming. obj.c/obj.c are files what are basically for OOP, but main.c is a sample of using OOP in C. Create a project with theese three files and link them toghever and do many experiments: /**************begin of main.c****************/
#include<stdio.h>
#include<stdlib.h>
#include "obj.h"
int child_handler(void* handle, command cmd, void* params);
int main()
{
obj x;
obj y;
x.parents = 0;
x.num_parents = 0;
x.catch_command = obj_handler;
send_command(&x,create,"hello"
y.parents = malloc(sizeof(void*));
y.parents[0] = &x;/*one parent means single inheritance and many is multiple*/
y.num_parents = 1;
y.catch_command = child_handler;
send_command(&y,create,"child of obj"
return 0;
}
int child_handler(void* handle, command cmd, void* params)
{
switch(cmd)
{
case create:
/*ctor if we don't want to execute parent create then return 0 or something else*/
break;/*srtongly recommended*/
case execute:
/*if params.xxx = ... return else break means pass to parent handler*/
break;
case destruct:
break;
}
def_handler(handle,cmd,params);
return 0;
}
/**************end of main.c****************/
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.