Computers and Technology

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

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 18:00, alexj29227405
Write a method named addall that could be placed inside the hashintset class. this method accepts another hashintset as a parameter and adds all elements from that set into the current set, if they are not already present. for example, if a set s1 contains [1, 2, 3] and another set s2 contains [1, 7, 3, 9], the call of s1.addall(s2); would change s1 to store [1, 2, 3, 7, 9] in some order. you are allowed to call methods on your set and/or the other set. do not modify the set passed in. this method should run in o(n) time where n is the number of elements in the parameter set passed in.
Answers: 2
image
Computers and Technology, 22.06.2019 20:40, wutdmgamerz
Assume that there is a 4% rate of disk drive failure in a year. a. if all your computer data is stored on a hard disk drive with a copy stored on a second hard disk drive, what is the probability that during a year, you can avoid catastrophe with at least one working drive? b. if copies of all your computer data are stored on three independent hard disk drives, what is the probability that during a year, you can avoid catastrophe with at least one working drive?
Answers: 1
image
Computers and Technology, 24.06.2019 09:30, bhadd4385
What is the definition of digital literacy?
Answers: 1
image
Computers and Technology, 24.06.2019 12:30, stephanieanaya7
Why does the pc send out a broadcast arp prior
Answers: 1
Do you know the correct answer?
Virtual function to ENABLE the later selection of desired function in polymorphism

Creat...

Questions in other subjects:

Konu
English, 13.09.2021 14:00
Konu
Mathematics, 13.09.2021 14:00