Javatpoint Logo
Javatpoint Logo

Characteristics of Destructor in C++

Introduction:

In C++, a Destructor is a special member function of a class used to perform cleanup activities when an object of the class goes out of scope, is deleted, or is destroyed. A Destructor is invoked automatically by the compiler when an object is no longer needed, and its purpose is to deallocate any resources that were allocated by the constructor. In this article, we will see the characteristics of a Destructor in C++.

Syntax of a Destructor:

A Destructor is identified by the tilde (~) symbol followed by the class name. Its syntax is as follows:

C++ Code:

Characteristics of a Destructor in C++

Name and Return Type:

As mentioned earlier, the name of the Destructor function is always the same as the class name preceded by a tilde (~). The destructor has no return type and no parameters. The Destructor is automatically invoked when the object goes out of scope, and its main function is to clean up the object before it is destroyed.

Default Destructor:

The compiler supplies a Default Destructor for a class if it lacks a user-defined Destructor. The Default Destructor has an empty body and does not do anything. However, if a class has dynamic memory allocation or other resources that need to be cleaned up, then it is necessary to define a Destructor explicitly.

Implicit Call:

A destructor is implicitly called when an object goes out of scope, when a delete operator is applied to an object, or when the program terminates. The order of destructors is opposite to the order of the constructors.

Non-virtual Destructors:

A Non-virtual destructor is used for a class that is not intended to be used as a base class. If a derived class is deleted through a pointer to the base class, and the Destructor of the parent or base class is not virtual, then only the Destructor of the base class will be called, and the Destructor of the derived class will not be called. Therefore, it is always a good practice to make the Destructor virtual in a base class.

Virtual Destructors:

A class that is meant to be used as a base or child class uses a Virtual Destructor. The Destructor of the derived class will also be called if a derived class is removed using a pointer to the base class and the Virtual Destructor of the base class. The resources allotted by the derived class are duly deallocated as a result. Remember that if a class is meant to be used as a parent or base class, the Destructor must always be virtual.

Dynamic Memory Allocation:

If a class has dynamically allocated memory or resources that need to be cleaned up when the object is destroyed, then the Destructor must deallocate the memory and resources. If the Destructor does not deallocate the memory, then the memory will be leaked, and the program will run out of memory.

Exceptions:

A Destructor should not throw any exceptions because if an exception is thrown or given from a Destructor, then the program terminates immediately without cleaning up any resources. Therefore, it is always better to catch any exceptions inside the destructor and handle them properly.

Inheritance:

If a class inherits from a parent or base class, then the destructor of the base class is automatically called before the Destructor of the derived class. If the Destructor of the derived class needs to perform any additional cleanup activities, then it can do so after the base class Destructor has been called.

Multiple Destructors:

If a class has multiple destructors, then only one Destructor will be called. The Destructor that is called depends on how the object was created. If the object was created with a default constructor, then the default Destructor will be called. If the object was created with a user-defined constructor, then the user-defined Destructor will be called.

Access Specifiers:

Like any other member function, a Destructor can have an access specifier. It can be present in the category of public, private, or protected. If the Destructor is declared as private, then only the class members can access it. This can be useful in implementing a Singleton class, where only one instance of the class can be created.

Pure Virtual Destructors:

A Pure Virtual Destructor is a Destructor that has no implementation in the base class but is declared as virtual. A class that has a pure Virtual Destructor is known as an abstract class, and it cannot be instantiated. A pure Virtual Destructor is useful when a class has one or more pure virtual functions and needs to ensure that its derived classes have proper destructors.

Conclusion:

In conclusion, a Destructor is a special member function of a class in C++ that is used to clean up any resources that were allocated by the constructor when an object is no longer needed. A Destructor is invoked automatically by the compiler when an object is destroyed, and its main function is to deallocate any resources that were allocated by the constructor. A Destructor must be declared as virtual if a class is intended to be used as a base class, and it should not throw any exceptions. In addition, a Destructor can have an access specifier, and a class with a pure Virtual Destructor is known as an Abstract class. Understanding the characteristics of a Destructor is essential for writing efficient and robust C++ code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA