Object Oriented Programming with C

Digin Antony
2 min readOct 28, 2020

Is C is a Object Oriented programming language?

No its definitely not then how to write object oriented programming with C yeah that is an important question how?

Here we deal with concept of object oriented with help of user defined datatype such as struct. A struct in the C programming language is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the same address. The struct data type can contain other data types so is used for mixed-data-type records.

defining an structure

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;

so we can define structure and we can access member items using arrow operator (->) or dot operator( . ) in object oriented programming we can access the member variable using member function of the object. so it fine to define member variable so what about member functions how we can do that ? we can do that with help of function pointer. a pointer pointed to a function

lets define an structure have only single member variable and a setter and getter function for the variable

#include<stdio.h>
struct Object{
int (*getvalue)(struct Object *);
void (*setvalue)(struct Object *,int);
int value;
};

getvalue , setvalue are two function pointer to set value to member variable value so lets define that function can be pointed by the function pointers

void setervalue(struct Object* instace,int value){
instace->value=value;
}
int getervalue(struct Object* instace){
return instace->value;
}

these function receive the object instance as the first argument so that we can set and get value of that instance in getvalue function we need to pass only the instance but in setervalue we pass instance and value that need to be set to the instance member variable

int main(){struct Object obj1; //Creating instance of  Object
obj1.setvalue=setervalue;//assigning actual function to function pointers
obj1.getvalue=getervalue;//assigning actual function to function pointers
obj1.setvalue(&obj1,10);
printf("The value is %d",obj1.getvalue(&obj1));
return 0;
}

Output

The value is 10

Summary

This article outlined the how to implement Object oriented programming in C even though c is a procedure language the concept oops is implement with help of structure and function pointers. I hope this knowledge is sufficient make your own simple object oriented program with c thank you!

Thanks for reading this article! Leave a comment below if you have any questions. Be sure to sign up to get the latest tips, tricks, and news about writing python programs and other web development technologies etc. to share your comments and ideas and connect with other writers.

--

--

Digin Antony

Full stack Developer and data scientist experienced graphic designer undertaking freelance project for all type of organisations