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 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
Get step-by-step solutions from verified subject matter experts
