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

#include
#include
class first
{
public:
~first( )
{
cout<<"n DEATH OF OBJECT OF CLASS FIRSTn";
}
};
class second : public first
{
public:
~second( )
{
cout<<"n DEATH OF OBJECT OF CLASS SECONDn" ;
}
};
class third : public second
{
public:
~third( )
{
cout<<"n DEATH OF OBJECT OF CLASS THIRDn" ;
}
};
class fourth : public third
{
public:
~fourth( )
{
cout<<"n DEATH OF OBJECT OF CLASS FOURTHn" ;
}
};
main()
{
clrscr();
fourth *d= new fourth;
first *b=d;
delete b;
getch();

}

Leave a reply