Summary: In this tutorial, we will learn with examples about constructors and destructors in C++ with their types and significance.

C++ being an object-oriented programming language allow us to take advantage of class and objects to decrease the complexity and promote code reusability in our program.

Constructor and destructor are one of the important properties of the class that helps in achieving the same.

What is Constructor?

A constructor is a special member function of a class that has the same name as the class, has no return type, and is automatically invoked when an instance of the class is created.

Example of Constructor in C++

Output:

Value of myVariable is: 5

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

How and When did this happen?

This happened when we created an instance of TestClass (i.e. object) in the main function.

The constructor was automatically invoked during object creation.

The main objective of constructors in C++ is to initialize the properties such as variables of the class.

Types of Constructors in C++

Depending on the number of parameters, a constructor can be classified into two categories:

  1. Non parametrized constructor.
  2. Parameterized constructor.

Non parametrized constructor

Non parametrized constructors are the constructors with no arguments.

Example:

Parameterized constructor

Parametrized constructors are the constructors with arguments. It is mainly used to initialize objects with user denied values.

Example:

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

In our first program, we explicitly defined a constructor, so it can be called a user-defined constructor.

When we don’t define any constructors in the class, the compiler automatically creates one similar to this:

We can overload constructor in C++ by writing multiple parameterized constructors with different function signatures.

Note: It is good practice to explicitly define the default constructor in the class.

What is Destructor?

The destructor like constructor is a special member method that has the same name as the class, does not have any return type, accepts no arguments but is prefixed with a tilde (~) in its declaration.

A class can only have a maximum of one destructor and is invoked automatically when an instance of the class is destroyed or released from the memory.

Example of Destructor in C++

Output:

Constructor Called
Destructor Called

The main object of the destructor is to releasing memory and other resources.

One such use case is to delete a raw pointer (one of the properties of the class) inside the destructor before the object is completely destroyed to prevent a memory leak.

Note: Like the constructor, we cannot overload destructors.

In this tutorial, we learned what constructors and destructors are and what are their significance in the C++ programming language.

Leave a Reply

10 + one =