Destructor in C++
Introduction
A destructor is a special member function of a class that is automatically called when an object is destroyed.
The main purpose of a destructor is to release resources used by an object, such as dynamically allocated memory, files, or other resources.
Syntax
A destructor has the same name as the class, preceded by a tilde (~).
class ClassName { public: ~ClassName() { // Destructor body } };
Important Features
- A destructor has the same name as the class.
- It starts with a tilde (
~). - It has no return type.
- It takes no parameters.
- A class can have only one destructor.
- It is automatically called when an object is destroyed.
- It is commonly used for resource cleanup.
1. Simple Destructor Example
#include <iostream> using namespace std; class Student { public: Student() { cout << "Constructor called" << endl; } ~Student() { cout << "Destructor called" << endl; } }; int main() { Student s1; return 0; }
Output
Constructor called Destructor called
The constructor is called when s1 is created, and the destructor is called when s1 is destroyed.
2. Destructor with a Block
An object created inside a block is destroyed when the block ends.
Example
#include <iostream> using namespace std; class Demo { public: Demo() { cout << "Object created" << endl; } ~Demo() { cout << "Object destroyed" << endl; } }; int main() { { Demo d1; } cout << "Program continues" << endl; return 0; }
Output
Object created Object destroyed Program continues
The destructor is called when d1 goes out of scope.
3. Destructor and Dynamic Memory
Destructors can be useful for releasing dynamically allocated memory.
Example
#include <iostream> using namespace std; class Array { private: int* data; public: Array() { data = new int[5]; cout << "Memory allocated" << endl; } ~Array() { delete[] data; cout << "Memory released" << endl; } }; int main() { Array a1; return 0; }
Output
Memory allocated Memory released
Here:
new int[5]allocates memory.delete[] datareleases the memory.- The destructor performs the cleanup automatically.
4. Multiple Objects and Destructor
When multiple objects are created in the same scope, their destructors are generally called in the reverse order of construction.
Example
#include <iostream> using namespace std; class Demo { public: int id; Demo(int i) { id = i; cout << "Constructor: " << id << endl; } ~Demo() { cout << "Destructor: " << id << endl; } }; int main() { Demo d1(1); Demo d2(2); Demo d3(3); return 0; }
Output
Constructor: 1 Constructor: 2 Constructor: 3 Destructor: 3 Destructor: 2 Destructor: 1
The objects are destroyed in reverse order.
5. Constructor vs Destructor
| Constructor | Destructor |
|---|---|
| Initializes an object | Cleans up an object |
| Same name as the class | Same name with ~ |
| Called when object is created | Called when object is destroyed |
| Can have parameters | Cannot have parameters |
| Can be overloaded | Cannot be overloaded |
| No return type | No return type |
| Used for initialization | Used for resource cleanup |
6. When is a Destructor Called?
A destructor is automatically called when:
- A local object goes out of scope.
- A function containing the object finishes.
- The program reaches the end of
main()for local objects. - A dynamically allocated object is explicitly destroyed using
delete.
Example
#include <iostream> using namespace std; class Demo { public: ~Demo() { cout << "Destructor called" << endl; } }; int main() { Demo d1; cout << "Inside main" << endl; return 0; }
The destructor is automatically called before the program exits.
7. Virtual Destructor
When a class is used as a base class and objects may be deleted through a base-class pointer, the base class should generally have a virtual destructor.
Example
#include <iostream> using namespace std; class Animal { public: virtual ~Animal() { cout << "Animal destructor" << endl; } }; class Dog : public Animal { public: ~Dog() { cout << "Dog destructor" << endl; } }; int main() { Animal* a = new Dog(); delete a; return 0; }
Output
Dog destructor Animal destructor
The virtual destructor ensures that the derived-class destructor is also called when deleting through a base-class pointer.
8. Real-Life Example
Consider a File class.
When an object is created:
File object created ↓ File opened ↓ Program uses file ↓ Object destroyed ↓ File closed
A destructor can automatically perform the cleanup when the object is destroyed.
Advantages of Destructors
- Automatically performs cleanup.
- Helps release dynamically allocated memory.
- Helps close files and other resources.
- Reduces resource leaks.
- Makes resource management easier.
Important Points
- Destructor starts with
~. - It has the same name as the class.
- It has no return type.
- It cannot accept parameters.
- It cannot be overloaded.
- It is called automatically when an object is destroyed.
- It is mainly used for cleanup and resource management.
- A base class should often have a virtual destructor when used polymorphically.
Summary
Object Created ↓ Constructor Called ↓ Object Used ↓ Object Goes Out of Scope ↓ Destructor Called ↓ Resources Released
Simple Example
class Student { public: Student() { cout << "Constructor"; } ~Student() { cout << "Destructor"; } };
Constructor → creates/initializes the object
Destructor → cleans up when the object is destroyed