Appendix B — Object-Oriented Programming in Pure C

B.1 Preamble

In this chapter, we focus on how to imitate Object-Oriented Programming (OOP) in Pure C language.

First of all, we must recognize that OOP is a programming paradigm, rather than a built-in language feature. In other words, this paradigm can, in principle, be implemented in any programming language — including C, which is fundamentally a procedural language.

Many modern programming languages provide syntactic support (often referred to as syntax sugar) to make OOP easier to express. By contrast, since C does not natively provide such syntax, we say that we imitate OOP in C by manually applying its principles and design patterns.

We will begin by introducing the key concepts of OOP — Encapsulation, Inheritance, and Polymorphism. After that, we will examine a simple example of how OOP can be implemented in pure C. Finally, we will look at some well-known C macros in the Linux kernel that embody OOP principles and discuss their usage.

B.2 Core Concepts of OOP

B.2.1 Encapsulation

  • Concept: hide internal data, expose methods
  • C implementation: struct + functions

B.2.2 Inheritance

  • Concept: reuse and extend behavior
  • C implementation: embedding structs

B.2.3 Polymorphism

  • Concept: multiple forms via dynamic dispatch
  • C implementation: function pointers / vtable

B.3 A Simple Example of OOP in Pure C

  • Step-by-step implementation
  • Combining encapsulation, inheritance, polymorphism
  • Small program students can compile and run

B.4 Advanced: C Macros Related to OOP in Linux Kernel

  • Macros like container_of, DEFINE_MUTEX, etc.
  • How macros emulate OOP patterns
  • Optional deep dive for advanced students