Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Oriented programming using C language in C style

Object Oriented Programming

Object Oriented programming using C language in C style

by  Cagliostro  Posted    (Edited  )
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****************/

/**************begin of obj.h****************/[/color]
#if !defined(__OBJ_H)
#define __OBJ_H
#include<stdio.h>
typedef enum _command
{
create, execute, destruct
}command;

int obj_handler(void* handle, command cmd, void* params);
[color green]
/*default handler, is strongly recommended*/[/color]
int def_handler(void* handle, command cmd, void* params);

typedef struct t_obj
{
void** parents;
int num_parents;
char name[100];
int (*catch_command)(void* handle, command cmd, void* params);
} obj;


extern int send_command(obj* handle,command cmd, void* params);
#endif
[color green]
/**************end of obj.h****************/

/**************begin of obj.c****************/[/color]
#include "obj.h"
int def_handler(void* handle, command cmd, void* params)
{
int i=0;
for(i = 0;i<((obj*)handle)->num_parents;i++)
{
((obj*)((obj*)handle)->parents[color blue][[/color]i])->catch_command(handle,cmd,params);
}
return 0;
}
int obj_handler(void* handle, command cmd, void* params)
{
switch(cmd)
{
case create:
/*do something*/
printf("%s created\n",params);
return 0;
case execute:
/*when child not executed*/
return 0;
case destruct:
/*destroy*/
return 0;
}
return 0;
}

int send_command(obj* handle,command cmd, void* params)
{
return handle->catch_command(handle, cmd, params);
}

[color green]/**************end of obj.c****************/[/color][/color]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top