Question: what are the functions that can be called on ANY object of class Pet, Dog and Rock. what are the functions that can be called
what are the functions that can be called on ANY object of class Pet, Dog and Rock.
what are the functions that can be called ONLY on object of class Dog.
What does the print function display and does it display the same items for the objects of all these three classes
What does the getLifeSpan function return and does it return the same string for the objects of all these three classes
Driver.cpp
#include "Pet.h" #include "Dog.h" #include "Rock.h" #include
using namespace std; int main() { Pet p("Petty", 5, 25); Dog d("Husky", 3, 500, "husky"); Rock rock;
cout << "Pet class" << endl; p.print(); cout << p.getLifespan() << endl << endl;
cout << "Dog class" << endl; d.print(); cout << d.getLifespan() << endl << endl;
cout << "Rock class" << endl; rock.print(); cout << rock.getLifespan() << endl << endl;
cout << "Dog class after using a few setters" << endl; d.setAge(13); d.setWeight(50.0); d.print(); cout << d.getLifespan();
int dummyVar = _getch(); return 0; }
Pet.h
#ifndef PET_H #define PET_H #include
class Pet { private: string name; int age; double weight; public: Pet(); Pet(string n, int a, double w); void setName(string n); string getName() const; void setAge(int a); int getAge() const; void setWeight(double d); double getWeight() const; void print(); string getLifespan() const; };
#endif
Pet.cpp
#include
using namespace std;
Pet::Pet() { name = "Pet"; age = 0; weight = 0.0; }
Pet::Pet(string n, int a, double w) { name = n; age = a; weight = w; }
void Pet::setName(string n) { name = n; }
string Pet::getName() const { return name; }
void Pet::setAge(int a) { age = a; }
int Pet::getAge() const { return age; }
void Pet::setWeight(double d) { weight = d; }
double Pet::getWeight() const { return weight; }
void Pet::print() { cout << "Pet's Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; } string Pet::getLifespan() const { return "unknown lifespan"; }
Rock.h
#include
using namespace std;
class Rock : public Pet { public: Rock(); Rock(string n, int a, double w); string getLifespan() const; };
Rock.cpp
#include
using namespace std;
Rock::Rock() :Pet("Rock", 1000, 1000.0) {} Rock::Rock(string n, int a, double w) : Pet(n, a, w) {} string Rock::getLifespan() const { return "Thousands of years"; }
Dog.h
#include
class Dog : public Pet { private: string breed; public: Dog(); Dog(string n, int a, double w, string b); void setBreed(string b); string getBreed() const; string getLifespan() const; };
Dog.cpp
#include
Dog::Dog() :Pet("Dog", 0, 0) { breed = "Breed"; }
Dog::Dog(string n, int a, double w, string b) : Pet(n, a, w) { breed = b; }
void Dog::setBreed(string b) { breed = b; }
string Dog::getBreed() const { return breed; }
string Dog::getLifespan() const { if (getWeight() > 100) { return "Approximately 7 years"; } else return "Approximately 13 years"; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
