Constructor & Destructor in C++

programming

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++

#include <iostream>
using namespace std;
 
class TestClass {
int myVariable;
public:
    //constructor
    TestClass(){
        this->myVariable = 5;
    }

    void display(){
        cout << "Value of myVariable is: " << this->myVariable << endl;
    }
};
 
int main()
{
    TestClass object;
   //creating instance of TestClass
    object.display();
    
    return 0;
}

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:

TestClass(){ 
  this->myVariable = 5; 
}

Parameterized constructor

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

Example:

TestClass(int val){
   this->myVariable = val; 
}

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:

public: 
  TestClass(){ }

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++

#include <iostream>
using namespace std;
 
class TestClass {
public:
    //Constructor
    TestClass(){
       cout <<"Constructor Called" << endl;
    }
    
    //Destructor
    ~TestClass(){
        cout <<"Destructor Called" << endl;
    }
};
 
int main()
{
    //createing object
    TestClass object;
    //object is destroyed when main function ends
    return 0;
}

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

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