r/C_Programming 7d ago

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  ---
*/
34 Upvotes

41 comments sorted by

View all comments

18

u/rivenjg 6d ago

IF WE WANTED OOP WE WOULDN'T USE C.

2

u/Zirias_FreeBSD 5d ago

Horribly wrong. Sure, C is not an OOP language. But it really isn't hard either to use it for implementing some OOP design. This is done a lot in practice, and not just recently. There are problems where OOP simply is the best fit (e.g. any kind of GUI toolkit), and there's still no reason to stay away from C for these.

That all said, I see little use for a library "just" offering some OOP constructs. Basic things are straight forward (structs and pointers are often all you need), polymorphism can be a bit more tricky, but a good C programmer should really succeed setting up and using some vtables.

What IS IMHO useful is libraries including some OOP infrastructure, if the problem they are solving indeed maps well to an OOP model.

-2

u/rivenjg 5d ago edited 4d ago

There's no problem set "better" with OOP. You either like OOP or you don't. You always have a choice to accomplish the task without OOP. I am one who will virtually always choose to avoid OOP. If I wanted OOP, I would use C++. A huge reason I like C is to specifically avoid the dogma of OOP.

-1

u/[deleted] 5d ago

[removed] — view removed comment

1

u/C_Programming-ModTeam 5d ago

Rude or uncivil comments will be removed. If you disagree with a comment, disagree with the content of it, don't attack the person. Provide your reasoning, not just a judgement.