# Include <iostream> using namespace STD; class base {public: Base () {} virtual ~ Base () {} // The destructor of the base class should be declared as a virtual destructor. Virtual void test () {cout <"base test" <Endl ;}}; class derived: public base {public: derived (){}~ Derived () {cout <"delete derived" <endendl;} void test () {cout <"derived test" <Endl ;}}; void main () {base * pbase = new derived (); // the pointer of the parent class points to the object of the derived class. Pbase-> test (); Delete pbase ;//If the Destructor without the base class is not declared as virtual, The destructor of the subclass will not be executed.// Declare the destructor of the parent class as a virtual function. The function is to use the pointer of the parent class to delete a derived class object .}
Declare the destructor of the parent class as virtual to call the destructor of the subclass When deleting the parent class pointer.