Bài giải minh họa
Ví dụ chương trình quản lý thú cưng.
#include <iostream>
#include <string>
using namespace std;
struct DATE{
int day, month, year;
};
class Pet{
public:
float weight, length, height;
string type, color;
DATE lastPhysCheck;
public:
virtual float FoodCalc(string half) =0;
virtual int RemainingDays(DATE currDate) = 0;
};
class Cat: public Pet{
public:
float sleepinghours;
public:
float FoodCalc(string half){
if (half == "morning")
return height * 5;
else
return height * 5 + 15 - sleepinghours;
}
int RemainingDays(DATE currDate)
{
int cur = 365*(currDate.year-1) + 30* (currDate.month-1) + currDate.day;
int last = 365*(lastPhysCheck.year-1) + 30* (lastPhysCheck.month-1) +
lastPhysCheck.day;
return 3*30 - (cur-last);
}
};
class Dog: public Pet{
public:
float playinghours;
public:
float FoodCalc(string half){
if (half == "morning")
return 0;
else
return height * 5 + 15 - playinghours;
}
int RemainingDays(DATE currDate)
{
int cur = 365*(currDate.year-1) + 30* (currDate.month-1) + currDate.day;
int last = 365*(lastPhysCheck.year-1) + 30* (lastPhysCheck.month-1) +
lastPhysCheck.day;
return 6*30 - (cur-last);
}
};
int main()
{
Cat c;
Dog d;
c.lastPhysCheck = {1,1,2023};
d.lastPhysCheck = {12,12,2022};
cout << c.RemainingDays({21,3,2023}) << endl;
cout << d.RemainingDays({21,3,2023}) << endl;
return 0;
}