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
// 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 4 - Function (Base and Derived classes should already be coded in part 3. Create an animal base class. string animalName; string sound // Use constructors to set appropriate 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. Write a function that has one parameter, a derived pointer The function void polyNoise( baseClass * ptrBase ) { cout ptrBase->animalSound;} //Note polymorphis - This one line of code should work for ALL/Any of the different derived classes. Call the function 4 times, pass each animal derived pointer to it and the output should be the correct animal sound.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
