现在的位置: 首页 > 综合 > 正文

模拟抽象类

2018年04月14日 ⁄ 综合 ⁄ 共 1158字 ⁄ 字号 评论关闭

#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;
}

【上篇】
【下篇】

抱歉!评论已关闭.