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
Solved

OOP in C

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

860 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