Question: please to help me to deal with those problems #include #include using namespace std; class Pet { public: Pet(string nm, int initialHealth); void eat(int amt);

please to help me to deal with those problems

#include

#include

using namespace std;

class Pet

{

public:

Pet(string nm, int initialHealth);

void eat(int amt);

void play();

string name() const;

bool alive() const;

private:

string m_name;

int m_health;

int health() const;

};

// 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 pet's health by 1 for the energy consumed

}

string Pet::name() const

{

//TODO: Return the pet's name. Delete the following line and

//replace it with the correct code.

return m_name; // This implementation compiles, but is incorrect

}

int Pet::health() const

{

//TODO: Return the pet's current health level. Delete the

//following line and replace it with the correct code.

return m_health; // This implementation compiles, but is incorrect

}

bool Pet::alive() const

{

//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.

if (m_health > 0)

return true;

else

return false;// This implementation compiles, but is incorrect

}

void reportStatus(const Pet* p)

{

cout << p->name() << " has health level " << p->health();

if (!p->alive()) {

cout << ", so has died";

}

cout << endl;

}

void careFor(Pet* p, int d)

{

if (!p->alive())

{

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]->alive()) {

cout << "Animal Control has come to rescue " << myPets[k]->name() << endl;

}

delete myPets[k];

}

}

Comment out the entire implementation of Pet::eat, all the way from the void Pet::eat(int amt) to its close curly brace. Why do you get the resulting build error?

In main, replace myPets[0] = new Pet("Fluffy", 2); with myPets[0] = new Pet("Fluffy"); or myPets[0] = new Pet;, Why do you get the resulting compilation errors?

Try removing the const from the implementation, but not the declaration of the alive member function. What happens? Why?

Try removing the const from both the declaration and the implementation of the alive member function. Explain why the use of that function doesn't compile in reportStatus, but does compile in careFor and main.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!