模拟抽象类
#include <iostream>
using namespace std;
class Human//接口類ADT
{
public:
?Human(){cout<<"構造人類!"<<endl;}
?virtual void smart(){}
?virtual void beautiful(){}
?virtual ~Human(){cout<<"析構人類!"<<endl;}
};
class father:virtual public Human
{
public:
?father(){cout<<"構造父親類!"<<endl;}
?virtual void smart(){cout<<"父親很聰明!"<<endl;}
?virtual ~father(){cout<<"析構父親類!"<<endl;}
};
class mother:virtual public Human
{
public:
?mother(){cout<<"構造母親類!"<<endl;}
?virtual void beautiful(){cout<<"母親很漂亮!"<<endl;}
?virtual ~mother(){cout<<"析構母親類!"<<endl;}
};
class son:public father,public mother
{
public:
?son(){cout<<"構造兒子類!"<<endl;}
?virtual void smart(){cout<<"兒子也很聰明"<<endl;}
?virtual void beautiful(){cout<<"兒子也很英俊!"<<endl;}
?virtual ~son(){cout<<"析構兒子類!"<<endl;}
};
int main()
{
?Human *p;
?int choice=0;
?while(choice<99)
?{
??bool quit=false;
??cout<<"[0]退出[1]父親[2]兒子[3]母親:";
??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到2之前的數"<<endl;
???break;
??}
??if (quit==true)
??{
???break;
??}
?}
?cout<<"程序結束!"<<endl;
?return 0;
}
總結