A destructor is a special member function, prefixed with '~', that is automatically called when an object goes out of scope or is destroyed to free resources like memory, files, or connections.
- Destructor neither requires any argument nor returns any value.
- A destructor cannot be declared as static. It also cannot be const-qualified because a destructor is meant to destroy the object, which inherently involves modifying its state.
- A class can have only one destructor, and it cannot be overloaded.
- Destructors release resources and destroy objects in reverse order of creation.
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() {
cout << "Constructor Called"
<< endl;
}
// User-Defined Destructor
~Test() {
cout << "Destructor Called"
<< endl;
}
};
int main() {
Test t;
return 0;
}
Output
Constructor Called Destructor Called
Explanation: This program demonstrates that the constructor is called when an object is created and the destructor is called automatically when the object is destroyed.
Syntax
Destructors are automatically present in every C++ class but we can also redefine them using the following syntax.
~className(){
// Body of destructor
}
where, tilda(~) is used to create destructor of a className.
When do we need to write a user-defined destructor?
- If we don’t write a destructor, the compiler provides a default one.
- The default destructor works fine for classes without dynamic memory or pointers.
- If a class has pointers or dynamically allocated memory, we must write a destructor.
- A user-defined destructor releases memory or other resources before the object is destroyed.
- Writing a destructor in such cases prevents memory leaks.
#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;
}
Output
10 Destructor: Memory deallocated
Note: When the object is destroyed, the destructor releases the dynamically allocated resources, which in this case is the pointer.
When is the destructor called?
- When the function ends.
- When the program ends.
- When when a block containing local variables ends.
- When a delete operator is called.
Program to demonstrates the number of times constructors and destructors are called.
#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
How to call destructors explicitly?
Destructor can also be called explicitly for an object. We can call the destructors explicitly using the following statement:
object_name.~class_name()