r/C_Programming • u/actguru • 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
•
u/github-guard Sep 02 '26
🔍 GitHub Guard: Trust Report
This project scored 3/6 on our safety audit.
Audit Breakdown:
30
u/musbur Sep 04 '26
You are aware of glib/gobject?
4
u/Lucrecious Sep 05 '26
Simplify?? Have you seen your own examples?
13
u/Turbulent_File3904 Sep 04 '26 edited Sep 04 '26
um i mean it look confuseing as hell.
you can have one struct/type implement multiple interfaces, add interface implementation for anytype at anytime as long as you can access the struct definition. btw this is how linux does oop
10
u/QuirkyXoo Sep 04 '26
If you want to use OOP in C there is already a thing called "C with classes", although the term is frequently used colloquially to refer to a programming style rather than a strict set of rules.
18
u/rivenjg Sep 04 '26
IF WE WANTED OOP WE WOULDN'T USE C.
4
u/ReDucTor Sep 06 '26
You don't need a library to write OOP style code in C, you also dont need the class keyword to write OOP style code.
Many C code bases are OOP style, even if people believe they are not, you will do things like
Want a virtual interface? Use a struct with function pointers.
Want to hide data? Use an opaque pointer.
Want to have inheritance? Have the first member be the base class.
Go look at the Linux Kernel it uses object oriented patterns all over the place https://lwn.net/Articles/444910/
14
u/tastygames_official Sep 04 '26
> examples that illustrate cases where an Object Oriented design can simplify C-Code
it can't. If you feel that you need OOP (which by definition cannot simplify anything), then use C++.
3
u/Altaic___Brothership Sep 06 '26
We've agreed to call "oop in c" as C++
1
u/AutoModerator Sep 02 '26
Hi /u/actguru,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/SeriousPug Sep 05 '26
Convince me that if i want to program with classes, i should take your approach instead of C
1
u/pconroy329 Sep 04 '26
I appreciate anyone's effort, if perhaps academic. I'm an OOP fan. I did C++ early on and switched to Java in 2000. If I've got a problem that needs an OOP solution, I'm picking one of the many mature compilers/interpreters out there. When I pick C it's because I think a mature, procedural solution is best. I think you've invented a New Hammer, when I'm already awash in hammers. Good luck!
1
u/_evilpenguin Sep 06 '26
checkout objc :)
1
u/Sweet_Pianist8564 Sep 08 '26
Isn't that what the Gobject is for?
-4
u/Eastern-Turnover348 Sep 04 '26
C wasn't designed to be object orientated; matter closed.
Great languages were designed to solve a specific problem.
36
u/[deleted] Sep 04 '26
[removed] — view removed comment
1
u/[deleted] Sep 06 '26 edited Sep 06 '26
[removed] — view removed comment