首页 > 分享 > 宠物生长管理系统

宠物生长管理系统

宠物的生长(多态)

最新推荐文章于 2023-06-18 21:24:01 发布

Self-den1al丶 于 2020-05-17 18:15:15 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

现在要开发一个系统,对宠物的生长状态进行管理。

给出下面的一个基类框架

class Pet { protected: string name;//姓名 int length;//身长 int weight;//体重 int current;//当前日期 public: virtual void display(int day)=0;//输出目标日期的身长和体重 Pet(string name, int l, int w, int c); }

123456789101112131415161718192021

以Pet为基类,构建出Cat和Dog两个类:

Cat一天身长加1,体重加2。

Dog一天身长加2,体重加1。

生成上述类并编写主函数,要求主函数中有一个基类Pet指针数组,数组元素不超过10个。

Pet *pt[10];

主函数根据输入的信息,相应建立Cat类对象或Dog类对象,并给出目标日期宠物的身长和体重。

提示:应用虚函数实现多态

输入格式:
每个测试用例占一行,每行给出宠物的基本信息,第一个为当前宠物的类型:1为Cat,2为Dog。接下来为它的名字,随后的两个数字为身长和体重,最后为测身长和体重的日期(不大于10的正整数)。最后一行为目标日期(大于10的正整数)

输出格式: 输出目标日期宠物姓名、身长和体重

输入样例:
1 Marry 22 12 5
2 Jack 10 9 9
1 Jim 11 8 6
11
输出样例:
Marry 28 24
Jack 14 11
Jim 16 18

我的代码:

#include <iostream> using namespace std; class Pet { protected: string name;//姓名 int length;//身长 int weight;//体重 int current;//当前日期 int day; public: virtual void display(int day)=0;//输出目标日期的身长和体重 Pet(){ name=""; length=0; weight=0; current=0; day=0; } }; class Cat:public Pet{ public: Cat(string na,int l,int w,int c){ name=na; length=l; weight=w; current=c; } void display(int d){ day=d; length+=(day-current); weight+=(day-current)*2; cout<<name<<" "<<length<<" "<<weight<<endl; } }; class Dog:public Pet{ public: Dog(string na,int l,int w,int c){ name=na; length=l; weight=w; current=c; } void display(int d){ day=d; length+=(day-current)*2; weight+=(day-current); cout<<name<<" "<<length<<" "<<weight<<endl; } }; int main(){ int length,weight,current,day,type,i,count=0; string name; Pet *pt[10]; while(1){ cin>>type; if(type>10){ for(i=0;i<count;i++){ pt[i]->display(type); delete pt[i]; } break; } cin>>name>>length>>weight>>current; if(type==1){ pt[count]=new Cat(name,length,weight,current); } else{ pt[count]=new Dog(name,length,weight,current); } count++; } return 0; }

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374

相关知识

宠物生长管理系统的设计与实现
宠物管理系统宠物医院管理系统免费宠物系统
宠物训练销售卡管理系统【宠物训练销售卡管理系统销售客户管理系统】
【宠物医院管理系统 】宠物医院管理系统
【宠物门店管理收银系统】宠物门店管理收银系统怎么做?
宠物管理系统(宠物管理系统的设计与实现)
宠物管理系统是什么?
宠物管理系统怎么样
RFID宠物管理系统
一种宠物管理系统

网址: 宠物生长管理系统 https://m.mcbbbk.com/newsview1156396.html

所属分类:萌宠日常
上一篇: 意大利布斯托
下一篇: 正确的时间、方法和注意事项,让宠