Test non-virtual and virtual destructors - C++ Class

C++ examples for Class:Virtual Function

Description

Test non-virtual and virtual destructors

Demo Code

#include <iostream>
using namespace std;
class Base/* w ww.j a  va 2s.  co  m*/
{
   public:
   ~Base()                          //non-virtual destructor
   //    virtual ~Base()                  //virtual destructor
   { cout << "Base destroyed\n"; }
};
class Derv : public Base
{
   public:
   ~Derv()
   { cout << "Derv destroyed\n"; }
};
int main()
{
   Base* pBase = new Derv;
   delete pBase;
   return 0;
}

Result


Related Tutorials