码迷,mamicode.com
首页 > 编程语言 > 详细

【c++程序】复杂抽象结构

时间:2015-01-14 22:58:27      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
using namespace std;
class Animal
{
public:
	Animal(int);
	virtual ~Animal(){cout<<"析构动物"<<endl;}
	virtual int GetAge(){return itsage;}
	virtual void Sleep()=0;
	virtual void Propagate()=0;
	virtual void Move()=0;
	virtual void Body()=0;
	virtual void Eat()=0;
	virtual void Show()=0;
private:
	int itsage;
};
Animal::Animal(int age):itsage(age)
{
	cout<<"创建动物....."<<endl;
}
class Mammalia:public Animal
{
public:
	Mammalia(int age):Animal(age)
	{
		cout<<"创建哺乳类....."<<endl;
	}
	virtual ~Mammalia()
	{
		cout<<"析构哺乳类...."<<endl;
	}
	virtual void Propagete()
	{
		cout<<"哺乳类是胎生动物,通过胚胎来繁殖后代"<<endl;
	}
};
class Brid:public Animal
{
public:
	Brid(int age):Animal(age)
	{
	   cout<<"创建鸟类...."<<endl;
	}
	virtual ~Brid()
	{
		cout<<"析构鸟类....."<<endl;
	}
	virtual void Sleep()
	{
		cout<<"鸟类喜欢站着睡觉...."<<endl;
	}
	virtual void Eat()
	{
		cout<<"极个别动物喜欢吃肉,其他喜欢吃素...."<<endl;
	}
	virtual void Propagate()
	{
		cout<<"鸟类是卵生动物,通过排卵繁殖...."<<endl;
	}
	virtual void Move()
	{
		cout<<"鸟类可以飞...."<<endl;
	}
	virtual void Body()
	{
		cout<<"鸟类体表背赋羽毛...."<<endl;
	}
	virtual void Show()
	{
		cout<<"鸟类一般寿命"<<GetAge()<<endl;
	}
};
class Human:public Mammalia
{
public:
	Human(int age):Mammalia(age)
	{
		cout<<"创建人类...."<<endl;
	}
	virtual ~Human()
	{
		cout<<"析构人类..."<<endl;
	}
	virtual void Sleep()
	{
		cout<<"人类在床上睡觉...."<<endl;
	}
	virtual void Eat()
	{
		cout<<"大多数人不吃生食...."<<endl;
	}
	virtual void Propagate()
	{
		cout<<"人类通过胚胎繁殖...."<<endl;
	}
	virtual void Move()
	{
		cout<<"人类可以走...."<<endl;
	}
	virtual void Body()
	{
		cout<<"人类体表无毛...."<<endl;
	}
	virtual void Show()
	{
		cout<<"人类一般寿命"<<GetAge()<<endl;
	}
};
class Pig:public Mammalia
{
public:
	Pig(int age):Mammalia(age)
	{
		cout<<"创建猪类...."<<endl;
	}
	virtual ~Pig()
	{
		cout<<"析构猪类..."<<endl;
	}
	virtual void Sleep()
	{
		cout<<"猪类喜欢在烂泥中躺着睡觉...."<<endl;
	}
	virtual void Eat()
	{
		cout<<"猪是杂食动物...."<<endl;
	}
	virtual void Propagate()
	{
		cout<<"猪也通过胚胎繁殖...."<<endl;
	}
	virtual void Move()
	{
		cout<<"猪四条腿走路...."<<endl;
	}
	virtual void Body()
	{
		cout<<"猪体表被毛...."<<endl;
	}
	virtual void Show()
	{
		cout<<"猪因为要被人宰了吃,所以寿命为"<<GetAge()<<endl;
	}
};
int main()
{
	Animal *p=0;
	int choice=0;
	bool quit=false;
	while(choice<4)
	{
		cout<<"1-猪类2-人类3-鸟类0-退出"<<endl;
		cin>>choice;
		switch(choice)
		{
		case 1:p=new Pig(1);
			break;
		case 2:p=new Human(80);
			break;
		case 3:p=new Brid(50);
			break;
		default:quit=true;
			break;
		}
		if(quit)
		{
			break;
		}
		p->Show();
		p->Eat();
		p->Propagate();
		p->Move();
		p->Sleep();
		p->Body();
		cout<<endl;
		delete p;
	}
}
技术分享技术分享

【c++程序】复杂抽象结构

标签:

原文地址:http://blog.csdn.net/u012503639/article/details/42716105

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!