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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OO programming in C

Status
Not open for further replies.

anand102

Programmer
Aug 2, 2001
11
US
hi,

i am looking for tips on OO programming(C++) in C . could you pls
give me some poniters on things like polymorphism,inhetiance etc.

Thanks
 
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.

bitwise
 
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 &quot;obj.h&quot;
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,&quot;hello&quot;);
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,&quot;child of obj&quot;);
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****************/

/**************begin of obj.h****************/

#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);

/*default handler, is strongly recommended*/
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

/**************end of obj.h****************/

/**************begin of obj.c****************/

#include &quot;obj.h&quot;
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[i])->catch_command(handle,cmd,params);
}
return 0;
}
int obj_handler(void* handle, command cmd, void* params)
{
switch(cmd)
{
case create:
/*do something*/
printf(&quot;%s created\n&quot;,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);
}


/**************end of obj.c****************/
John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top