Solved

OOP in C

Posted on 1998-06-11
4
265 Views
Last Modified: 2010-05-18
I read somewhere that although it is tough to do OOP in C
(compared to C++) it is not impossible and can be done.
How?
0
Comment
Question by:riddhi_barman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 2

Accepted Solution

by:
abesoft earned 50 total points
ID: 1251189
It is perfectly possible!

To do OOP, you need to concentrate on objects.  C++ provides some mechanisms to help you with encapsulation, polymorphism, and data hiding.  These are simply mechanisms, though.  (You are always free to use or abuse them, or not use them at all.)

In traditional C, you can use structs instead of C++ classes.  With a little effort and a lot of discipline, you can use C (or almost any language) to do OOP.  

Watch for a few more comments that follow...
0
 
LVL 2

Expert Comment

by:abesoft
ID: 1251190
Encapsulation is easy in C: You simply use a struct.

typedef struct s_GraphicObject{
    int x, y; /* Location of object */
} GraphicObject;

Now you need to encapsulate a few functions for the object:

void GraphicObject_Initialize( GraphicObject *obj)
{
    obj->x = obj-> y = 0;
}

void GraphicObject_Print( FILE *f, GraphicObject *obj)
{
    fprintf( f, "GraphicObject( %d, %d)", obj->x, obj->y);
}

These functions would be prototyped in the header file, along with the typedef.  Then when people want to create an object, they would
    GraphicObject point;
    GraphicObject_Initialize( &point);
    GraphicObject_Print( stdout, &point);

It's a little clunky at points, but it works....
0
 
LVL 2

Expert Comment

by:abesoft
ID: 1251191
Inheritance can be done using containment.  For example, if we extend the GraphicObject given above to be a circle (which adds a radius) you could do:

typedef struct sCircle{
    GraphicObject parent;
    int radius;
} Circle;

and then define:

void Circle_Initialize( Circle *obj)
{
    GraphicObject_Initialize( &obj->parent);
    obj->radius = 1;
}

void Circle_Print( FILE *f, GraphicObject *obj)
{
    fprintf( f, "Circle( ");
    GraphicObject_Print( f, &obj->parent);
    fprintf( f, " raduis= %d)", obj->radius);
}
0
 
LVL 2

Expert Comment

by:abesoft
ID: 1251192
If you want some polymorphism, you'll need to use function pointers:

typedef void (*memberFunction)( void *);
typedef struct s_GraphicObject{
    int x, y; /* Location of object */
    memberFunction Print;
} GraphicObject;

void GraphicObject_Initialize( GraphicObject *obj)
{
    obj->x = obj-> y = 0;
    obj->Print = (memberFunction) GraphicObject_Print;
}

void Circle_Initialize( Circle *obj)
{
    GraphicObject_Initialize( &obj->parent);
    obj->radius = 1;
    obj->parent.Print = (memberFunction) Circle_Print;
}

[Note that I assume that Print only takes a pointer to the object, and not a pointer to the FILE like it did in the previous comments. ]

And then you can print an arbitrary object, like so:
    GraphicObject point;
    GraphicObject_Initialize( &point);
    point.Print( &point);
    Circle myCircle;
    Circle_Initialize( &myCircle);
    myCircle.Print( &myCircle);
Likewise, if you define:
    void foo( GraphicObject *obj)
    {
        obj->Print( obj);
    }
and then call foo( &myCircle), (with a few extra casts) all will work as you would hope it to.

Neat, non?

[We won't talk about multiple inheritance.  I don't think anyone really needs to use it, and if you find an example where it's needed, then you can probably figure out a way to do it yourself...]

Hope all this helps...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question