#include<iostream> usingnamespace std; classA { public: A() { cout << "class A is constructed" << endl;} ~A() { cout << "class A is destroyed" << endl; } //method voidfa(){ cout << "class A fa method" << endl;} voidfb(){ cout << "class A fb method" << endl;} }; classB : public A { public: B() { cout << "class B is constructed" << endl;} ~B() { cout << "class B is destroyed" << endl; } //method voidfa(){ cout << "class B fa method" << endl;} voidfb(){ cout << "class B fb method" << endl;} }; intmain() { A *pa = new A; B *pb = new B; cout << endl; pa->fa(); pa->fb(); cout << endl; pb->fa(); pb->fb(); cout << endl; delete pa; delete pb; cout << endl; return0; }
1 2 3 4 5 6 7 8 9 10 11
运行结果如下: class A is constructed class A is constructed class B is constructed class A fa method class A fb method class B fa method class B fb method class A is destroyed class B is destroyed class A is destroyed