Since C++ is an Object Oriented Programming (OOP), it allows the user to create class and objects mainly to decrease the complexity of the program. And without class, there would be no constructor in C++.

We can say that the constructor is one of the properties of the class in C++.

What is Constructor?

A constructor is a special type of function:

  • Which has the same name as of class name.
  • Doesn’t have any return type (not even return type).
  • Called automatically when the object of the class is created.

Constructor in C++ Example

Output

Constructor overloading in C++

Notice that in the output the value of myVariable is 5. I have assigned 5 to myVariable in the constructor. Also, I have not called the constructor explicitly.

How & When did this happen?

This happened when we created an object of TestClass in the main function. Constructor gets automatically invoked during object creation.

Types of Constructors in C++

The constructor can be of two types:

  1. Non parametrized constructor.
  2. Parameterized constructor.

Non parametrized constructor

The constructor which do not accept any arguments is known as Non parametrized constructor.

Parametrized constructor

The constructor which do accept one or more arguments is known as a Parametrized constructor.

From the user point of view, the constructor can also be classified into default and user-defined constructors.

In our first program, we explicitly defined a constructor, hence it can be said as a user-defined constructor.

when we do not define any constructor in the class, the compiler automatically creates one which looks like this.

Note: when we write only parametrized constructor in the class and try to create an object with no arguments then it will show compiler error. In this case, you have to explicitly define non-parametrized constructor if you need.

Constructor overloading

When more than one constructor has the same name as that of class, but with different numbers and types of arguments then the constructor is said to be overloaded.

Example:

Output

Constructor in C++

Constructor Initialization List

In the previous example, myVariable is a global variable declared inside the class.

Notice we haven’t initialize it so before the constructor call, it contains garbage value and this garbage value gets replaced when we initialize myVariable in the constructor which only occurs at the time of object creation.

Constructor Initialization List is an initialization method because of which we can directly initialize myVariable without letting it store any garbage value.

How to do it?

Thus Constructor initialization list is more efficient and initialization list immediately follows the parameter list.

Delegating Constructor

C++ provides the delegating constructor feature. With delegating constructors, we can call an overloaded constructor from another overloaded constructor in the initialization list.

To use delegating simply call another constructor passing the value in the initialization list.

Example

This code produces the same output as the previous one.

Destructor

A destructor is a special member method that has the same name as the class prefixed with a tilde (~).

  • It has no return type and no parameters and is invoked manually when the object is destroyed.
  • It is useful in releasing memory and other resources.
  • Only 1 destructor is allowed per class.

That’s all for the constructor and destructor in C++. If you have any doubts then comment below.

Leave a Reply

Close Menu