Object Oriented Programming in C

Discussion in 'C' started by vishal sharma, Jan 14, 2007.

  1. vishal sharma

    vishal sharma New Member

    Joined:
    Jul 23, 2004
    Messages:
    106
    Likes Received:
    6
    Trophy Points:
    0
    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:
    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:
    /* 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:
    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:
    /* 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:
    /* 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:
    /* 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:
    /* 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:
    /* 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 */
     
  2. leovarma

    leovarma New Member

    Joined:
    Jan 22, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Re: Object Orinted Programming in C

    Pretty cool buddy.. I felt his link provides good links to OOP content
     
    Last edited by a moderator: Jan 31, 2007
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Re: Object Orinted Programming in C

    Confine links to signatures only. The link you provided is a site made for adsense and nothing more.
     
  4. rohanag

    rohanag New Member

    Joined:
    Oct 21, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Studying
    Location:
    Jaipur
    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->str_name=value
    where i is the index
     
  5. sreeja

    sreeja New Member

    Joined:
    Nov 13, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Re: Object Orinted Programming in C

    The information by vishal sharma about c and c++ is very useful.The information about structures and pointers is very helpful.
     
  6. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    By Any Way!! It's nice...
     

Share This Page

  1. 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.
    Dismiss Notice