【c++程序】多重继承

来源:互联网 发布:手机网络渗透软件 编辑:程序博客网 时间:2024/05/09 05:44
#include<iostream>using namespace std;class father {public:void smart(){cout<<"父亲很聪明"<<endl;}//virtual void beautiful(){}virtual ~father(){cout<<"析构父亲"<<endl;}};class son:public father{public:virtual void beautiful(){cout<<"儿子也很帅"<<endl;}~son (){cout<<"析构儿子"<<endl;}};int main(){father *pf;int choice=0;while(1){bool quit=false;cout<<"0-退出1-父亲2-儿子"<<endl;cin>>choice;switch(choice){case 0:quit=true;break;case 1:pf=new father;//pf->beautiful();break;case 2:pf=new son;dynamic_cast<son*>(pf)->beautiful();//dynamic_cast的作用是对不同类之间数据类型进行转换pf->smart();delete pf;break;default:cout<<"请输入0-3"<<endl;}if(quit){break;}}cout<<"END!!";return 0;}


 

0 0