Constructor Functions  «Prev  Next»
Lesson 4 What is a destructor?
Objective Explore the use of destructor member functions.

Destructor Member Functions in C++

Question: What is the purpose and function of destructor member functions in C++?
A destructor in C++ is a special member function of a class that is executed whenever an object of the class goes out of scope or is explicitly deleted. It is used to perform "housekeeping" tasks such as releasing memory, closing files, or disconnecting from network resources.
The primary role of a destructor is to ensure efficient management of resources and to prevent memory leaks. In a language like C++, where the programmer has direct control over memory allocation, it is important to carefully manage resources to maintain performance and prevent issues like memory overflows.
Destructors are defined using the same name as the class but are preceded by a tilde (~). For example, if we have a class named MyClass, its destructor would be declared as ~MyClass().
Unlike constructors, which can be overloaded to provide different ways to initialize objects, each class can have only one destructor, and it cannot take arguments because you cannot overload destructors.
Here is an example of a destructor:
class MyClass {
public:
    // Constructor
    MyClass() {
        // Initialization code
    }

    // Destructor
    ~MyClass() {
        // Cleanup code
    }
};

In the destructor definition (~MyClass()), we could put any code necessary to clean up after an object of type MyClass.
It's important to note that if you don't provide a destructor, the C++ compiler creates a default destructor for you. However, this default destructor only performs basic tasks and won't handle freeing up dynamic memory or releasing other resources, hence the need to often define your own destructors in complex classes.
In conclusion, destructors are a critical part of C++ that allow programmers to manage resources effectively and maintain the health and performance of their applications.

Destructor Member Function Definition

A destructor is a member function whose name is the class name preceded by the tilde character (~). A destructor's usual purpose is to destroy values of the class type, typically by using delete.

Function of Destructors

  1. are invoked implicitly when an object goes out of scope. This is typically at block exit for automatic objects and program exit for static objects.
  2. cannot be overloaded or take arguments.

C++ Destructors

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor is a member function with the same name as its class prefixed by a ~ (tilde). For example:

class X {
public:
  // Constructor for class X
  X();
  // Destructor for class X
  ~X();
};

A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const, volatile, const volatile or static. A destructor can be declared virtual or pure virtual. If no user-defined destructor exists for a class and one is needed, the compiler implicitly declares a destructor.
This implicitly declared destructor is an inline public member of its class.

Destructor is the complement of the Constructor

The complement of the constructor is the destructor. In many circumstances, an object will need to perform some action or actions when it is destroyed. Local objects are created when their block is entered, and destroyed when the block is left. Global objects are destroyed when the program terminates. When an object is destroyed, its destructor (if it has one) is automatically called. There are many reasons why a destructor may be needed. For example, an object may need to deallocate memory that it had previously allocated or it may need to close a file that it had opened. In C++, it is the destructor that handles deactivation events. The destructor has the same name as the constructor, but it is preceded by a ~. For example, here is the stack class and its constructor and destructor.
(Keep in mind that the stack class does not require a destructor; the one shown here is just for illustration.)

// This creates the class stack.
class stack {
 int stck[SIZE];
 int tos; 
 public:
  stack(); // constructor
  ~stack(); // destructor
  void push(int i);
    int pop();
  };
// stack's constructor
stack::stack()
{
 tos = 0;
 cout <<  "Stack Initialized\n";
}
// stack's destructor
stack::~stack()
{
 cout << "Stack Destroyed\n";
}