Destructors in C++

A destructor is also a special member function as a constructor. Destructor destroys the class objects created by the constructor. Destructor has the same name as their class name preceded by a tilde (~) symbol. It is not possible to define more than one destructor. The destructor is only one way to destroy the object created by the constructor. Hence destructor can-not be overloaded. Destructor neither requires any argument nor returns any value. It is automatically called when the object goes out of scope.  Destructors release memory space occupied by the objects created by the constructor. In destructor, objects are destroyed in the reverse of object creation.

Syntax of Destructors in C++

Like constructors, destructors can also be defined either inside or outside of the class.

The syntax for defining the destructor within the class

~ <class-name>(){}

The syntax for defining the destructor outside the class

<class-name>: : ~<class-name>(){}

Examples of Destructors in C++

The below examples demonstrate how to use the destructors in C++.

Example 1: Defining a Simple Destructor

C++




#include <iostream>
using namespace std;
 
class Test {
public:
    Test() { cout << "\n Constructor executed"; }
 
    ~Test() { cout << "\n Destructor executed"; }
};
main()
{
    Test t;
 
    return 0;
}


Output

 Constructor executed
 Destructor executed

Example 2: Counting the Number of Times Object is Created and Destroyed

C++




// C++ Program to count the number of objects created and
// destroyed
#include <iostream>
using namespace std;
 
// global variable to count
int count = 0;
 
// class definition
class Test {
public:
    Test()
    {
        count++;
        cout << "No. of Object created: " << count << endl;
    }
 
    ~Test()
    {
        cout << "No. of Object destroyed: " << count
             << endl;
        --count;
    }
};
 
// driver code
int main()
{
    Test t, t1, t2, t3;
    return 0;
}


Output

No. of Object created: 1
No. of Object created: 2
No. of Object created: 3
No. of Object created: 4
No. of Object destroyed: 4
No. of Object destroyed: 3
No. of Object destroyed: 2
No. of Object destroyed: 1

Characteristics of Destructors in C++

The following are some main characteristics of destructors in C++:

  • Destructor is invoked automatically by the compiler when its corresponding constructor goes out of scope and releases the memory space that is no longer required by the program.
  • Destructor neither requires any argument nor returns any value therefore it cannot be overloaded.
  • Destructor  cannot be declared as static and const;
  • Destructor should be declared in the public section of the program.
  • Destructor is called in the reverse order of its constructor invocation.

Constructors in C++

Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. It constructs the values i.e. provides data for the object which is why it is known as a constructor.

  • Constructor is a member function of a class, whose name is the same as the class name.
  • Constructor is a special type of member function that is used to initialize the data members for an object of a class automatically when an object of the same class is created.
  • Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as a constructor.
  • Constructors do not return value, hence they do not have a return type.
  • A constructor gets called automatically when we create the object of the class.
  • Constructors can be overloaded.
  • A constructor can not be declared virtual.

Similar Reads

Syntax of Constructors in C++

The prototype of the constructor looks like this:...

Examples of Constructors in C++

The below examples demonstrate how to declare constructors for a class in C++:...

Characteristics of Constructors in C++

...

Types of Constructor in C++

...

1. Default Constructor in C++

The following are some main characteristics of the constructors in C++:...

2. Parameterized Constructor in C++

Constructors can be classified based on in which situations they are being used. There are 4 types of constructors in C++:...

3. Copy Constructor in C++

A default constructor is a constructor that doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor....

4. Move Constructor in C++

...

Destructors in C++

...

Frequently Asked Questions on C++ Constructors

Parameterized Constructors make it possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object....