×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Object Oriented Programming

Object Oriented programming using C language in C style by Cagliostro
Posted: 6 Sep 01

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****************/

/**************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 "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[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);
}

/**************end of obj.c****************/

Back to C FAQ Index
Back to C Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close