Pure virtual destructor in C++


The pure virtual destructor is possible in C++. If a class contains pure virtual destructor it is must to provide a function body for the pure virtual destructor.

Example Code

 Live Demo

#include <iostream>
using namespace std;

class B {
   public:
   virtual ~B()=0; // Pure virtual destructor
};

B::~B() {
   std::cout << "Pure virtual destructor is called";
}

class D : public B {
   public:
   ~D() {
      cout << "~D() is executed"<<endl;
   }
};

int main() {
   B *bptr=new D();
   delete bptr;
   return 0;
}

Output

~D() is executed
Pure virtual destructor is called

Updated on: 30-Jul-2019

421 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements