Updated April 21, 2023
Introduction to Destructor in C++
As everyone knows, IT industries widely use C++ – an object-oriented programming language – for developing software, drivers, embedded firmware, and client-server applications. It is a middle-level language encapsulating features of both high-level and low-level language. It has a concept of classes and objects. In C++, the constructors play an essential role in creating an object and initializing the instance variables, but what happens to these objects and resources after they are no longer in use or the program ends? Destructors in C++ destroy objects created by Constructors when they no longer need to release memory.
C++ automatically calls these special member functions. Compiler to free up the memory when there is no user-defined destructor in the program. Like Constructors, destructors have the same name as class preceded by a tilde (~) sign. But the Destructor does not accept any arguments/parameters and does not return anything, i.e., they do not have any return type.
You cannot overload Destructors like Constructors in a class. Below is a list of certain situations in which the compiler automatically calls the Destructor.
- When you call a delete operator.
- The compiler calls destructors when the program execution finishes.
- When the block/scope of local variables ends.
A programmer can define a destructor known as a user-designed constructor. You can declare a destructor as virtual or purely virtual, but you cannot declare it as const, volatile, const volatile, or static.
How does Destructor work in C++?
For working on the Destructor, the below-mentioned points need to be kept in mind:
- When calling destructors of class objects, the compiler calls the destructors of the class objects first before calling the Destructor of members and bases. The compiler also calls the destructors of non-virtual base classes before calling the destructors of virtual base classes.
- The compiler calls the Destructor of non-static members before calling the Destructor of base classes. Additionally, the compiler calls the destructors of virtual and non-virtual base classes in reverse order of their declaration.
- The C++ compiler automatically calls implicit destructors when an object goes out of scope or when program execution terminates for external and static objects in a program. The compiler calls destructors to destroy things created using the new keyword.
- In the case of implicit and user-defined destructors, the compiler executes the destructor body, calls destructors of all non-static non-variant classes, and then calls the destructors of non-virtual and virtual base classes in the reverse order of their construction.
Syntax of Destructor
Destructors in C++ are preceded by the tilde(~) sign. Below mentioned is the basic syntax of Destructor:
Syntax:
class class_name()
{
…
…
public:
class_name(); // Constructor
~class_name(); // destructor
}
}
In the syntax, you name the class “class_name,” In the primary method, you define both the constructor and Destructor with the same name as the class. Destructor neither has any parameters nor return type.
Examples of Destructor in C++
Below mention is an example of a Destructor in C++:
Example #1: With User-Defined Destructor
Code:
#include <iostream>
using namespace std;
class Example1{ //class
public:
Example1(){ // constructor cout << "Hello I am inside a constructor" << endl;
}
~Example1(){ //destructor
cout << "Hello I am inside a destructor" << endl;
}
void display()
{
cout << "Hello this is a display method" << endl
}
};
int main()
{
Example1 ex; //object created
ex.display(); // display method called return 0;
}
Output:
In the above example, you name the class “Example1” and create an object “ex” for that class. The program first calls the constructor, which prints the text. After that, the program calls the display method, and once the program finishes execution, it calls the Destructor.
Example #2: With Virtual Destructor
Code:
#include<iostream>
using namespace std;
class b1 { //base class
public:
b1() // constructor of base class
{
cout << "Hello this is base constructor" << endl;
}
~virtual b1() // destructor of base class
{
cout << "Hello this is base destructor" << endl;
}
};
class b2: public b1{ // derived class of base class (b1) public:
b2() //constructor of derived cass
{
cout << "Hello this is derived constructor" << endl;
}
~ b2() // destructor of derived class
{
cout << "Hello this is derived destructor" << endl;
}
};
int main(void) //main method
{
b2 *der = new b2();
b1 *bas = der;
delete der;
getchar();
return 0;
}
Output:
Making the base class’s Destructor virtual is an excellent practice, ensuring that the derived class’s object is appropriately destroyed. Whenever you use a virtual type, you should immediately add a virtual destructor to prevent future unexpected results.
Advantages of Destructor in C++
- It gives a final chance to clean up the resources that are not in use to release the memory occupied by unused objects, like deleting dynamic objects and closing the system handles and used files.
- Because computers often have many unused resources taking up space, using a destructor can reduce the chances of memory leaks by destroying those new resources.
- Though C++ does have the mechanism of Garbage Collection but calling the Destructor automatically, whether the programmer calls it or not, to free up space prevents the user from many worst situations in the future.
Points to Summaries about Destructor
- You use destructors to destroy the unused resources of a class.
- Destructors have the same name as the class name preceding the (~) sign.
- Unlike Constructors, there can be no parameter of the Destructor.
- There is no return type of Destructor.
- If the user does not define a destructor in a program, the compiler automatically constructs one Destructor for it.
- There cannot be more than one Destructor in a single class.
Conclusion
The above description of destructors clearly defines the use and implementation of destructors in the C++ program. Though the concept of the Destructor is not complex, it is essential to understand it before implementation in the program, as improper use of the Destructor can lead to some unexpected results.
Recommended Article
This is a guide to Destructor in C++. Here we discuss Introduction to Destructor in C++ and the Working of Destructor along with its Advantages. You can also go through our other suggested articles to learn more–