Note: If you haven’t read the previous article, Understanding Basic Object Orientated Programming and Classes, I 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 Understanding Basic Object Orientated Programming and Classes, we looked at creating a simple c++ class. It stores three variables that are represented as objects in the created class. I was contacted by a reader saying that “classes are meant to encapsulate behavior. And teaching people that classes are meant to hold only data to create anemic models and god classes”. While he is absolutely correct, I only show a basic example of how to create a class with variables represented as objects.

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

Adding functions to a c++ class

The previous sample Car class only contained data, but no functions. We can easily add 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 compiler 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 compiler 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 are common concepts in object-orientated programming. The definition of a constructor is that it is 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 used, the destructor de-allocates the memory it previously took up. To add a destructor to the class, all you have to do is add 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 compiler will automatically do it.

class steps

The car class contains 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, when the values of the class are displayed, the destructor will then clear the allocated memory of the variables due to the deconstructor.

Share this post