C++ Destructors

Similarly to a constructor, A Destructor is a special kind of member function that is automatically called when an object about to destroy. Destructors are usually used to deallocate memory and do another 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 className {
   public:
     className();   // Constructor
     ~className()   // Destructor
};

Characteristics of Destructors

If no user-defined destructor exists for a class then compiler implicitly declares a destructor. This implicitly-declared destructor is an inline public member of its class.

The compiler will implicitly define an implicitly declared destructor when the compiler uses the destructor to destroy an object of the destructor’s class type. Suppose if class A has an implicitly declared destructor. The following is equivalent to the function the compiler would implicitly define for A: A::~A() { }

Example

/* C++ program to demonstrate the working wit destructor*/ 
#include <iostream> 
#include <string.h> 
using namespace std; 
class student 
{ 
    private: 
        int rno; 
        char* name; 
        unsigned marks; 
        public: 
        student(); //constructor prototype 
        ~student(); //destructor prototype 
        void setData(int tempNo, char*tempName, unsigned teampMarks); 
        void display(); 
}; 
student::student() //constructor definition 
{ 
    cout<<"Constructor Executed"<<endl; 
    rno = 0; 
    name = new char[30]; //dynamically allocating 
    marks = 0; 
} 
student::~student() //destructor definition 
{ 
    delete[] name; 
    cout<<"Destructor Executed"<<endl; 
}
void student::setData(int tempNo, char*tempName, unsigned tempMarks) 
{ 
    rno = tempNo; 
    strcpy(name, tempName); 
    marks = tempMarks; 
} 
void student::display() 
{ 
    cout<<"Roll No = "<<rno<<endl; 
    cout<<"Name = "<<name<<endl; 
    cout<<"Marks = "<<marks<<endl; 
} 
int main() 
{ 
    student s1; //constructor called here 
    s1.setData(111, "balututorial.com",450); 
    s1.display(); //destructor called here before return statement 
    return 0;
}

[quote]

Output:

Constructor Executed                                                                                                                                         
Roll No = 111                                                                                                                                                
Name = balututorial.com                                                                                                                                      
Marks = 450                                                                                                                                                  
Destructor Executed

[/quote]

We can use a destructor explicitly to destroy objects, although this practice is not recommended. However, to destroy an object created with the placement new operator, you can explicitly call the object’s destructor. The following example demonstrates this:

#include <iostream> 
using namespace std; 
class MyClass 
{ 
    public: 
        MyClass() { 
            cout<< "MyClass::MyClass()" << endl; 
        } 
        ~MyClass() 
        { 
            cout <<"MyClass::~MyClass()" << endl; 
        } 
}; 
int main () 
{ 
    MyClass* obj = new MyClass; 
    delete obj; 
    return 0; 
}

[quote]

Output:

MyClass::MyClass()                                                                                                                                           
MyClass::~MyClass() 

[/quote]

The statement MyClass* obj = new MyClass; A dynamically creates a new object of type MyClass not in the free store but in the memory allocated by obj. The statement delete obj; will delete the storage allocated by obj, but the run time will still believe that the object pointed to by obj still exists until you explicitly call the destructor.