Destructor Concepts in C++

Destructor Concepts in C++

In the C++ programming language, a destructor is a special parameterized member function of a class called data. It is invoked in a user-defined program when an object of a class in the current class needs to be destroyed. A special use case for the destructor method in a C++ program is to release or free class resources acquired during the lifetime of an object in the class. For example, the destructor method in an existing class is used to manage memory for dynamically allocated program variables or for other program resource management tasks such as file handling, network connection establishment, etc. In most C++ programs, the destructor method is used to cleanly free or deallocate existing system resources.

Destructor Concepts in C++

Special Features of the Destructor Method in C++ Programming

Automatic Destructor Invocation.

When an object in a user-defined class goes out of scope. (for local parameter objects declared in the class) or when it is deleted in explicit order. For example, (for class objects dynamically allocated in an existing class), the destructor method is automatically called in this process.

The destruction method defined in a class executes after the last statement in the parameter object’s scope is executed.

No arguments.

The destructor in a user-defined class has no parameter variables and can never be overloaded. A C++ user can only define one destructor method per class.

No return value.

The destructor in a user-defined class does not return any object value, meaning it has no value (no return data type).

Cannot be called explicitly.

The destructor method in a user-defined class cannot be called in explicit order like a regular function. When an object declared in the current class is automatically destroyed, it is automatically invoked.

Invoked in reverse order of construction.

In a user-defined class, the destructor method is called in the opposite order of the constructor method. For example, if a new object is created in a class function, the destructor method will be called when the current program function returns and the object goes out of scope.

Cannot be inherited or overloaded.

Since destructor methods automatically inherit object-derived class properties, they cannot be overloaded like constructor methods in existing classes.

Basic C++ program destructor method syntax.

Here, you must declare the destructor method defined in your desired class with the same name as the class. However, you can define it with a tilde (~) symbol prefix.

~ClassName() {

// Cleanup code for the destructor method declared in the class

}

Example of a destructor in C++.

  • So, let’s understand how destructor methods work in C++ programming with an example.

Example of a destructor method for resource management cleanup.

#include <iostream>

#include <string>

class Course {

public:

std::string course_name;

int c_price;

// Here we declare a constructor method

Course(std::string cn, int cp) : course_name(cn), c_price(cp) {

std::cout << “Course class object created – ” << course_name << std::endl;

}

// here we declare a Destructor method with ~ prefix symbol

~Course() {

std::cout << “Course class object destroyed – ” << course_name << std::endl;

}

void preview() {

std::cout << “The Course Name – ” << course_name << “, The Course Price – ” << c_price << std::endl;

}

};

int main() {

{

//here we are creating a class object in a scope

Course course(“Java programming”, 3999);

course.preview(); // Result – The Course Name – Java programming, The Course Price – 3999

} // here course goes out of scope, and its destructor method is automatically called

return 0;

}

Explanation of a destructor method for resource management cleanup.

  • Here in this example, a constructor method is defined for the Course class, which initializes the course name and course price class objects, and a destructor method is defined in it, which prints a message when the object in the current class is destroyed.
  • In this, in the main function, when the course object class goes out of scope (at the end of the inner block), its destructor method is automatically called in the current program, thereby automatically releasing the resources attached to these objects.

Destructor Method in Inheritance.

In a C++ program, destructor methods declared in a C++ class are inherited by subclasses, but this inheritance concept has its own specific rules and regulations.

Base Class Destructor in C++.

If a user-defined class is inherited from a base class, the base class’s destructor is called first, followed by the derived class’s destructor.

Virtual Destructor in C++.

If a C++ user wishes to delete a class object parameter that is passed through a pointer to a base class, they must declare the base class’s destructor as virtual. This ensures that the destructor of the derived class defined in the current class is called first, followed by the base class’s destructor method, preventing resource leaks in the current class.

Example of a destructor (virtual destructor) method in inheritance.

#include <iostream>

class BaseClass {

Public:

// here we create a Constructor method

BaseClass() {

std::cout << “BaseClass class constructor method define \n”;

}

// here we create a Virtual destructor method

virtual ~BaseClass() {

std::cout << “BaseClass class destructor method define \n”;

}

};

class DerivedClass : public BaseClass {

Public:

//here we ceate a Constructor class method

DerivedClass() {

std::cout << “DerivedClass class constructor method define \n”;

}

// here we create a Destructor class method

~DerivedClass() {

std::cout << “DerivedClass class destructor method define \n”;

}

};

int main() {

BaseClass* classobj = new DerivedClass(); // Here we are creating a Derived class object but storing it in a BaseClass pointer

delete classobj; // Here we are properly calling the Derived destructor followed by the BaseClass destructor

return 0;

}

Explanation of a destructor (virtual destructor) method in inheritance.

  • In this example, BaseClass has a user-defined virtual destructor method, which ensures that in the current program, when delete classobj is called in the current class (in which classobj is a pointer to the BaseClass), the destructor of the DerivedClass class is called first. After that, the destructor of the BaseClass class is called. This process properly clears or fixes the resources used in both BaseClass and DerivedClass classes.

Leave a Reply