C++

3. Write an abstract class named Animal that has an integer variable named speed and a pure virtual member function named mov

3. Write an abstract class named Animal that has an integer variable named speed and a pure virtual member function named move. This function will display the nature and speed of the animal movement. Implementations of the function move will be left to the subclasses Crocodile, Cheetah, Kangaroo, and Dolphin. The message displayed by the function move is as follows: a. If the object is a Crocodile: b. If the object is a Cheetah: c. If the object is a Kangaroo: d. If the object is a Dolphin: “Crocodiles crawl at speed Si” “Cheetahs run at speed S2” “Kangaroos jump at speed 53” “Dolphins swim at speed 34” where S1, S2, S3, and S4 are the speeds entered by the user for a crocodile, a cheetah, a kangaroo, and a dolphin, respectively. In your main program, create an array of pointers to animals of various types (example: animalArray() ); then using the following polymorphic code, your program should display different messages as illustrated in Figure 4. for (int j = 0; j < NUM ANIMALS; j++) animalArray[j]->move(); Enter the crocodile’s speed (mph) : 20 Enter the cheetah’s speed (mph) : 75 Enter the kangaroo’s speed (mph): 44 Enter the dolphin’s speed (mph) : 37 Crocodiles crawl at speed 20 Cheetahs run at speed 75 Kangaroos jump at speed 44 Dolphins swim at speed 37 Figure 4



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *