Destructors in C++

A destructor in C++ is a special member function of a class that gets invoked automatically when an object of that class goes out of scope or when it’s explicitly deleted. The primary purpose of a destructor is to clean up resources, such as memory, file handles, or network connections, that the object may have acquired during its lifetime.

In the below PDF we discuss about Destructors in detail in simple language, Hope this will help in better understanding.

Destructors have the following characteristics:

1.Same Name as the Class:
A destructor has the same name as the class, preceded by a tilde (~).

class MyClass {
// Constructor
MyClass() {
// Constructor logic

// Destructor
~MyClass() {
// Destructor logic

2.No Return Type or Parameters:
Destructors don’t return a value, and they take no parameters. They are automatically called when the object they belong to is destroyed.

3.Automatic Invocation:
You don’t need to explicitly call a destructor; it’s automatically invoked when an object is no longer needed.

The Role of Destructors:

Destructors play a crucial role in managing resources efficiently, especially in scenarios where an object acquires resources like dynamic memory allocation or file handles. Without proper cleanup, your program may suffer from memory leaks or resource leaks, which can lead to performance issues and instability.

1.Dynamic Memory Deallocation:

One of the most common use cases for destructors is to deallocate dynamic memory allocated using the new operator. If an object manages dynamic memory, its destructor should release this memory to prevent memory leaks. Here’s an example:

class DynamicMemoryClass {
int* dynamicArray;

DynamicMemoryClass() {
dynamicArray = new int[100];

~DynamicMemoryClass() {
delete[] dynamicArray;

In this example, the destructor ~DynamicMemoryClass() ensures that the dynamically allocated array is properly deallocated when an object of DynamicMemoryClass goes out of scope.

2.Resource Management:

Destructors can also be used to manage other resources, such as file handles, database connections, or network sockets. By releasing these resources in the destructor, you ensure that they are properly closed or released, even if an exception is thrown.

class FileHandler {
FILE* file;

FileHandler(const char* filename) {
file = fopen(filename, "w");
if (!file) {
throw std::runtime_error("Failed to open file");

~FileHandler() {
if (file) {

In this example, the destructor ~FileHandler() closes the file handle, ensuring that the file is properly closed when the FileHandler object is destroyed.

Related Question

A destructor in C++ is a special member function that is used to clean up or release resources held by an object when it goes out of scope or is explicitly deleted. It has the same name as the class but preceded by a tilde (~).

A destructor is automatically called when an object of a class is destroyed or goes out of scope. This typically happens when the object’s lifetime ends, such as when a local object leaves a function or when a dynamically allocated object is deleted using delete.

The primary purpose of a destructor is to release resources or perform cleanup operations that were allocated or initialized during the object’s lifetime. This can include freeing memory, closing files, releasing locks, or any other resource management tasks.

No, a class can have only one destructor. The destructor has the same name as the class and cannot have any parameters or overloads. However, you can define multiple constructors with different parameter lists.


Storage Classes in C++ In

Preprocessors in C++ A preprocessor

Standard Template Library in C++

Exception Handling in C++ Exception

Constructors in C++ A constructor

Inheritance in C++ Inheritance is

C++ Classes and Objects C++

Leave a Comment

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

// Sticky ads