Introduction to OOP in C
Overview
"OOP in C" is a project that facilitates object-oriented programming concepts within the C programming language. Hosted by Quantum Leaps, the project includes code examples and educational resources that bring traditional object-oriented paradigms—like encapsulation, inheritance, and polymorphism—into the realm of C programming. While being perfect for desktop computers running Windows, Linux, or macOS, the project is also suitable for real-time embedded systems.
Educational Resources
One of the highlights of the OOP-in-C project is a comprehensive series of videos that walk through the application of OOP principles in C. The series includes:
- Encapsulation
- Inheritance
- Polymorphism in C++
- Polymorphism in C
Each video focuses on a core aspect of object-oriented programming, showcasing how these concepts can be applied using C.
Quick Example: The Shape Class
The project provides practical examples, such as the implementation of a Shape
class in ISO-compliant C language. This class illustrates basic object-oriented constructs such as encapsulation and interface design. Here's an overview:
- Attributes: The
Shape
class possesses basic attributes likex
andy
coordinates. - Operations: Methods available include constructor, move function, and getters for coordinates.
The C code makes use of structs to define the class attributes and functions for operations, enabling an organized and modular codebase.
// Shape's attributes...
typedef struct {
int16_t x; // x-coordinate of Shape's position
int16_t y; // y-coordinate of Shape's position
} Shape;
// Shape's operations (Shape's interface)...
void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);
int16_t Shape_getX(Shape const * const me);
int16_t Shape_getY(Shape const * const me);
The above code provides a foundation upon which developers can build more complex and structured applications in C.
Code Structure
The project is neatly organized into several directories, each demonstrating a different OOP concept:
- Encapsulation: Shows how data hiding and object interfaces can be achieved.
- Inheritance: Demonstrates deriving new classes from existing ones.
- Polymorphism: Focuses on call hierarchies and overriding functions.
Building and Running the Code
Set up for development is straightforward, with make.bat
scripts provided for Windows users to compile and execute the code examples. Alternatively, Linux and macOS users can run the necessary commands directly in the terminal.
C:\GitHub\OOP-in-C>cd encapsulation
C:\GitHub\OOP-in-C\encapsulation>make
gcc shape.c main.c -o oop_in_c
oop_in_c
Shape s1(x=0,y=1)
Shape s2(x=-1,y=2)
Shape s1(x=2,y=-3)
Shape s2(x=0,y=0)
Further Reading and Licensing
For those favoring a written guide, the project also offers a PDF document detailing the intricacies of implementing OOP in C. All source code and examples are distributed under the MIT open source license, encouraging broad use and adaptation.
Get Involved
Individuals passionate about programming and software design are encouraged to support the project by starring it on GitHub. This gesture not only shows appreciation but helps build a larger community around these educational resources.
For more information and continuous updates, visit the project’s website state-machine.com/oop.