• Articles
  • Tutorials
  • Interview Questions

What is Destructor in C++: A Detailed Guide

What is Destructor in C++: A Detailed Guide

How exactly does the destructor in C++ work, and when is it called? Read on and unlock the secrets of destructors, as we are going to find all the answers to your queries. 

Table of Contents

Watch the video below to understand C programming in detail:

Video Thumbnail

What is a Destructor in C++?

In C++, a destructor is a special member function that automatically calls when an object goes out of scope or is explicitly deleted using the “delete” keyword. Its primary purpose is to clean up resources or perform any necessary cleanup operations before an object is destroyed and its memory is deallocated.

Destructor has the same name as the class, preceded by a tilde (~) sign. They are essential for managing dynamically allocated memory, closing file handles, releasing allocated resources like memory, or performing other cleanup tasks needed to prevent memory leaks or resource leaks.

Destructors are particularly crucial in cases where classes manage non-memory resources, such as file handles, network connections, or database connections, ensuring proper cleanup to maintain the system’s stability and efficiency. 

Do you want to jumpstart your career in computer programming? Enroll in our C Programming Course and gain the skills to succeed!

Syntax for Writing Destructor

The syntax for writing a destructor is straightforward. It follows a specific naming convention: it has the same name as that of the class with a tilde (~) sign. Here’s an example:

Class MyClass {
Public:
        //constructor
MyClass() {
     //constructor code
}
        //other member function
       //destructor
~MyClass() {
       //dect code
  }
};

The destructor doesn’t take any parameters, and you write the necessary cleanup code within the destructor block. It is automatically called when an object of the class goes out of scope or is explicitly deleted using the “delete” keyword. 

Check out the Top 20 C++ Project Ideas

Get 100% Hike!

Master Most in Demand Skills Now!

Examples of Destructors

a). Refer to the code below, which displays the execution of the constructor and destructor:

#include <iostream>
using namespace std;
class MyClass 
{
public:
    // User-Defined Constructor
    MyClass() 
    { 
        cout << "\n Execution of Constructor "; 
    }
    // User-Defined Destructor
    ~MyClass() 
    {
        cout << "\n Execution of Destructor ";
    }
};
main()
{
    MyClass t;
    return 0;
}  

Output:

Execution of Constructor 

Execution of Destructor 

b). Refer to the code below, which displays the execution of the destructor with multiple object creations:

#include <iostream>
using namespace std;
class TestCase {
public:
        //   Constructor
    TestCase() { cout << "\n Constructor is executed"; }
    // Destructor
    ~TestCase() { cout << "\n Destructor is executed"; }
};
main()
{
    // Creating multiple objects of the TestCase class
    TestCase t, t1, t2;
    return 0;
}

Output:

Constructor is executed

Constructor is executed

Constructor is executed

Destructor is executed

Destructor is executed

Destructor is executed

When is a Destructor Called?

  • Automatic Destruction: When an object goes out of scope, the destructor is automatically invoked. This happens at the end of a block/function where the object was created.
  • Manual Invocation: Destructors can also be explicitly called by the programmer using the delete keyword for dynamically allocated objects (created using new).
  • Inheritance Hierarchy: In a class hierarchy, destructors are called in a specific order, from the most derived class to the base class, ensuring proper cleanup of resources allocated in each class.
  • Exceptions and Stack Unwinding: If an exception is thrown and objects are on the stack, the destructors of those objects in the current scope will be called during stack unwinding to handle exceptions.
  • Resource Cleanup: Destructors are crucial for releasing resources held by an object, such as closing files, freeing memory, or releasing other acquired resources, ensuring proper cleanup to prevent memory leaks and resource exhaustion.

Check out the top C++ Interview Questions to ace your next interview!

Properties of Destructors

Destructors are vital for resource management and play a critical role in ensuring proper cleanup and preventing memory leaks in object-oriented programming. Here are some key properties:

  • Same Name as Class: Destructors have the same name as the class but are preceded by a tilde (~). For instance, the destructor for a class named Example would be ~Example().
  • No Return Type or Parameters: Destructors don’t have a return type or accept any parameters. They are invoked implicitly or explicitly and cannot be overloaded with different parameter lists.
  • Automatic Invocation: They are automatically called when the object goes out of scope or is explicitly deleted. This ensures proper cleanup of resources held by the object.
  • Release of Resources: Destructors are used to release resources acquired by the object during its lifetime, such as deallocating memory, closing files, releasing locks, or freeing any other resources.
  • Order of Execution: In a class hierarchy, destructors are called in reverse order of constructors, starting from the most derived class down to the base class. This ensures that resources are properly released in the reverse order of acquisition.
  • Implicitly Defined: If a class doesn’t explicitly define a destructor, the compiler generates a default destructor, which does nothing explicitly but still ensures that the resources held by the object are properly cleaned up.
  • Virtual Destructors: In scenarios involving inheritance and polymorphism, a base class should have a virtual destructor to ensure that when deleting a derived class object through a base class pointer, the destructor of the derived class is properly called to avoid memory leaks.

Wrap-Up

In conclusion, understanding the significance of destructors in C++ is crucial for writing robust and efficient code. They act as a removal team, releasing resources and guaranteeing memory management when objects go outside of scope. Mastering the art of crafting effective destructors empowers developers to prevent memory leaks, manage dynamic memory allocation, and maintain code integrity. Remember, while they may seem like a small aspect of programming, their impact on resource management and code reliability is immense. Embracing the power of destructors contributes significantly to writing cleaner, safer, and more scalable C++ programs.

FAQs

What is the purpose of a C++ destructor?

The main objective of the destructor is to release any resources (opened files, opened sockets, database connections, resource locks, etc.) allocated throughout the lifetime of your object.

When we write our constructor, does the C++ compiler create a default constructor?

In C++, the compiler builds a default constructor for each class by default. However, if we define our constructor, the compiler will not generate the default constructor.

In C++, what is the sequence of destructor execution?

In general, derived class destructors are followed by base class destructors. Except when we copy a derived class object into a base-class pointer (or reference variable) and neglect to include the virtual keyword for the base class destructor. 

Can virtual destructors exist? If that's the case, what's the point of virtual destructors?

We can create a virtual destructor. This ensures that the right class destructor is called during runtime, specifically when we utilize a pointer or reference from the base class to hold the derived class object. If we don’t have a virtual destructor, it will simply call the base class destructor.

How do destructors differ from regular member functions?

The destructors’ names must be the same as the class name prefixed by a tilde (~). Destructors also do not take any arguments and do not return anything.

What exactly is a virtual destructor? Describe its application in C++.

If the destructor in the base class is not made virtual, an object of type base class and an instance of the child class would simply call the base class destructor rather than the derived class destructor. As a result, by making the base class destructor virtual, we ensure that the derived class destructor is called before the base class destructor.

Course Schedule

Name Date Details
Python Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.