Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / C
Article

Object based programming in C language

Rate me:
Please Sign up or sign in to vote.
1.85/5 (9 votes)
1 Feb 20073 min read 40.6K   11   10
This article describes object based programming in traditional C language.

Introduction

This article describes a simple way to design object oriented programming concepts in traditional C language.

The description is as follows:

1. Please refer the code below for better understanding of the description. The code given at the end is actually a working code and there should not be any compilation and execution issues.

2. struct A is equivalent to a 'class' like in any Object Oriented programming language.

3. Various member of A; like a, b, c; are data members of the class

4. Various members of A; like add, sub and print; are member function of the class. Basically they are pointers to functions and would be initialized just after the variable allocation/creation.

5. Global functions like add, sub and print are function they would be eventually linked to the class's function pointers. Process or step of assigning a function address to function pointers is called as ATTACH. Macro ATTACH does the function pointer assignment. AXS is a short form of 'ACCESS' meaning to access a data member or member function.

6. To access the data members we can use standard 'dot' operator in C with the structure variable.

7. To call a member function of A using an object (here in example 'aa') we need to use this syntax obj.functionptr(address of obj)

8. In this implementation all member functions should have atleast one argument of type struct A *. For uniformity we can specifically have it as struct A *this_ptr. This pointer (address of calling object) is used to access object's (aa's and ab's) data members or member functions.

9. Why this model is similar to Object Oriented Programming model? In this model the memory entities 'aa' and 'ab' are actually two objects of type 'A' ('A' can be considered as a class). Both the objects have their own data members and member functions (member functions are actually shared between all objects).

10. Other Possiblities: This model can be extended wherein we can selectively change one or more member function pointers to point to some other functions. Thus, will enable us to change the behavior of an object selectively. For instance, which means that aa's 'add' function could be adding variables 'a' and 'b' into 'c' (c=a+b) and at the same time ab's 'add' function could be adding squares of 'a' and 'b' into c (c=axa + bxb). This behavior is called as 'Function Overriding'.

11. Using data member pointers we can as well simulate 'static' variables (for both data members and member functions) similar to class (like in C++). These pointer variables (or logical static variables) would need to be initialized after all object creations.

Code Section

struct A

{

int a,b;

int c;

int (*add)();

int (*sub)();

int (*print)();

};

#define ATTACH_ALL_FOR_A(obj) {ATTACH(obj,add);ATTACH(obj,sub);ATTACH(obj,print);}

#define THIS this_ptr

#define DECLARE_THIS (*THIS)

#define AXS(X) (THIS->X)

#define ATTACH(obj,function) obj.function = function;

void print(struct A DECLARE_THIS)

{

printf("\nA: %d, B: %d, C: %d",AXS(a),AXS(b),AXS(c));

}

void add(struct A DECLARE_THIS)

{

AXS(c) = AXS(a) + AXS(b);

AXS(print(THIS));

}

void sub(struct A DECLARE_THIS)

{

AXS(c) = AXS(a) - AXS(b);

}

void main()

{

struct A aa,ab;

clrscr();

ATTACH_ALL_FOR_A(aa);

ATTACH_ALL_FOR_A(bb);

aa.a=4;

aa.b=1;

aa.add(&aa);

printf("aa.c: %d",aa.c);

printf("ab.c: %d",ab.c);

getch();

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Working with EMC Corp, Bangalore (India) in distributed embedded software design. Like to read and code in storage, computer security and language design areas.

Comments and Discussions

 
GeneralThe point in C language Pin
Bruno Challier15-Aug-09 0:04
Bruno Challier15-Aug-09 0:04 
QuestionIs it really usefull? Pin
FredericSivignonPro1-Feb-07 4:34
FredericSivignonPro1-Feb-07 4:34 
AnswerRe: Is it really usefull? [modified] Pin
BhaskarBora1-Feb-07 5:56
BhaskarBora1-Feb-07 5:56 
GeneralRe: Is it really usefull? Pin
FredericSivignonPro1-Feb-07 6:22
FredericSivignonPro1-Feb-07 6:22 
GeneralRe: Is it really usefull? [modified] Pin
BhaskarBora1-Feb-07 6:55
BhaskarBora1-Feb-07 6:55 
GeneralRe: Is it really usefull? Pin
Barry Dorman1-Feb-07 9:08
Barry Dorman1-Feb-07 9:08 
I write a lot of embedded software in C. It's common practice around here write C++ code and put a C wrapper around it. For instance, I want a function to add vectors, but I don't want to write one for every precision in C. I just use a generic template in C++ and write one to handle them all! Although I still have to declare functions for each precision in C, they are much simplier. The add function is trivial but it's a big deal on other more complex functions. Remember that OO isn't magic! The reason you can't write in C++ is because your compiler doesn't know how. It's not because the embedded device can't understand the binarys.
AnswerRe: Is it really usefull? Pin
Gerard Nicol1-Feb-07 11:27
Gerard Nicol1-Feb-07 11:27 
GeneralRe: Is it really usefull? Pin
BhaskarBora1-Feb-07 11:58
BhaskarBora1-Feb-07 11:58 
GeneralRe: Is it really usefull? Pin
Gerard Nicol1-Feb-07 12:49
Gerard Nicol1-Feb-07 12:49 
GeneralRe: Is it really usefull? Pin
BhaskarBora2-Feb-07 5:16
BhaskarBora2-Feb-07 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.