Program to delete a derived class object using a pointer to a base class that has a virtual destructor results in defined behavior.

#include
#include
class first
{
public:
virtual ~first( )
{
cout<<"n DEATH OF OBJECT OF CLASS FIRST" ;
}
};
class second : public first
{
public:
~second( )
{
cout<<"n DEATH OF OBJECT OF CLASS SECOND" ;
}
};
class third : public second
{
public:
~third( )
{
cout<<"n DEATH OF OBJECT OF CLASS THIRD" ;
}
};
class fourth : public third
{
public:
~fourth( )
{
cout<<"n DEATH OF OBJECT OF CLASS FOURTH" ;
}
};
main()
{
clrscr();
fourth *d = new fourth();
first *b=d;
delete b;
getch();
}

Leave a reply