Anne的宠物小屋有12个笼子每个笼子可以放不同的动物.docx
- 文档编号:28791172
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:9
- 大小:15.43KB
Anne的宠物小屋有12个笼子每个笼子可以放不同的动物.docx
《Anne的宠物小屋有12个笼子每个笼子可以放不同的动物.docx》由会员分享,可在线阅读,更多相关《Anne的宠物小屋有12个笼子每个笼子可以放不同的动物.docx(9页珍藏版)》请在冰豆网上搜索。
Anne的宠物小屋有12个笼子每个笼子可以放不同的动物
Anne的宠物小屋有12个笼子,每个笼子可以放不同的动物,但只能放1只或0只,包括猫Cat,狗Dog,蛇Snake.
1)定义描述宠物小屋的类shelves,其中有12笼子用于存放各种宠物。
2)定义虚拟基类Animal,包括纯虚函数ShowMe,显示每个宠物的情况,包括类型、颜色、体重和喜爱的食物。
3)定义派生类Cat,Dog,Snake,具体实现上述纯虚函数。
编写main函数,可增加、删除笼子中的宠物,查询每个笼子中存放的宠物类型,(包括笼子为空的情况),统计笼中宠物的种类和数量。
解答:
#include
#include"12.h"
//int
intCat:
:
CatNum=0;
Cat:
:
Cat(){
type=0;
colour=newchar[20];
food=newchar[20];
CatNum++;
}
voidCat:
:
ShowMe(){
cout<<"TheanimalintheshelveisaCat."< cout<<"Thecolouris: "< cout<<"Theweightis: "< cout<<"Thefavoritefoodis: "< return; } intCat: : number(){ returnCatNum; } istream&operator>>(istream&is,Cat&cat){ cout<<"TheCat'scolouris: "; cin>>cat.colour; cout<<"TheCat'sweightis: "; cin>>cat.weight; cout<<"TheCat'sfavoritefoodis: "; cin>>cat.food; returnis; } voidCat: : DeleteMe(){ CatNum--; } Cat: : ~Cat(){} intDog: : DogNum=0; Dog: : Dog(){ type=1; colour=newchar[20]; food=newchar[20]; DogNum++; } voidDog: : ShowMe(){ cout<<"TheanimalintheshelveisaDog."< cout<<"Thecolouris: "< cout<<"Theweightis: "< cout<<"Thefavoritefoodis: "< return; } intDog: : number(){ returnDogNum; } istream&operator>>(istream&is,Dog&dog){ cout<<"TheDog'scolouris: "; cin>>dog.colour; cout<<"TheDog'sweightis: "; cin>>dog.weight; cout<<"TheDog'sfavoritefoodis: "; cin>>dog.food; returnis; } voidDog: : DeleteMe(){ DogNum--; } Dog: : ~Dog(){ } intSnake: : SnakeNum=0; Snake: : Snake(){ type=2; colour=newchar[20]; food=newchar[20]; SnakeNum++; } voidSnake: : ShowMe(){ cout<<"TheanimalintheshelveisaSnake."< cout<<"Thecolouris: "< cout<<"Theweightis: "< cout<<"Thefavoritefoodis: "< return; } intSnake: : number(){ returnSnakeNum; } istream&operator>>(istream&is,Snake&snake) { cout<<"TheSnake'scolouris: "; cin>>snake.colour; cout<<"TheSnake'sweightis: "; cin>>snake.weight; cout<<"TheSnake'sfavoritefoodis: "; cin>>snake.food; returnis; } voidSnake: : DeleteMe(){ SnakeNum--; } Snake: : ~Snake(){ } Shelves: : Shelves(){ inti; for(i=0;i<12;i++) shelves[i]=NULL; } intShelves: : Add(intn,Animal*animal){ intsuccessful=1; if(shelves[n]! =NULL){ cout<<"The"< "< successful=0; returnsuccessful; } shelves[n]=animal; cout<<"Successful! "< returnsuccessful; } voidShelves: : Query(intn){ if(shelves[n]==NULL){ cout<<"The"< "< return; } shelves[n]->ShowMe(); return; } voidShelves: : Delete(intn){ switch(shelves[n]->type){ case0: Cat: : Deletenum();break; case1: Dog: : Deletenum();break; case2: Snake: : Deletenum(); } if(shelves[n]==NULL){ cout<<"The"< "< return; } deleteshelves[n]; shelves[n]=NULL; cout<<"Successful! "< return; } voidShelves: : List(){ cout<<"Thereare"; cout< : number()<<"cats," < : number()<<"dogs,and" < : number()<<"snakes."< return; } voidmain(){ Shelves*shelves=newShelves; Cat*cat; Dog*dog; Snake*snake; intn; inti=0; charch; while(i! =5) { cout<<"Pleasechiose(1--5): "< cout<<"1: addananimal"< cout<<"2: deleteananimal"< cout<<"3: queryanyanimalinthecage"< cout<<"4: Listalltheanimalsinthecage"< cout<<"5: exit"< cin>>i; switch(i){ case1: cout<<"whichcage? "< cin>>n; cout<<"c: cat"< dog"< snake"< cout<<"Pleasechoiseananimal: "; cin>>ch; switch(ch){ case'c': cat=newCat; cin>>*cat; if(! shelves->Add(n,cat)) cat->DeleteMe(); break; case'd': dog=newDog; cin>>*dog; if(! shelves->Add(n,dog)) dog->DeleteMe(); break; case's': snake=newSnake; cin>>*snake; if(! shelves->Add(n,snake)) snake->DeleteMe(); break; default: cout<<"Yourchoiseiswrong! "< break; } break; case2: cout<<"whichcage? "< cin>>n; shelves->Delete(n); break; case3: cout<<"whichcage? "< cin>>n; shelves->Query(n); break; case4: shelves->List(); break; case5: break; } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Anne 宠物 小屋 12 笼子 每个 可以 不同 动物