每个笼子可以放不同的动物,但只能放1只或0只,包括.docVIP

  • 15
  • 0
  • 约4.22千字
  • 约 12页
  • 2018-04-24 发布于江苏
  • 举报

每个笼子可以放不同的动物,但只能放1只或0只,包括.doc

每个笼子可以放不同的动物,但只能放1只或0只,包括

Anne的宠物小屋有12个笼子,每个笼子可以放不同的动物,但只能放1只或0只,包括猫Cat,狗Dog,蛇Snake. 定义描述宠物小屋的类shelves,其中有12笼子用于存放各种宠物。 定义虚拟基类Animal,包括纯虚函数ShowMe,显示每个宠物的情况,包括类型、颜色、体重和喜爱的食物。 定义派生类Cat,Dog,Snake,具体实现上述纯虚函数。 编写main函数,可增加、删除笼子中的宠物,查询每个笼子中存放的宠物类型,(包括笼子为空的情况),统计笼中宠物的种类和数量。 解答:#include iostream.h #include12.h //int int Cat::CatNum=0; Cat::Cat(){ type=0; colour=new char[20]; food=new char[20]; CatNum++; } void Cat::ShowMe(){ coutThe animal in the shelve is a Cat.endl; coutThe colour is: colourendl; coutThe weight is: weightendl; coutThe favorite food is: foodendl; return; } int Cat::number(){ return CatNum; } istream operator (istream is,Cat cat){ coutThe Cats colour is:; cincat.colour; coutThe Cats weight is:; cincat.weight; coutThe Cats favorite food is:; cincat.food; return is; } void Cat::DeleteMe(){ CatNum--; } Cat::~Cat(){} int Dog::DogNum=0; Dog::Dog(){ type=1; colour=new char[20]; food=new char[20]; DogNum++; } void Dog::ShowMe(){ coutThe animal in the shelve is a Dog.endl; coutThe colour is: colourendl; coutThe weight is: weightendl; coutThe favorite food is: foodendl; return; } int Dog::number(){ return DogNum; } istream operator (istream is,Dog dog){ coutThe Dogs colour is: ; cindog.colour; coutThe Dogs weight is: ; cindog.weight; coutThe Dogs favorite food is: ; cindog.food; return is; } void Dog::DeleteMe(){ DogNum--; } Dog::~Dog(){ } int Snake::SnakeNum=0; Snake::Snake(){ type=2; colour=new char[20]; food=new char[20]; SnakeNum++; } void Snake::ShowMe(){ coutThe animal in the shelve is a Snake.endl; coutThe colour is: colourendl; coutThe weight is: weightendl; coutThe favorite food is: foodendl; return; } int Snake::number(){ return SnakeNum; } istream operator(istream is,Snake snake) { coutThe Snakes colour is: ; cinsnake.colour; coutThe Snakes weight is: ; cinsnake.weight; coutThe Snakes favorite food is: ; cinsnake.food; return is; } void Snake::DeleteMe(){ SnakeNum--; } Snake::~Sn

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档