3 minute read

What is Object Oriented Programming (OOP)?

Object Oriented Programming is a way of programming technique which enable programmer think like real-life entity or object. It emphasis on What object do or behave instead of how to do which functional programming focus on.

Advantages:

  • High code reusablity
  • New features can be easily built into the existing code
  • Allow perform unit testing

Class vs Object

Class: Prototype or Blueprint from which objects are created

Object: An existing entity in the codebase

Class Object
Set of attributes or properties that describes every object Set of data (value for each of its attribute)
Set of behavior or actions that every object can perform Set of actions that it can perform

OOP concepts

Four important concept in OOP, we dont go in details for each concept.

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

How to implement OOP inside C

In C programming, there is no any class syntax inside the standard header file. we need to design our class with the struct data structure and a custom header file.

Examlple problem set:

OOP UML diagram

Solution

First of all, we will try to implement the pet class, since it is a super class for the cat class.

Pet Class

we will define all the data structure and the function signature in the pet.h file

//pet.h
#include <stdint.h>
#ifndef PET_H
#define PET_H
/* Pet's attributes... */
typedef struct {
 char name[25]; /* Pet name */
 int16_t weight; /* Pet weight */
 char master_name[25]; /* Pet's master_name */
} Pet;
/* PET's operations (PET's interface)... */
void petCreate(Pet * const self, char* t_name, int16_t weight, char* t_master_name);
void petCall(Pet * const self);
#endif /* PET_H */

Regarding to the #ifndef PET_H, #define PET_H and #endif /* PET_H */, it is trying to define a static variable called PET_H therefore it can prevent recurisve import, which will raise error in c language.

we will define all the method detail within the pet.c file

//pet.c
#include "pet.h"
#include <stdio.h>
#include <string.h>

void petCreate(Pet * const self, char* t_name, int16_t weight, char* t_master_name){
    strcpy(self->name, t_name);
    self->weight = weight;
    strcpy(self->master_name, t_master_name);
    
};
void petCall(Pet * const self){
    printf("I am %s \n", self-> name);
    printf("I am %s's pet\n", self-> master_name);
};

Regarding to the petCreate function, the name of the pet need to copy with the strcpy method in string.h since c-string doesnt have proper = equal operation for string copying.

Dont’t do something like self->name = t_name, which C doesn’t know what to do for.

Cat Class

After designing the pet class, lets start the prototyping for the cat class.

//cat.h
#ifndef CAT_H
#define CAT_H
#include "pet.h" /* the pet class interface */
/* Cat's attributes... */
typedef struct {
 Pet super; /* <== inherits pet class */
 /* attributes added by this subclass... */
 char colour[25];
 int16_t sleeping_hour;
} Cat;
/* constructor prototype */
void catCreate(Cat * const self, char* t_name, int16_t weight, char* t_master_name, char* colour, int16_t sleeping_hr);
void catCallType(Cat * const self);
void catCallSleep(Cat * const self);
#endif /* CAT_H */

Regarding to the cat struct, I inherit the struct from pet by defining the a pet struct inside cat struct, therefore pet data segment will allocate before the cat data segment, then all the method in pet class, can still be used.

Similarily, I will define the function implementation in pet.c

//pet.c
#include "cat.h"
#include <stdio.h>
#include <string.h>

void catCreate(Cat * const self, char* t_name, int16_t weight, char* t_master_name, char* colour, int16_t sleeping_hr){
    petCreate(&self->super, t_name, weight, t_master_name);
    strcpy(self->colour, colour);
    self->sleeping_hour = sleeping_hr;
};

void catCallType(Cat * const self){
    printf("I am a cat\n");
};
void catCallSleep(Cat * const self){
    printf("I need to sleep for %d hours\n", self->sleeping_hour);
};

Now both pet and cat class have well defined! So how do we call the method?

//main.c
#include "cat.h"
#include <stdio.h>
int main() {
    Cat olivia;
    catCreate(&olivia, "olivia", 50, "Marco", "BLACK", 15);
    catCallType(&olivia);
    petCall(&olivia.super);
    catCallSleep(&olivia);
}

First of all, we define a main function. We need to first initialize the struct data for cat, so we call Cat olivia which olivia is the name of the data struct(keeping the cat details).

Secondly, We will call the catCreate function and initialize the cat object in the program, so that now we can call all the cat operation with the olivia data struct.

Sooooo Now, How can we call the pet operation, we need to call the pet function with olivia.super which the super is the data struct for the pet function.

Lets compile and see the output!

$ gcc main.c pet.c cat.c -o main
$ ./main
I am a cat
I am olivia 
I am Marco\'s pet
I need to sleep for 15 hours

Wohooo! You already know the technique of OOP in C!