r/C_Programming • • Sep 02 '26

Example of OOP in C

Hello everyone, I have developed a library to support OOP in plain C that allows the creation of object classes and includes dynamic Array and Dictionary container classes.

I am looking for suggestions for new examples that illustrate cases where an Object Oriented design can simplify C-Code.

I would love to get some reviews and feedback on this project.

I have included a code example below:

Supported by: Coda-C Library

Example code: Coda-C/examples/shapes.c

// Coda-C subclass examples

    #include <coda-c.h> // shapes.c  08/20/2026
    #include <math.h>

    struct Shape_ { Char color; };
#define class Shape
CodaClassZerosC(); // 1. ABSTRACTION: blueprint for all shapes
CodaClass(Shape,struct Shape_,Root);
    propertyO$(Char,color); // 2. ENCAPSULATION: Private data
    typedef double OSig$(calculate_area); // Abstract method: required in subclasses
    typedef double OSig$(calculate_perimeter); // defines function type and signature key
#undef class // Shape

    struct Circle_ { double radius; };
#define class Circle
CodaClassZerosC(); // 3. INHERITANCE: Circle inherits from Shape
CodaClass(Circle,struct Circle_,Shape);
    getter$(double,radius,_ radius); // double Circle_radius(Circle self) { return _ radius; }
    method$(double,calculate_area) { return(M_PI * _ radius * _ radius); }
    method$(double,calculate_perimeter) { return(2.0 * M_PI * _ radius); }

class Circle_new(Char color,double radius) {
    class self=newO(class);
    Shape_set_color((Obj)self,color);
    _ radius=radius; // where #define _ self->
    return(self);
    }
#undef class // Circle

    struct Rectangle_ { double width,height; };
#define class Rectangle
CodaClassZerosC();
CodaClass(Rectangle,struct Rectangle_,Shape);
    method$(double,calculate_area) { return _ width * _ height; }
    method$(double,calculate_perimeter) { return 2.0 * (_ width + _ height); }

class Rectangle_new(Char color,double width,double height) {
    class self=newO(class);
    Shape_set_color((Obj)self,color);
    _ width=width; _ height=height;
    return(self);
    }
#undef class // Rectangle

// 4. POLYMORPHISM (Usage Example)
void print_shape_details(Obj shape) { // This function accepts any subclass of Shape.
    printf("--- %12.12s  Details  Color:%-8.8s",kindO(shape),Shape_color(shape));
    printf("Area: %3.2f  ",obj_(calculate_area,shape));
    printf("Perimeter: %3.2f  ---\n",obj_(calculate_perimeter,shape));
    }

 int main() {
    Circle my_circle = Circle_new(Os("Blue"),5.0);
    Obj my_rectangle = Rectangle_new(Os("Green"),4.0,6.0);

    print_shape_details(my_circle);
    print_shape_details(my_rectangle);
    }


/* OUTPUT:
---       Circle  Details  Color:Blue      Area: 78.54  Perimeter: 31.42  ---
---    Rectangle  Details  Color:Green     Area: 24.00  Perimeter: 20.00  ---
*/
33 Upvotes

45 comments sorted by

View all comments

Show parent comment

2

u/actguru Sep 08 '26

Thank you for your suggestion. I am aware of GObject.

From what I understand: there is a lot of boiler-plate, odd padding for virtual function tables, dispose() called more than once, etc.

It seems to work for Linux GUI work, etc. But its adoption seems highly specialized.

1

u/musbur Sep 08 '26

This is very typical in C: Either you have a large "universal" library that requires lots of boilerplarte code like gobject, or you make a small bespoke solution for the particular problem at hand. Which is what you did.

1

u/actguru Sep 08 '26

It would be very helpful if you could point out what part of this example makes it a "bespoke solution" ?

1

u/catbrane Sep 28 '26

They have macros for most of the boilerplate, so it's not that painful.

They have _dispose() (drop refs, can be called more than once) and _finalize() (free stuff, only called once) as separate things so they can break ref count cycles.

I don't think there's odd padding in vtables, is there? They have multiple inheritance of interfaces, maybe you're thinking of that.

There's a signal/slot system (like qt, obj-c) and lazy binding, which is nice.

It has full run-time introspection, so languages like python and ruby can use gobject classes in a simple way, including things like deriving from gobject.

It's used for non-GUI projects too. The introspection system is very handy.

2

u/catbrane Sep 28 '26

I'd say the big con was that gobject is relatively heavy. You wouldn't use it to make a container type, for example. And there are no type parameters.

1

u/actguru Sep 29 '26

I agree and it seems to me that any good object system would make container classes easy to define. One of my problems with gobject is that it looks like someone took the crude idea of nesting structures as the basis for an object system. This problem is avoided in Coda-C with a spindle object that is dynamically created allowing libraries and programs to be updated independently without breaking things.

1

u/catbrane Sep 29 '26

gobject is a nice thing, don't knock it until you've looked into it in detail. I'd say it was more that gobject is solving a different problem.

They need an object system for GUI programming, so it must have introspection, type-safe late binding, multiple inheritance of interfaces, dynamic properties, all that stuff, and that's inevitably enough extra machinery that you wouldn't use it on very small objects.

You see something similar in Objective-C: they have two function calls, one which uses the object system and one which doesn't, because their object system has enough of a runtime cost that you can't use it everywhere.

I'd guess your library is lower-overhead then gobject, which is great, but it's also much more compile-time and therefore less dynamic and less suitable for making a GUI.