Home System Programming Learn about Constructors as Object Oriented Programming in C++

Learn about Constructors as Object Oriented Programming in C++



After introducing the Object-Oriented Programming concept: what it is, and what it is used for. And after we wrote our first Class program, we are going to build up on what we have learned in the previous article and go an extra mile. So, bear with us, and you won’t regret it.

Always Remember
A Class can be thought of like a model, pattern, or blueprint that specifies the attributes (variables) and methods (functions) that will exist in the instances (objects) of that class.
An Object is an instance of a defined class. It is the actual implementation of the sketched blueprint.


As mentioned in the previous article that the relation between an object and a class is analogous to that between a variable and the data type it belongs to. As you initialize a variable by assigning an initial value to it, there should be also a way to initialize an object. For this reason, Constructors comes to the scene.

I could use the set methods to assign values to the member variables, so why complicate things by using  “Constructors”?!

Actually, Constructors were introduced to simplify things, not to complicate it as you may think. Refer to the example in the previous article. We have created the P object of the class point.

Then, we used the setx() and sety() member functions to set values for the member variables x and y:

So, three statements were required. That is what we used in our first class program, and this is exactly the way you have just suggested to initialize the data items in an object.

– Yes, that is exactly what I mean.

Okay, it is a good way. However, don’t you agree with me that it would be much better if we could do the object creation and initialization steps in one single statement instead of three?

– Mmm…Yes, it would be better, but how?!


What are Constructors?

A Constructor is a public member function, whose name is the same as the class name, which is called automatically on object creation.

In this example, we are going to re-write the point class to allow the initialization of objects when created.

Consider the following modified version:

When executed, the program should behave as follows:


The class definition has been modified by adding the constructor function definition:

The constructor is a member function that is given the same name as the class in which it is defined (point in this case). The following rules must be taken into account when defining a constructor for a class:

  • The constructor must be public.
  • The constructor can’t specify a return type, even void.

The other modification is in the main() function:

Objects P1 and P2 are created and initialized in one step (for each).

Constructor Overloading

As a function, a constructor can be overloaded. This is useful to provide more than on way to initialize an object.

In this example, we are going to write a program that implements the counter functionality. The counter could be initialized to any positive integer value. If not initialized, the counter is set to zero. The counter should have a set of member functions that increment, decrement, reset (set to zero), and retrieve its value.

Consider the following code:

Let’s see it in action:


In this program, two constructors have been defined:

The one that expects an argument (initial value):

And the one that doesn’t have an argument:

Obviously, the decision to invoke the former or the latter depends on the object being created. For example, when the object c1 was created with no initial value provided:

the second constructor is called. One the other hand, when the object c2 was created with initial value:

the first constructor is automatically called.

In this article, we talked about Constructors as a way to initialize objects on their creation.

  • A Constructor is a member function, that is called automatically when an object is created.
  • A constructor must: be public, have the same name as the class it is defined in, and not specify any return value.
  • As any other function, constructors can be overloaded.

In the next article, we will continue with OOP. We will finish Constructors; introduce Destructors, and Static Class Attributes. See you there.



Please enter your comment!
Please enter your name here

Become An Insider!

Discover latest news, tech updates, fresh 
arrivals, sale announcements and exciting offers!
I'm In!
Hurry up! Offer valid till stocks last.
Share This