subject

Virtual function to ENABLE the later selection of desired function in polymorphism

Create an animal base class.

string animalName;

string sound

virtual animalSound() { cout << AnimalName << " says " << sound << endl;

Create 4 SETUP 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 switch statement that prints out the animal noise, depending on which animal is chosen.

prompt the user for the type of animal, compare with switch statement, and print out animal sound.

Part 2 - Funciton

(Base and Derived classes should already be coded in part 1.

Create an animal base class.

string animalName;

string 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.

Part 3 - loop

(Base and Derived classes should already be coded in part 1.

Create an animal base class.

string animalName;

string 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.

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;
}

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 09:00, 19elbatawisaly
What is one way in which tablets differ from laptops and notebooks? tablets are designed for touch-based interaction. tablets are designed to be used as desktops. tablets are designed for input via a keyboard and mouse. tablets are designed to be larger than laptops.
Answers: 1
image
Computers and Technology, 22.06.2019 21:00, depression4eternity
The average cost of one year at a private college in 2012-2013 is $43,289. the average grant aid received by a student at a private college in 2012-2013 is $15,680.   what is the average student contribution for one year at a private college in 2012-2013?
Answers: 3
image
Computers and Technology, 23.06.2019 00:30, Thisisdifinite
Which of the following would you find on a network
Answers: 3
image
Computers and Technology, 23.06.2019 12:00, trippie4life
3. when you right-click a linked spreadsheet object, what commands do you choose to activate the excel features? a. linked worksheet object > edit b. edit data > edit data c. linked spreadsheet > edit d. object > edit data
Answers: 2
You know the right answer?
Virtual function to ENABLE the later selection of desired function in polymorphism

Creat...

Questions in other subjects:

Konu
Mathematics, 14.06.2021 08:50
Konu
Mathematics, 14.06.2021 08:50
Konu
Social Studies, 14.06.2021 08:50