1#include <iostream>2 3 4 using namespacestd;5 6 classA7 {8 Public:9A () {cout <<"Construction a!"<<Endl;};Ten Virtual~a () {cout <<"destructor a!"<<Endl;}; One A Virtual voidDoSomething () {cout <<"A's dosomething!"<<Endl;}; - }; - the classB: PublicA - { - Public: -B () {cout <<"Construction b!"<<Endl;}; +~b () {cout <<"destructor b!"<<Endl;}; - + voidDoSomething () {cout <<"B's dosomething!"<<Endl;}; A }; at - intMain () - { -A *test1 =NewA; -cout <<"**************"<<Endl; -Test1->dosomething (); incout <<"**************"<<Endl; - DeleteTest1; tocout <<"**************"<<Endl; +B *test2 =NewB; -cout <<"**************"<<Endl; theTest2->dosomething (); *cout <<"**************"<<Endl; $ DeleteTest2;Panax Notoginsengcout <<"**************"<<Endl; -A *test3 =NewB; thecout <<"**************"<<Endl; +Test3->dosomething (); Acout <<"**************"<<Endl; the DeleteTest3; + return 0; -}View Code
Operation Result:
Modify the code, delete the virtual
1#include <iostream>2 3 4 using namespacestd;5 6 classA7 {8 Public:9A () {cout <<"Construction a!"<<Endl;};Ten~a () {cout <<"destructor a!"<<Endl;}; One A Virtual voidDoSomething () {cout <<"A's dosomething!"<<Endl;}; - }; - the classB: PublicA - { - Public: -B () {cout <<"Construction b!"<<Endl;}; +~b () {cout <<"destructor b!"<<Endl;}; - + voidDoSomething () {cout <<"B's dosomething!"<<Endl;}; A }; at - intMain () - { -A *test1 =NewA; -cout <<"**************"<<Endl; -Test1->dosomething (); incout <<"**************"<<Endl; - DeleteTest1; tocout <<"**************"<<Endl; +B *test2 =NewB; -cout <<"**************"<<Endl; theTest2->dosomething (); *cout <<"**************"<<Endl; $ DeleteTest2;Panax Notoginsengcout <<"**************"<<Endl; -A *test3 =NewB; thecout <<"**************"<<Endl; +Test3->dosomething (); Acout <<"**************"<<Endl; the DeleteTest3; + return 0; -}View Code
Operation Result:
If it is
b b;
Results:
Destructors and virtual destructors