1、控制台测试代码:
#include <stdio.h> #include <windows.h> class A { public: A() { printf("A constructor "); } public: virtual void SayHi() { printf("A say hi "); } }; class B :public A { public: B() { printf("B constructor "); } B(int _i) { printf("B constructor : %d ", _i); } public: void SayHi() { A::SayHi(); printf("B say hi "); } }; class C :public B { public: void SayHi() { // 要调用父类的SayHi(),就需要指明是哪个父类的SayHi()。它不会默认去调用父类的SayHi() B::SayHi(); printf("C say hi "); } }; void main() { // 子类创建时,必然会调用父类的构造函数 B *pb = new B(); B *pb1= new B(2); printf(" "); C c; c.SayHi(); system("pause"); }
2、控制台输出:
A constructor
B constructor
A constructor
B constructor : 2
A constructor
B constructor
A say hi
B say hi
C say hi
请按任意键继续. . .
3、
4、
5、