- 面向对象上机考试题—宠物进笼
- 发布日期时间:2007-1-14 来源:网络 点击数: 作者:佚名
Anne的宠物小屋有12个笼子,每个笼子可以放不同的动物,但只能放1只或0只,包括猫Cat,狗Dog,蛇Snake.
1,实现一个简单的管理系统,可增加、删除笼子中的宠物,查询每个笼子中存放的宠物类型,(包括笼子为空的情况),统计笼中宠物的种类和数量。
2,定义描述宠物小屋的类shelves,其中有12笼子用于存放各种宠物。
3,定义虚拟基类Animal,包括纯虚函数ShowMe,显示每个宠物的情况,包括类型、颜色、体重和喜爱的食物。
4,定义派生类Cat,Dog,Snake,具体实现上述纯虚函数。
5,重载输入‘>>’*作符,使得可能通过cin直接读入宠物颜色、体重和喜爱的食物。
6,编写main函数,测试上述要求和各种功能。
*/
#include "Stdafx.h"
#include "iostream.h"
#include "string.h"
enum {NONE=0, CAT=1, DOG=2, SNAKE=3};
class Animal
{
protected:
char color[20];
char food[20];
double weight;
public:
virtual void ShowMe()=0;
};
class Cat:public Animal
{
public:
friend istream& operator>>(istream &is, Cat &cat);
void ShowMe()
{
cout<<"猫的颜色、喜欢的食物、体重分别为:";
cout<<color<<","<<food<<","<<weight<<endl;
}
};
istream & operator>>(istream &is, Cat &cat)
{
cout<<" 请输入猫的颜色: ";
is>>cat.color;
cout<<" 喜爱的食物:";
is>>cat.food;
cout<<" 体重:";
is>>cat.weight;
return is;
}
class Dog:public Animal
{
public:
friend istream& operator>>(istream &is, Dog &Dog);
void ShowMe()
{
cout<<"狗的颜色、喜欢的食物、体重分别为:";
cout<<color<<","<<food<<","<<weight<<endl;
}
};
istream& operator>>(istream &is, Dog &dog)
{
cout<<" 请输入狗的颜色: ";
is>>dog.color;
cout<<" 喜爱的食物:";
is>>dog.food;
cout<<" 体重:";
is>>dog.weight;
return is;
}
class Snake:public Animal
{
public:
friend istream& operator>>(istream &is, Snake &snake);
void ShowMe()
{
cout<<"蛇的颜色、喜爱的食物、体重分别为:";
cout<<color<<","<<food<<","<<weight<<endl;
}
};
istream& operator>>(istream &is, Snake &snake)
{
cout<<" 请输入蛇的颜色: ";
is>>snake.color;
cout<<" 喜爱的食物: ";
is>>snake.food;
cout<<" 体重: ";
is>>snake.weight;
return is;
}
class Shelves
{
int cage[12];
int sum[3];
Cat cats[12];
Dog dogs[12];
Snake snakes[12];
// int flag[12];
// int sum_c, sum_d, sum_s;
public:
Shelves ()
{
for (int i=0; i<12; i++)
cage[i]=NONE;
&
文章转载请注明来源于:汕头自考网
|
|



