1<span style="font-size:18px;">#include <iostream> 2 3using namespace std ; 4 5class AA 6{ 7 public: 8 int a ; 9 10 //虚函数 11 virtual void say_hello(void) 12 { 13 cout << "this is your parent " << endl ; 14 } 15 16}; 17 18class BB : public AA 19{ 20 public: 21 int b ; 22 23//如果子类没有实现虚函数,则多态中会调用父类的虚函数 24//如果子类有重新实现虚函数,则多态中会调用子类的虚函数 25//__weak 相似 26 27// void say_hello(void) 28// { 29// cout << "this is your son " << endl ; 30// } 31}; 32 33int main(void) 34{ 35 BB bb ; 36 37 AA * aa = NULL ; 38 39 aa = (AA *)&bb ; 40 41 aa->say_hello(); 42 43 44 return 0 ; 45} 46 47</span>
运行结果:

本文同步分享在 博客“Engineer-Bruce_Yang”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。