向下类型转换
//向下類型轉換指針
#include?<iostream>
using?namespace?std;
class?Father?
{
public:virtual?void?experience(){cout<<"父親經驗比兒子多\n";}
};
class?Son:public?Father?
{
public:virtual?void?Run(){cout<<"兒子跑的比父親快";}
};
int?main()
{Father*pFather;int?choice;cout<<"(1)Father(2)Son:";cin>>choice;if?(choice==2){pFather=new?Son;}elsepFather=new?Father;Son*p=dynamic_cast<Son*>(pFather);if?(p){p->Run();}elsecout<<"轉換失敗\n";delete?pFather;return?0;
}
總結
- 上一篇: 她说她女儿(JAVASCRIPT小时的玩
- 下一篇: 其实没有啥好说的公司组织去清远漂流