C++ Program to Show the Use of Destructor

In C++, destructors are special member functions that are executed when an object of a class is destroyed. They are used to release resources that the object may have acquired during its lifetime. This article will guide you through the concept and implementation of destructors in C++ with detailed explanations and examples.

Prerequisites

Before diving into the implementation, ensure you have a basic understanding of the following concepts:

  1. Classes and Objects: Understanding of how classes and objects work in C++.
  2. Constructors: Familiarity with constructors and their purpose.
  3. Memory Management: Basic knowledge of dynamic memory allocation and deallocation.

1. Introduction to Destructors

1.1 Definition

A destructor is a member function with the same name as the class but preceded by a tilde (~). It has no return type and takes no parameters. Destructors are automatically invoked when an object goes out of scope or is explicitly deleted.

1.2 Purpose

Destructors are primarily used for:

  1. Releasing resources such as memory, file handles, or network connections that were acquired by the object.
  2. Performing any necessary cleanup before the object is destroyed.

2. Implementation of Destructors in C++

2.1 Example 1: Basic Destructor

Let’s start with a basic example to demonstrate the use of a destructor in C++.

C++
#include <iostream>
using namespace std;

class MyClass {
public:
    MyClass() {
        cout << "Constructor called." << endl;
    }

    ~MyClass() {
        cout << "Destructor called." << endl;
    }
};

int main() {
    MyClass obj;
    cout << "In main function." << endl;
    return 0;
}

Output:

C++
Constructor called.
In main function.
Destructor called.

Explanation:

  • The constructor is called when the object obj is created.
  • The destructor is called when the object obj goes out of scope at the end of the main function.

2.2 Example 2: Destructor with Dynamic Memory Allocation

In this example, we will use a destructor to release dynamically allocated memory.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int* data;
public:
    MyClass(int size) {
        data = new int[size];
        cout << "Constructor: Memory allocated." << endl;
    }

    ~MyClass() {
        delete[] data;
        cout << "Destructor: Memory deallocated." << endl;
    }
};

int main() {
    MyClass obj(10);
    cout << "In main function." << endl;
    return 0;
}

Output:

C++
Constructor: Memory allocated.
In main function.
Destructor: Memory deallocated.

Explanation:

  • The constructor allocates memory for an array of integers.
  • The destructor deallocates the memory when the object obj is destroyed, ensuring there are no memory leaks.

2.3 Example 3: Destructor in a Class with Multiple Objects

This example demonstrates the use of destructors in a class managing multiple objects.

C++
#include <iostream>
using namespace std;

class MyClass {
private:
    int id;
public:
    MyClass(int i) : id(i) {
        cout << "Constructor called for object " << id << endl;
    }

    ~MyClass() {
        cout << "Destructor called for object " << id << endl;
    }
};

int main() {
    MyClass obj1(1);
    MyClass obj2(2);
    MyClass obj3(3);
    cout << "In main function." << endl;
    return 0;
}

Output:

C++
Constructor called for object 1
Constructor called for object 2
Constructor called for object 3
In main function.
Destructor called for object 3
Destructor called for object 2
Destructor called for object 1

Explanation:

  • Constructors and destructors are called for each object (obj1, obj2, obj3).
  • Destructors are called in the reverse order of constructors, demonstrating the stack-like behavior of object destruction.

Conclusion

Destructors are essential for managing resources in C++. They ensure that resources such as memory and file handles are properly released when an object is destroyed. This article provided a comprehensive guide to the use of destructors in C++, demonstrating their application through basic examples, dynamic memory management, and multiple object management.