Object Oriented Programming (OOP) in C

by Sanjay Mishra

Object Oriented Programming is a way of modeling real life that fits naturally in many situations. It success has resulted in many modern languages like C++, Java, C# providing built in support for object oriented programming concepts.

In deep embedded programming the language used most often is C. The good news is that while C does not support Object Oriented Programming concepts as a part of the language, with a little discipline and programming structure it is possible to enjoy many of the benefits of OOP in C.

Many who have been programming for a long, long time tend to dismiss OOPs out of hand. I would request those of you who do do so keep an open mind and try OOP design and programming out in a small project. I first used OOP while developing a satellite simulator in the early nineties and have found it to be a good for many but not all projects.

Object Oriented design and programming work flow

The first step is to model your system as classes of objects. A class is the type of on object while an object is an instance of it.

Thick books have been written about this and learned people hold lengthy discourse on how to do this. However the best approach that I have found is to describe in words how the module works. Then take a look at the description and map the nouns in the description to objects and the verbs to function calls on the objects.

Although not rigorous this approach has stood me in good stead. My goal while designing a system is to be able to express the concepts of the domain in a natural and easy to use way and this approach helps quite a bit in getting started.

As your system evolves and as you develop the implementation, you will of course change the description and the class hierarchy based on your improved understanding. You may choose to document the system in Unified Modeling Language (UML) or in a system as offered by Event Helix Design Studio.

Once you have your classes laid out. You are ready to implement them.

OOP in C : Implementing classes and objects

Objects can be thought of as structure with associated functions.

We extend the functionality as exposed in Modular Programming in C.

Simple Classes and Objects

We define a separate header file and a separate implementation file for each class.

The variables for each class are stored together in a structure for each class that is defined in the header.

The public functions for each class are also defined and exposed in the header file.

The first parameter for each public function of the class is a pointer to a structure that contains the variables which define each class.

The header file also contains any constant definitions required by the class.

It is good practice to prefix all public function definitions with the name of the class. This has the effect of ensuring that your class function names will not conflict with any other function names.

The implementation file contains the implementation of publicly defined functions.

The implementation file also contains the any helper functions and internal variables that are not exposed. Such variables and helper functions should be declared as static so that they cannot be accessed from outside.

I find it good programming practice to prefix internal variables and function declarations with "_"

Inheritance and Polymorphism

The structure for inherited classes expands on the structure of the base class by adding more variables.

We can define a table of function pointers for exposed public functions that are virtual. Each derived class puts its own function pointer entry in this table. When the function is called, it gets routed to the right derived class function.

An alternative to using a table of function pointers is to use a switch statement. I find the function pointer scheme of routing calls more elegant and easier to extend and use.

Top of Object Oreinted C

Example

Coming soon! Check back in a couple of weeks. Because of the need to protect the privacy of clients or code size I cannot display existing code. I will come up with an example specifically for this page when I have a chance. If you have a particular type of example you would like to see let me know.

Top of OOP in C

Need More Help? Have An Opinion?

Do you need more help to solve your problem? Would you like to ask the author a question about your specific problem? Do you have a great idea about this?

We will post an answer within 2 business days. If you need more immediate assistance or you would like to discuss your issue privately, please use our contact us form or call us at 1-888-215-8557. We love solving technical issues and there is no charge if we solve your problem over email or over a short phone call.

[ ? ]

Author Information (optional)

To receive credit as the author, enter your information below.

(first or full name)

(e.g., City, State, Country)

Submit Your Contribution

  •  submission guidelines.


(You can preview and edit on the next page)