Question: C++ #include stdafx.h #include #include using namespace std; // base class animal class animal { // declare the required variables protected: string animalName; string sound;

C++

#include "stdafx.h" #include #include using namespace std; // base class animal class animal { // declare the required variables protected: string animalName; string sound; //declare the required method and constructor public: // constructor animal(string animalName, string sound) { this->animalName = animalName; this->sound = sound; } // virtual method virtual void animalSound() { cout << animalName << " says " << sound << endl;; } }; // derived class cat class cat : public animal { public: // constructor cat(string animalName, string sound) : animal(animalName, sound) {} // method overriding void animalSound() { cout << animalName << " says " << sound << endl; } }; // derived class dog class dog : public animal { public: // constructor dog(string animalName, string sound) : animal(animalName, sound) {} // method overriding void animalSound() { cout << animalName << " says " << sound << endl; } }; // derived class elephant class elephant : public animal { public: // constructor elephant(string animalName, string sound) : animal(animalName, sound) {} // method overriding void animalSound() { cout << animalName << " says " << sound << endl; } }; // derived class mouse class mouse : public animal { public: // constructor mouse(string animalName, string sound) : animal(animalName, sound) {} // method overriding void animalSound() { cout << animalName << " says " << sound << endl; } }; // main method int main(void) { // declare the required variables string aname; int op;

// create object for the class cat C1("Tom", "Meow"); dog D1("Rozar", "Bow..Bow"); elephant E1("Batyr", "roar"); mouse M1("Jerry", "Crich..Crich"); // get the user option cout << "Eneter animal name (Tom Rozar Batyr Jerry):"; cin >> aname; if (aname == "Tom") op = 1; if (aname == "Rozar") op = 2; if (aname == "Batyr") op = 3; if (aname == "Jerry") op = 4; // switch case switch (op) { case 1: C1.animalSound(); break; case 2: D1.animalSound(); break;

case 3: E1.animalSound(); break; case 4: M1.animalSound(); break; default: cout << " option must be (Tom, or Rozar, or Batyr, or Jerry)"; break; } system("pause"); }

Part 6 - loop (Base and Derived classes should already be coded in part 3) Create an animal base class. string animalName; string sound; // Use constructor to set derived class sound virtual animalSound() { cout << AnimalName << " says " << sound << endl; Create 4 new derived classes based on animal, that override the virtual function, animalSound, with the specific animal noise, and assign the animal name to the base animal name variable. cat dog elephant mouse Declare instances of each of the 4 different animals. C1, D1, E1, M1. Declare an array of 4 of type animal animal ** ptrBaseAnimal = new Animal * [4]; Declare instances of each of the 4 different animals. C1, D1, E1, M1.

example: ptrBaseAnimal[0] = new Cat;

do the rest... Assign the derived address of each to on element in the base array. Write a for loop to print out the animal noices for ( int I = 0 ; I < 4 ; I++) { ptrBaseAnimal[I]->animalSound; } --------

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!