Tuesday, April 23, 2013

Virtual Destructor in C++

#include iostream.h
class Base
{
    public:
      Base(){ cout<<"Constructing Base";}
     
     // this is a destructor:
virtual ~Base(){ cout<<"Destroying Base";}
};

class Derive: public Base
{
        public:
        Derive(){ cout<<"Constructing Derive";}
       
        ~Derive(){ cout<<"Destroying Derive";}
 };

void main()
{
    Base *basePtr = new Derive();
        
        delete basePtr;
}

---------------------------------------------------------------------------------------

output

Constructing Base  
Constructing Derive 
Destroying Derive
Destroying Base

No comments:

Post a Comment