# 宠物的生长（虚函数和多态）

235 篇文章 36 订阅

AC代码

### 题目描述

class Pet

{ protected:

string name;//姓名

float length;//身长

float weight;//体重

CDate current;//开始记录时间

（日期类CDate包含年、月、日三个私有数据，其他方法根据需要自拟。）

public:

virtual void display(CDate day)=0;//输出目标日期时宠物的身长和体重

}

Cat一天身长加0.1，体重加0.2。

Dog一天身长加0.2，体重加0.1。

3
2019 5 5
1 tony 10 10 2018 12 30
2 jerry 5 6 2019 5 10
1 tom 3 4 2019 6 1

error
jerry after 5 day: length=6.00,weight=6.50
tom after 27 day: length=5.70,weight=9.40

### AC代码

#include"iostream"
#include"string"
#include"iomanip"
using namespace std;
class CDate
{
int year, month, day;
public:
CDate(int year,int month,int day) :year(year), month(month), day(day) {}
friend int days(CDate &a, CDate &b);
friend int remaindays(CDate &a);
};
class Pet
{
protected:
string name;//姓名
float length, weight;//体重
CDate current;//开始记录时间
public:
Pet(string name, float length, float weight, CDate current) :name(name), length(length), weight(weight), current(current) {}
virtual void display(CDate day) = 0;//输出目标日期时宠物的身长和体重
};
class Cat :public Pet
{
public:
Cat(string name, float length, float weight, CDate current) :Pet(name, length, weight, current) {
}
virtual void display(CDate day)
{
if (days(current, day)!=-1)
{
length += 0.1 * days(current, day);
weight += 0.2 * days(current, day);
cout << name << " after " << days(current, day) << " day: length=" << fixed << setprecision(2) << length << ",weight=" << weight << endl;
}
else
cout << "error" << endl;
}
};
class Dog :public Pet
{
public:
Dog(string name, float length, float weight, CDate current) :Pet(name, length, weight, current) {
}
virtual void display(CDate day)
{
if (days(current, day)!=-1)
{
length += 0.2 * days(current, day);
weight += 0.1 * days(current, day);
cout << name << " after " << days(current, day) << " day: length=" << fixed << setprecision(2) << length << ",weight=" << weight << endl;
}
else
cout << "error" << endl;
}
};
bool isleap(int year)
{
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
return 1;
return 0;
}
int remaindays(CDate &a)
{
long days;
int i,leapyear[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 },normalyear[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (isleap(a.year))
{
days = leapyear[a.month - 1] - a.day;
for (i = a.month; i < 12; i++)
days += leapyear[i];
}
else
{
days = normalyear[a.month - 1] - a.day;
for (i = a.month; i < 12; i++)
days += normalyear[i];
}
return days;
}
int days(CDate &a, CDate &b)
{
if (a.year > b.year)
return -1;
if (a.year == b.year && a.month > b.month)
return -1;
if (a.year == b.year && a.month == b.month && a.day > b.day)
return -1;
long daysss = remaindays(a);
if (a.year < b.year)
{
for (int i = a.year+1; i <=b.year; i++)
if (isleap(i))
daysss += 366;
else
daysss += 365;
daysss -= remaindays(b);
}
else if (a.year == b.year)
daysss = remaindays(a) - remaindays(b);
else
daysss= b.day - a.day;
return daysss;
}
int main()
{
Pet* p;
int year, month, day, t,kind;
string name;
float length,weight;
cin>>t>>year>>month>>day;
CDate a(year,month,day);
while (t--)
{
cin>>kind>>name>>length>>weight>>year>>month>>day;
CDate b(year,month,day);
if(kind==1)
p=new Cat(name,length,weight,a);
else
p=new Dog(name,length,weight,a);
p->display(b);
}
}
• 2
点赞
• 2
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
10-08
07-31
10-26 182
03-03 546
08-17
07-01 1339

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。