Debunking OOPs in C : Part 2

Amogh Singhal
3 min readJan 28, 2018

--

Understanding object oriented paradigm through C programming

OOPs, I did it again! Expectation vs. Reality !

In the previous article, I explained the motivation behind this article, what do you mean by object-oriented paradigm and described some points of precaution in this approach. Let’s move on to the next set of principles:

Objects and methods

We will try to make an instance (or object) of the Rectangle class which we have implemented earlier. The method will usually help to describe the behavior of the object. As in this case, the draw method helps us identify the behavior of the Rectangle object, by giving us information about that object.

Object creation and calling methods in C.
Object creation and calling methods in C++

Constructors and Destructors

Constructors are special methods that share the same name as class name and initializes an object with values, after storage for it is allocated. Unlike C++, we have to perform manual memory allocation in C before we assign any value to the object. Please refer the code for more clarity.

Constructor implemented in C. Notice that we are allocating memory before assigning any values to the object.
Constructor implemented in C++. The `new` keyword calls the constructor and implicitly perform memory allocation.

Destructors are special methods that share the same name as the class name (prefixed with ~ tidle) and destroy an object as soon as it goes out of scope. In C++, it is implicitly defined and called when the object is no longer needed. In case of C, it has to be explicitly defined and called manually if needed.

Other Features

There are some other features that OOPs offer along with the ones discussed above:

  1. Encapsulation: implementation details of a class is hidden from the user.
  2. Inheritance: access to data is restricted to members of that class or it’s child classes.
  3. Abstraction: reduces the amount of details needed to know when trying to use a class.
  4. Polymorphism: assigning different behaviors in regards to different contexts to the same entity.

Encapsulation

Making sense of Encapsulation

As explained earlier, encapsulation is a mechanism of data hiding. The data is not accessed directly (their accessibility is private), but through the methods (their accessibility is public). There is a third variant of accessibility as well: protected.

Inheritance

Image result for inheritance in oops meme
Understanding Inheritance with the help of a diagram

As the name suggests, inheritance allows new classes to receive properties and method from the existing classes and make the code re-usable. Following our previous example, I can make a parent class Shape, which will support the draw method. The Rectangle child class can inherit from Shape and use the draw method with its own object. Similarly, other child classes can re-use the properties and methods of Shape to implement their own version of draw method.

Wait a second ! Implementing their own version of the draw method ? Isn’t it already implemented. Yes, this is where polymorphism comes into play.

Polymorphism

The Speak() method for different animals will yield different results.

It allows values of different data types to be handled using an
uniform interface. A polymorphic method can be evaluated or applied to values of different types. For example, the Rectangle object requires height and width in their version of the draw method. In case of Polygon, we require the number of sides and length. We can make our draw method polymorphic for it’s child classes to implement their own version of the same method.

Talk is cheap. Show me the code.

Implementing the Shape class in C

That’s it from my end. Thank you for your time and not sleeping :) . Hope that you have understood the OOPs concepts and how to implement them in C. If you have 12 seconds, I’d love to read your comment below..If this post was helpful, please click the clap 👏button below a few times to show your support!⬇⬇

--

--