Implementing a class with Object Orientated Programming principles - C++ Better Explained

Note: If you haven’t read the previous article, Understanding Basic Object Orientated Programming and Classes, I would strongly recommend reading it first before you dive into this article as the code examples and concepts are being extended from that article.

In the previous article of Understanding Basic Object Orientated Programming and Classes, we look at creating a simple class that would store three variables that would be represented as objects in the created class. I was contacted by a reader saying that “classes are meant to encapsulate behaviour and teaching people that classes are meant to hold only data is the best way to teach them how to create anemic models and god classes”. While he is absolutely correct, I was only showing a very basic example of how to create a class with variables represented as objects.

In this article, we are going to look at adding in functions to the class and creating constructors and destructors which allows us to encapsulate the behaviour. For those who do not know what Encapsulation is, it is simply the process of packing data and functions into a single component.

Adding in functions to a class

The previous sample Car class only contained data, but no functions. We can easily add in a function by entering a function prototype.

car class

Implementing a function from a class

When you are implementing a function from a class, you must tell the complier what type of function it is.

implementng a function from a class diagramAs you can see, the :: operator, we are implementing a function declared from a class. This links a class name with a member name in order to tell the complier what class the member belongs to. In this example, the “car_weight()” belongs to the Car class.

function implementation If you haven’t understood how to create functions, read How to create and understand functions.

Constructors and Destructors

Constructors and Destructors is a common concept in object-orientated programming. The definition of a constructor is that they are used to initialize an object when it is created. When you use a constructor, you will give initial values to variables defined in the class.

The Destructor does the opposite of a constructor. When objects in a class are no longer being used, the destructor can de-allocate memory it previously took up. To add in a destructor to the class, all you have to do is add in a “~” next to the name of the class name. When you are implementing a destructor in the code, you do not need to manually configure each variable in the class, as the complier will automatically do it.

class steps

The car class containing a constructor and destructor

car class with con and des

Implementing a constructor and destructor

car class con and des implementation

Applying Object Orientated Programming principles to display the values set in the constructor.

car main implemenation

deconstructor output

As you can see in the output screenshot, when the values of the class have been displayed, the destructor will then clear the allocated memory of the variables due to the de constructor.

Share this post