虚函数_构造函数_测试_VS2010x86
1、控制台测试代码:
#include <stdio.h> #include <windows.h> class A { public: A() { printf("A constructor\n"); } public: virtual void SayHi() { printf("A say hi\n"); } }; class B :public A { public: B() { printf("B constructor\n"); } B(int _i) { printf("B constructor : %d\n", _i); } public: void SayHi() { A::SayHi(); printf("B say hi\n"); } }; class C :public B { public: void SayHi() { // 要调用父类的SayHi(),就需要指明是哪个父类的SayHi()。它不会默认去调用父类的SayHi() B::SayHi(); printf("C say hi\n"); } }; void main() { // 子类创建时,必然会调用父类的构造函数 B *pb = new B(); B *pb1= new B(2); printf("\n"); 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、