Cpp Tutorial: Virtual destructor and Pure virtual destructor in C++

Need for virtual destructor.

When you delete a derived class object by using a base class pointer, the destructor call will be an undefined behavior.

For example:

In the below code, we are creating an object of derived class and assigning to base class pointer. Now when use “delete” operator, check the output:

#include <iostream>       // std::cout

// for more tutorials visit www.ProDeveloperTutorial.com

using namespace std;

class Base 
{ 
  public: 
    Base()      
    { 
        cout<<"Constructor Base \n"; 
    } 
    ~Base() 
    { 
        cout<<"Destructor Base \n"; 
    }      
}; 
  
class Derived: public Base 
{ 
  public: 
    Derived()      
    { 
        cout<<"Constructor Derived \n"; 
    } 
    
    ~Derived() 
    { 
        cout<<"Destructor Derived \n"; 
    } 
}; 
  
int main(void) 
{ 

  // create a derived class object
  Derived *d = new Derived();   
  
  //assign it to base class pointer
  Base *b = d; 
  
  //delete derived class object
  delete b; 

  return 0; 
} 

Output:

Constructor Base
Constructor Derived
Destructor Base

As you can see from the above output, derived class destructor is not called. Thus, it is important to make destructor to be virtual.

Making destructor as virtual.

#include <iostream>       // std::cout

// for more tutorials visit www.ProDeveloperTutorial.com

using namespace std;

class Base 
{ 
  public: 
    Base()      
    { 
        cout<<"Constructor Base \n"; 
    } 
    virtual ~Base() 
    { 
        cout<<"Destructor Base \n"; 
    }      
}; 
  
class Derived: public Base 
{ 
  public: 
    Derived()      
    { 
        cout<<"Constructor Derived \n"; 
    } 
    
    ~Derived() 
    { 
        cout<<"Destructor Derived \n"; 
    } 
}; 
  
int main(void) 
{ 

  // create a derived class object
  Derived *d = new Derived();   
  
  //assign it to base class pointer
  Base *b = d; 
  
  //delete derived class object
  delete b; 

  return 0; 
} 

Output:

Constructor Base
Constructor Derived
Destructor Derived
Destructor Base

Pure virtual destructor in C++

Destructor can be declared as pure virtual.

Once the destructor is made as pure virtual, then the destructor body needs to be provided.

This is because, for destructor, they will not be overridden in derived class, but they will be called in the reverse order.

Hence you need to provide destructor body for pure virtual destructor.

Program for pure virtual destructor:

#include <iostream>       // std::cout

// for more tutorials visit www.ProDeveloperTutorial.com

using namespace std;

class Base 
{ 
  public: 
    Base()      
    { 
        cout<<"Constructor Base \n"; 
    } 
    virtual ~Base() = 0; // pure virtual destructor 
   
}; 

Base::~Base() 
{ 
    cout<<"Destructor Base \n"; 
}        


class Derived: public Base 
{ 
  public: 
    Derived()      
    { 
        cout<<"Constructor Derived \n"; 
    } 
    
    ~Derived() 
    { 
        cout<<"Destructor Derived \n"; 
    } 
}; 
  
int main(void) 
{ 

  // create a derived class object
  Derived *d = new Derived();   
  
  //assign it to base class pointer
  Base *b = d; 
  
  //delete derived class object
  delete b; 

  return 0; 
} 

Output:

Constructor Base 
Constructor Derived 
Destructor Derived 
Destructor Base
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *