In this post, we are going to tell you about the techniques for implementing a C++ class using C. But before we get on with the techniques,
Design a C++ Class using C Language
let’s understand how close we can design a C structure to imitate a C++ class. The answer varies depending on the kind of object-oriented features you would like to have. Today, I’ll demonstrate adding three of the most commonly used object-oriented features to C structures.
Check out: 15 C Programming Tips and Tricks for Beginners
Define C++ Class Constructors in C
It’s the first step you need to execute for implementing a C++ class using C.
Constructors are exclusive functions that initialize an object of a class. In a similar pattern, we have to write a constructor-styled function that does the initialization for structure instances. Since the structure is a data type, you can’t directly assign values to it. You have to set values to instances or objects of such data types.
Let’s look into the code examples now.
1.1- In this example, we are providing the definition of a single argument constructor which initializes a structure called Object.
// Single argument Constructor to allocate memory // and set default values Object * Object_new(int index) { Object * obj = malloc(sizeof(Object)); obj - > index = index; obj - > value = 0; return obj; } ... // Using constructor from your code Object * O1 = Object_new(index++); O1.value = 10;
In case you require multiple constructors for the Object structure, then you would need to decorate the function names. You can’t have more than one Object_new() function.
1.2- Follow the below example which allows an additional argument value and calls the single argument constructor function.
Object * Object_new_with_value(int index, int value) { Object * obj = Object_new(index); obj - > index = index; obj - > value = value; return obj; }
Integrate C++ Class Encapsulation in your C Program
It’s another step toward implementing a C++ class using C. You easily do it by keeping the structure’s definitions in the source (.c) file instead of putting them in the header. And let the outer world access our objects through pointers, we only need to expose functions accepting such pointers as the “methods” of our objects.
Introduce Polymorphism using Function Pointers
Polymorphism in “C” can be implemented using function pointers. In other words, by introducing a compound structure that holds both the data and pointers to functions that manipulate the data. For example, let’s define a Database Adaptor class that connects a DB, selects a table, and executes a query without bothering about the type of DB being used. DB type should be allowed to switch dynamically.
// DB Api class exposing generic methods with the help of virtual pointers typedef struct { int( * init)(void * sqlHandle); int( * connect)(void * sql, char * hostname, char * user, char * pwd, char * database); int( * close)(void * sql); } DBApi; // DB class inheriting the DB Api class typedef struct { DBApi api; void * handle; } DBClass; DBClass db; // DBClass object holding the MYSQL Api reference db.api.init = & mysql_init; db.api.connect = & mysql_real_connect; db.api.close = & mysql_close; // DBClass object holding the Mongo Api reference db.api.init = & mongo_init; db.api.connect = & mongo_connect; db.api.close = & mongo_destroy;
Here is a basic example of usage.
// Test program // int main(void) { int status; DBClass db; // Init DBClass object with MYSQL Api reference db.api.init = & mysql_init; db.api.connect = & mysql_real_connect; db.api.close = & mysql_close; // Initializes MYSQL DB instance and call connect db.api.init(db.api.handle); status = (db.api.connect)(db.api.handle, "localhost:8080", "usr", "pwd", "default"); // Init the same DBClass object with Mongo Api reference db.api.init = & mongo_init; db.api.connect = & mongo_connect; db.api.close = & mongo_destroy; // Initializes Mongo DB instance and call connect db.api.init(db.api.handle); status = (db.api.connect)(db.api.handle, "localhost:8080", "usr", "pwd", "default"); return 0; }
Final Word
It would be nice if this tutorial would’ve left you with some great ideas about creating a successful and rich C++ class-like framework using C programming. We would suggest you try out the things that you’ve learned from here. If you experiment with it later, then do share your experiences with us.
And if you like being here reading this particular post, please do share it with your friends or on social media of your choice.
Since legends have said that C programming is like an ocean to explore you can find out more details on Object-oriented programming using C language, follow the link.
Keep Learning,
TechBeamers