Solved

OOP in C

Posted on 1998-06-11
4
261 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now