[go: up one dir, main page]

0% found this document useful (0 votes)
19 views2 pages

Example PetManagement

Uploaded by

khangthinh.htp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Example PetManagement

Uploaded by

khangthinh.htp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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;
}

You might also like