-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj7warmup.cpp
120 lines (106 loc) · 2.53 KB
/
proj7warmup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
using namespace std;
class Pet
{
public:
Pet(string nm, int initialHealth);
void eat(int amt);
void play();
string name() const;
int health() const;
bool isAlive() const;
private:
string m_name;
int m_health;
};
// Initialize the state of the pet
Pet::Pet(string nm, int initialHealth)
{
m_name = nm;
m_health = initialHealth;
}
void Pet::eat(int amt)
{
m_health += amt;
// TODO: Increase the pet's health by the amount
}
void Pet::play()
{
m_health--;
// TODO: Decrease the pet's health by 1 for the energy consumed
}
string Pet::name() const
{
return m_name;
// TODO: Return the pet's name. Delete the following line and
// replace it with the correct code.
// This implementation compiles, but is incorrect
}
int Pet::health() const
{
return m_health;
// TODO: Return the pet's current health level. Delete the
// following line and replace it with the correct code.
// This implementation compiles, but is incorrect
}
bool Pet::isAlive() const
{
if (m_health > 0)
{
return true;
}
return false;
// TODO: Return whether pet is alive. (A pet is alive if
// its health is greater than zero.) Delete the following
// line and replace it with the correct code.
// This implementation compiles, but is incorrect
}
/////////
// Do not change any code below this point
/////////
void reportStatus(const Pet *p)
{
cout << p->name() << " has health level " << p->health();
if (!p->isAlive())
cout << ", so has died";
cout << endl;
}
void careFor(Pet *p, int d)
{
if (!p->isAlive())
{
cout << p->name() << " is still dead" << endl;
return;
}
// Every third day, you forget to feed your pet
if (d % 3 == 0)
cout << "You forgot to feed " << p->name() << endl;
else
{
p->eat(1); // Feed the pet one unit of food
cout << "You fed " << p->name() << endl;
}
p->play();
reportStatus(p);
}
int main()
{
Pet *myPets[2];
myPets[0] = new Pet("Fluffy", 2);
myPets[1] = new Pet("Frisky", 4);
for (int day = 1; day <= 9; day++)
{
cout << "======= Day " << day << endl;
for (int k = 0; k < 2; k++)
careFor(myPets[k], day);
}
cout << "=======" << endl;
for (int k = 0; k < 2; k++)
{
if (myPets[k]->isAlive())
cout << "Animal Control officers have come to rescue "
<< myPets[k]->name() << endl;
delete myPets[k];
}
}