Pure virtual destructor in C++

C++Server Side ProgrammingProgramming

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
raja
Published on 03-Apr-2019 14:24:57
Advertisements