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:
[color blue]
[color green]/**************begin of main.c****************/[/color]
#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;[color green]/*one parent means single inheritance and many is multiple*/[/color]
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:
[color green] /*ctor if we don't want to execute parent create then return 0 or something else*/[/color]
break;[color green]/*srtongly recommended*/[/color]
case execute:
[color green]/*if params.xxx = ... return else break means pass to parent handler*/[/color]
break;
case destruct:
break;
}
def_handler(handle,cmd,params);
return 0;
}
[color green]
/**************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.