Question: C++ 1. Make the Pet class abstract by making the getLifespan function pure virtual. Make the destructor virtual. You may leave all the others unchanged.

C++

1. Make the Pet class abstract by making the getLifespan function pure virtual. Make the destructor virtual. You may leave all the others unchanged.

2. Remove all the testing code of the given project and replace it with the following: Make an array of 20 pointers to class Pet. Construct 20 objects of Dog or Rock randomly and let the array of Pet pointers point to them. Loop through the array of pointers to Pet and print the name of the object class using runtime type information (RTTI), and then display the life span returned by function getLifespan.

Driver.cpp

#include "Pet.h" #include "Dog.h" #include "Rock.h" #include #include #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 using namespace std;

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 #include "Pet.h"

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 #include "Pet.h"

using namespace std;

class Rock : public Pet { public: Rock(); Rock(string n, int a, double w); string getLifespan() const; };

Rock.cpp

#include #include "Rock.h"

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 #include "Pet.h" using namespace std;

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 #include #include "Dog.h"

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

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!