Object-Oriented Programming in C
C is a procedural language, it does not support OOP principles, like inheritance, or polymorphism. There are various approaches to adding OOP into C. All of them are different variants of type punning.
All these approaches are inherently unsafe. The compiler will not catch all of the issues, the program will just explode in the runtime (most likely “Segmentation Fault”).
Using Union
This simple approach allows us to add some kind of polymorphism into C.
Here’s a simple example:
A few points to note:
- we have a base type (
Person
) and two “deriving” types:Student
andWorker
. - the base type’s members have to be repeated in every “inheriting” type (in the exact same order!). This is due to the fact that we’re using a union, and the memory layout should be the same in every “derived” type. Any change in the base type requires changes in derived types (unsafe).
- we can recognize what the actual type is in the runtime via an enum.
- there’s only one level of inheritance
Using Parent Member
Let’s look at a different approach that uses composition:
Notes:
- this time, we have an inheritance chain:
Base
->UniversityWorker
->Professor
(multiple levels of inheritance). - we’re using pointer casting to treat
Professor
as aPerson
(unsafe)