Question: Write a complete C++ program, using classes that implements polymorphism in order to create the following output: Let's populate your farm Please choose 4 animals

Write a complete C++ program, using classes that implements polymorphism in order to create the following output:

Let's populate your farm

Please choose 4 animals :

(1)dog (2)sheep (3)horse (4)pig: 1

Mammal constructor...

Dog constructor

(1)dog (2)sheep (3)horse (4)pig: 2

Mammal constructor...

Sheep constructor

(1)dog (2)sheep (3)horse (4)pig: 3

Mammal constructor...

Horse constructor

(1)dog (2)sheep (3)horse (4)pig: 4

Mammal constructor...

Pig constructor

WOOF

BAA

NEIGH

OINK

Dog destructor

Mammal destructor..

Sheep destructor

Mammal destructor...

Horse destructor

Mammal destructor...

Pig destructor

Mammal destructor...

Program ended with exit code: 0

Use the mammal class developed below as the base class and create 4 subclasses for dog, horse, sheep and pig derived objects to be stored in a Mammal array farm[4]; Each mammal should call its own Speak() function.

Place your classes and driver in one file.

#include using namespace std; class Mammal { private: int Age; public: Mammal() { cout << "Mammal constructor ..." << endl;} //constuctor ~Mammal() { cout << "Mammal destructor..." << endl;} // destructor virtual void Move() { cout << "Mammal moves a step" << endl;} virtual void Speak() { cout << "Mammal says Hi! " << endl;} }; class Dog: public Mammal { public: Dog() {cout << "Dog constructor..." << endl;} ~Dog() {cout << "Dog destructor..." << endl;} void Move() {cout << "Dog moves a step " << endl;} void Speak() {cout << "BARK!" << endl;} }; int main() { Mammal *dptr = new Dog; dptr->Speak(); dptr->Move(); return 0; }

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!