Constructor in C++ | Destructor | Overloading | Initialization List

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 constructor is one of the properties of a 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.

As 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++ allows delegating constructor. With delegating constructor one we can call another constructor from one constructor in the initialization list.

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

Example

This code produces same output as previous one.

Destructor

A destructor is a special member method which has the same name as the class proceeded 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.

Also checkout :

Some other C++ programs.

admin

I am a B.tech IT Student from India. Technology and programming is the most enthusiastic thing for me in this world. I like learning new techniques and use them for real-world application because I feel tech is future. Let's learn and grow TOGETHER.

Leave a Reply

Your email address will not be published. Required fields are marked *

shares