Object Oriented Programming in C

vishal sharma's Avatar author of Object Oriented Programming in C
This is an article on Object Oriented Programming in C in C.
The thing is that it won’t be really OOP because the encapsulation will be a bit weak and you will have to handle constructors and destructors manually but it’s still cool.

First of all, basic knowledge:

1) Structures
2) Pointers to Functions

1: Structures



A structure is a collection of variables and data types under one name for instance:

Code: C
struct Car {
    int id;
    char model[255] ;
    char company[255];
}
If you wont do “typedef struct Car car;” to define struct variables you will have to use “struct Car” as the type but with typedef “car” would be enough;

To define the structure you can:

Struct Car car = {1432423, “Super GLX”, “Mitsubishi”};
Struct Car *pCar = &car;

Or

car.id = 1432423;
pCar->id = 123456; /* use -> when the struct is a pointer type */

now a simple example of a program that uses structs to summarize the subject:

Code: C
/* start of car.c file */

typedef struct _Car {
    int id;
    char model[255] ;
    char company[255];
} Car;

int main()
{
    Car car = {1432423, “Super GLX”, “Mitsubishi”};
    printf(“Car details: %d, %s, %s”, car.id, car.model, car.company);

    return 0;
}

/* end of car.c file */

2: Pointers to Functions



To define a pointer to a function use the syntax: “type (*name)(argument list);“

Now let’s say we have a function: int add(int a, int b) that adds a and b and returns the sum, now lets say that int (*add)(int, int) is a pointer to this function, we declare it by saying:

Code: C
int(*pfAdd)(int, int) = add;
so now if we use (*pfAdd)(1, 3) it will do the same thing as add(1, 3) and return 4.

Now a simple example of a program that uses pointers to functions to summarize the subject:

Code: C
/* start of ptf.c file */

#include <stdio.h>

int add(int a, int b)
{
    return a+b;
}

int main()
{
    int (*pfAdd)(int, int) = add;

    printf(“the sum of 1 and 3 is %d\n”, (*pfAdd)(1, 3));

    return 0;
}

/* end of ptf.c file */

Object Orinted Programming in C



Now begins the cool part, the actual purpose of this article… now, we are used to define classes and have them hold methods and members, this can also be done with a struct like this:

Code: C
/* start of example */

#define CLASS typedef struct

CLASS
{
    // add some pointers to functions that will be the methods without giving them values
    // add some public members if needed without values even if const
} Car;

/* end of example */
But every function that we want to use MUST be declared before the structure, implementation can come later. To use a constructor and a destructor we will need to write the functions and then manually call them, or we can use a macro as well but still have to call it manually. Let’s finish this tutorial with an example:
Code: C
/* start of class.h */

#define CLASS typedef struct

int add(void *obj);

CLASS
{
    int ( *pfAdd)(void *);
} math;

math *cMath(math *pMath, int a, int b);

/* end of class.h */
Code: C
/* start of class.c */

#include <stdio.h>
#include “class.h

int _a, _b; // private members of the class

// constructure for class math
math *cMath(math *pMath, int a, int b)
{
    // if the object was not constructed yet, construct it else return it
    if(pMath == 0)
    {
        pMath = (math *)malloc(sizeof(math));
        pMath->pfAdd = add;
        _a = a;
        _b = b;
    }

    return pMath;
}

int add()
{
    return obj->a + obj->b;
}

/* end of class.c */
Code: C
/* start of main.c */

#include <stdio.h>
#include “class.h

void main
{
    math *m = 0; // it’s recommended to give the value zero at start for the “constructor functions
    int pause;

    m = cMath(m, 5, 3);

    printf(5 + 3 = %d”, m->add());
    scanf(“%d”, &pause);
}

/* end of main.c */
0
leovarma's Avatar
Newbie Member
Pretty cool buddy.. I felt his link provides good links to OOP content

Last edited by shabbir; 31Jan2007 at 15:22.. Reason: Confine links to signatures only.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by leovarma
Pretty cool buddy.. I felt his link provides good links to OOP content
Confine links to signatures only. The link you provided is a site made for adsense and nothing more.
0
rohanag's Avatar, Join Date: Oct 2007
Light Poster
hi
i have a ques about array of strucures

how do we access an element (thru pointers) which is in an array of strucures

c is giving error for
ptr[i]->str_name=value
where i is the index
0
sreeja's Avatar
Go4Expert Member
The information by vishal sharma about c and c++ is very useful.The information about structures and pointers is very helpful.
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
By Any Way!! It's nice...