OOP in CMany programmers mistakenly think that Object Oriented Programming (OOP) is possible only with OO languages, such as C++ or Java. However, OOP isn't the use of a particular language or a tool; it is a way of design that can be implemented in almost any language, such as C. Knowing how to code classes, inheritance, and polymorphism in C can be very useful for embedded programmers, who often maintain C code or deal with embedded microprocessors released to the field with nothing but a bare-bones C compiler.
As a C programmer, you already may have used ADTs (abstract data types). For example, in the Standard C runtime library, the family of functions that includes fopen(), fclose(), fread(), and fwrite() operates on objects of type FILE. The FILE ADT is encapsulated so that the clients have no need to access the internal attributes of FILE. (When was the last time you looked up what's inside the FILE structure?) You can think of the FILE structure and the associated methods that operate on it as the FILE class. The following bullet items summarize how the C runtime library implements the FILE "class":
- Attributes of the class are defined with a C struct (the FILE struct).
- Methods of the class are defined as C functions. Each function takes a pointer to the attribute structure (FILE *) as an argument. Class methods typically follow a common naming convention (e.g., all FILE class methods start with f).
- Special methods initialize and clean up the attribute structure (fopen() and fclose(), respectively). These methods play the roles of class constructor and destructor.
A simple way of implementing single inheritance in C is by literally embedding the superclass attribute structure as the first member of the subclass structure. (Please convince yourself that the resulting memory alignment lets you always treat any pointer to the subclass as a pointer to the superclass.) In particular, you can always pass this pointer to any C function that expects a pointer to the superclass. (To be strictly correct in C, you should explicitly upcast this pointer.) Therefore, all methods designed for the superclass are automatically available to subclasses; that is, they are inherited.
For example, Listing 2 shows how to derive class Keyboard from the base class Fsm (lines 3-7). The superclass instance super_ (Listing 2, line 5) is embedded as the first attribute of the subclass Keyboard.
For more information on implementing OOP in C (including polymorphism, which I don't use in the FSM example), see the first chapter of C+ C++: Programming With Objects in C and C++ by Allen Holub (McGraw-Hill, 1992); Reusable Software Components, Object-Oriented Embedded Systems Programming in C by Ted Van Sickle (Prentice Hall, 1997); CUJ articles from July 1990 and July 1993; or my article "Portable Inheritance and Polymorphism in C," ESP, December 1997. Needless to say, Googling around for "OOP in C" or "Inheritance in C" will reveal countless other resources.