this code is in c++ and i having trouble making It run this is my first time working with classes and header files main.cpp #include #include #include #include"Pet.h" #include "Pet.cpp" using namespace std; 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]; } return 0; } pet.cpp #include #include "Pet.h" Pet::Pet(std::string nm, int initialHealth) { m_name = nm; m_health = initialHealth; } void Pet::eat(int amt) { if (amt < 0) return; m_health += amt; // Increase the pets health by the amount } void Pet::play() { m_health--; // Decrease pets health by1 for the energy consumed } std::string Pet::name() const { //Return the pets name. delete the following line // and replace it with the correct code. return m_name; // this implentation 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; } bool Pet::alive() const { if (m_health < 0) { return true; } else { 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 isn’t right } void reportStatus(const Pet* p) { std::cout << p->name() << " has health level " << p->health(); if (!p->alive()) { std::cout << ", so has died"; } std::cout << std::endl; } void careFor(Pet* p, int d) { if (!p->alive()) { std::cout << p->name() << " is still dead" << std::endl; return; } // Every third day, you forget to feed your pet if (d % 3 == 0) { std::cout << "You forgot to feed " << p->name() << std::endl; } else { p->eat(1); // Feed the pet one unit of food std::cout << "You fed " << p->name() << std::endl; } p->play(); reportStatus(p); } Pet.h #ifndef Pet_h #define Pet_h #include class Pet { public: Pet(std::string nm, int intialHealth); void eat(int amt); void play(); std::string name() const; int health() const; bool alive() const; private: std::string m_name; int m_health; }; #endif

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

this code is in c++ and i having trouble making It run this is my first time working with classes and header files

main.cpp

#include <iostream>
#include<iomanip>
#include <string>
#include"Pet.h"
#include "Pet.cpp"

using namespace std;

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];
}
return 0;
}

pet.cpp

#include<iostream>
#include "Pet.h"

Pet::Pet(std::string nm, int initialHealth)
{
m_name = nm;
m_health = initialHealth;
}


void Pet::eat(int amt)
{
if (amt < 0)
return;
m_health += amt;
// Increase the pets health by the amount
}

void Pet::play()
{
m_health--;
// Decrease pets health by1 for the energy consumed
}

std::string Pet::name() const
{
//Return the pets name. delete the following line
// and replace it with the correct code.
return m_name; // this implentation 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;
}
bool Pet::alive() const
{
if (m_health < 0)
{
return true;
}
else
{
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 isn’t right
}

void reportStatus(const Pet* p)
{
std::cout << p->name() << " has health level " << p->health();
if (!p->alive()) {
std::cout << ", so has died";
}
std::cout << std::endl;
}
void careFor(Pet* p, int d)
{
if (!p->alive())
{
std::cout << p->name() << " is still dead" << std::endl;
return;
}
// Every third day, you forget to feed your pet
if (d % 3 == 0) {
std::cout << "You forgot to feed " << p->name() << std::endl;
}

else
{
p->eat(1); // Feed the pet one unit of food
std::cout << "You fed " << p->name() << std::endl;
}
p->play();
reportStatus(p);
}

Pet.h

#ifndef Pet_h
#define Pet_h
#include<string>

class Pet
{
public:
Pet(std::string nm, int intialHealth);
void eat(int amt);
void play();
std::string name() const;
int health() const;
bool alive() const;
private:
std::string m_name;
int m_health;

};
#endif

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education