您的位置:首页 > 编程语言 > C语言/C++

【c++程序】模拟抽象类

2015-01-06 20:51 363 查看
#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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: