Destructor is an instance member function that is invoked automatically whenever an object is going to be destroyed. Meaning, a destructor is the last function that is going to be called before an object is destroyed.
Syntax
Destructors are automatically present in every C++ class but we can also redefine them using the following syntax.
C++
~className(){
// Body of destructor
}
where, tilda(~) is used to create destructor of a className.
Just like any other member function of the class, we can define the destructor outside the class too:
C++
className {
public:
~className();
}
class-name :: ~className() {
// Desctructor Body
}
But we still need to at least declare the destructor inside the class.
Examples of Destructor
The below programs demonstrate the behaviour of the destructor in different cases:
Example 1
The below code demonstrates the automatic execution of constructors and destructors when objects are created and destroyed, respectively.
C++
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() {
cout << "Constructor Called"
<< endl;
}
// User-Defined Destructor
~Test() {
cout << "Destructor Called"
<< endl;
}
};
main() {
Test t;
return 0;
}
OutputConstructor Called
Destructor Called
Example 2
The below C++ program demonstrates the number of times constructors and destructors are called.
C++
#include <iostream>
using namespace std;
int Count = 0;
class Test {
public:
Test(){
// Number of times constructor is called
Count++;
cout << "No. of Object created: "
<< Count << endl;
}
~Test() {
// It will print count in decending order
cout << "No. of Object destroyed: " << Count
<< endl;
Count--;
}
};
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
Note: Objects are destroyed in the reverse order of their creation. In this case, t3 is the first to be destroyed, while t is the last.
When do we need to write a user-defined destructor?
If we do not write our own destructor in class, the compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in the class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leaks.
Example:
C++
#include <iostream>
using namespace std;
class MyClass {
private:
// Pointer to dynamically
// allocated memory
int* data;
public:
MyClass(int value) {
data = new int;
*data = value;
cout << *data << endl;
}
// User-defined destructor: Free
// the dynamically allocated memory
~MyClass() {
// Deallocate the dynamically
// allocated memory
delete data;
cout << "Destructor: Memory deallocated";
}
};
int main() {
MyClass obj1(10);
return 0;
}
Output10
Destructor: Memory deallocated
In the above example, when the object is destroyed, the destructor releases the dynamically allocated resources, which in this case is the pointer.
Characteristics of a Destructor
All the points mentioned below show the characteristics of a destructor:
- 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 cannot be overloaded.
- It cannot be declared static or const.
- Destructor neither requires any argument nor returns any value.
- It is automatically called when an object goes out of scope.
- Destructor release memory space occupied by the objects created by the constructor.
- In destructor, objects are destroyed in the reverse of an object creation.
When is the destructor called?
A destructor function is called automatically when the object goes out of scope or is deleted. Following are the cases where destructor is called:
- Destructor is called when the function ends.
- Destructor is called when the program ends.
- Destructor is called when a block containing local variables ends.
- Destructor is called when a delete operator is called.
How to call destructors explicitly?
Destructor can also be called explicitly for an object. We can call the destructors explicitly using the following statement:
C++
object_name.~class_name()
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems