#include<iostream>
using namespace std;
class Human
{
public:
Human(){cout<<"构造Human"<<endl;}
virtual void smart(){}
virtual void beautiful(){}
virtual ~Human(){cout<<"析构Human"<<endl;}
};
class father:virtual public Human
{
public:
father(){cout<<"构造father"<<endl;}
virtual void smart(){cout<<"父亲很聪明"<<endl;}
virtual ~father(){cout<<"析构父亲"<<endl;}
};
class mother:virtual public Human
{
public:
mother(){cout<<"构造mother"<<endl;}
virtual void beautiful(){cout<<"mother is very beautiful!"<<endl;}
virtual ~mother(){cout<<"析构mother"<<endl;}
};
class son:public father,public mother
{
public:
son(){cout<<"构造son"<<endl;}
virtual void smart(){cout<<"儿子也很聪明"<<endl;}
virtual void beautiful(){cout<<"儿子也很帅"<<endl;}
~son (){cout<<"析构儿子"<<endl;}
};
int main()
{
Human *p;
int choice=0;
while(1)
{
bool quit=false;
cout<<"0-退出1-父亲2-儿子3-母亲"<<endl;
cin>>choice;
switch(choice)
{
case 0:quit=true;
break;
case 1:p=new father;
p->beautiful();
delete p;
break;
case 2:p=new son;
p->beautiful();
p->smart();
delete p;
break;
case 3:p=new mother;
p->beautiful();
delete p;
break;
default:cout<<"请输入0-3"<<endl;
break;
}
if(quit)
{
break;
}
}
cout<<"END!!";
return 0;
}
【c++程序】模拟抽象类
最新推荐文章于 2022-02-09 17:16:34 发布