Learning materials
• base class and derived class destructor execution order
Defining derived class destructors
Note : When you define an object, you call the constructor of the base class, and then call the constructor of the derived class, which is the exact opposite of invoking the destructor of the derived class, and then calling the destructor of the base class.
1#include <iostream>2 using namespacestd;3 4 classBase5 {6 Public:7 Base ()8 {9cout <<"Base contruction"<<Endl;Ten } One Virtual~Base () A { -cout <<"Base Deconstruction"<<Endl; - } the - }; - - classDerived: PublicBase + { - Public: +Derived (inti) A { atnum =i; -cout <<"Derived contruction"<< Num <<Endl; - } - Virtual~Derived () - { -cout <<"Derived Deconstruction"<< Num <<Endl; in } - Private: to intnum; + }; - the intMain () * { $Derived Derived (1);Panax Notoginseng -base*baseptr; theBaseptr =NewDerived (2); + Delete baseptr; A}
Output Result:
"C + + Primer 15th" defines a derived class destructor