Make a main function that use the animal and derived classes


Assignment: C++ Programming- Polymorphism

Overview

The objective of this is to give you some practice using inheritance, virtual functions, and polymorphism. With a focus on dynamic binding, this program shifts the focus from design-time static binding of functions to objects to run-time (or dynamic binding). It leverages the use of virtual functions and introduces the concept of abstract and concrete classes in its implementation of pure virtual functions as it also demonstrates polymorphic behavior.

Instructions

A. Make a base class called Animal.

a. All Animals belong to an Order i.e. "Artiodactyla" (Antelope), "Artiodactyla" (Buffalo), "Casuariformes" (Long Eared Owl), "Dasyuromorphia" (Tasmanian Devil), etc., and have an age that is measured in years, including the decimal portion (use a double data type).

b. Provides a default constructor that initializes the age to zero, the Order and CommonName to "", and outputs the message "Invoking the default Animal constructor"

c. Provides another constructor that allows the CommonName, Order, and Age to be set by the client when the object is created. This other constructor should also output the message "Invoking the 3-argument Animal constructor."

d. Provides a destructor for this class that outputs the message "Invoking the default Animal destructor."

e. Provides a function called Eat() that cannot be implemented. That is, it should be declared as a purely virtual function.

f. Provides Get and Set methods to allow the CommonName, Order, and Age to be accessed.

B. From the Animal class, derive Antelope, Buffalo, Long_Eared_Owl, and Tasmanian_Devil classes.

a. The derived classes should each have constructors and destructors that output an appropriate message (e.g., the Antelope constructor outputs "Invoking Antelope constructor," and the Antelope destructor outputs "Invoking Antelope destructor").

The constructor of each derived class should allow the CommonName, Order, and Age of the Animal to be passed directly to the constructor of the base class.

Make a main function that uses the Animal and derived classes as needed to do the following. You must perform the actions below in the sequence described (i.e., do not take a shortcut around using dynamic memory allocation/deallocation and virtual methods since they are the whole point of the lab).

A. Use the rand() function in a formula to generate a random age in years including a decimal portion based on the Animal selected.

B. The range of ages for each Animal is as follows:

a. Antelope: 10.5 to 25.9 years
b. Buffalo: 15.3 to 22.1 years
c. Long Eared Owl: 40.0 to 60.0 years
d. Tasmanian Devil: 5.3 to 8.7 years

C. As a refresher on the rand() function, recall that it generates an integer between 0 and the largest integer that can be stored. If you don't want the low end of the range to start at 0, you add a number to the rand() function that represents the smallest number in the range of numbers you want to generate. This is the offset. Using the modulus operator (%), you can specify the interval.

For example, to generate a random number between 10.5 and 25.9, you can use this formula (note that since rand() generates an integer, I am multiplying the desired offset and interval by 100 and then dividing the final result by 100.0)

double randomNumber = ((rand() % interval ) + (offset))/100.0
randomNumber = ((rand() % ((15.4*100)+1)) + (10.5*100))/100.0
where the interval of 15.4 = 25.9-10.5
"1" is added to the 15.4 * 100 to make the range inclusive on the upper end.

Although not for this assignment...

An alternative to using the rand() function is to use the uniform_real_distribution class, which is described in section 13-6 of your textbook. It's actually much easier to use and more robust in terms of generating random numbers that are more random. The rand() function is somewhat predictable, but it's still used in programs today. It's always good to know both techniques, though. In this assignment, though, to receive credit for generating the random number, you must use the rand() function.

A. Your program must use a seed value of 100 for the random number generator. You should set the seed only once at the beginning of main(). Use the srand() function to set the seed.

B. Prompt the user to make an Animal selection, e.g. (i) for Antelope, (ii) for Long Eared Owl, (iii) for Buffalo, and (iv) for Tasmanian.

a. Enter an Order for the Animal.

b. Dynamically make an Antelope, Long Eared Owl, Buffalo, or Tasmanian Devil object (depending on what the user entered) and initialize it with a constructor to which is passed its CommonName, Order, and Age.

c. Save the object (use an array called "Animals" - credit will not be awarded if you use a vector for this).

Note: You may hardcode the size of the array to hold 3 elements. That is, you don't need to dynamically create this array unless you want to. If you do dynamically allocate it, though, be sure to deallocate its memory properly.

C. Repeat step 2(prompt user to make an animal selection) above 2 more times.

You do not know what Animals the user will select or in what order, so you must figure out how to create and store the appropriate objects. Notice that there are 4 Animals from which to choose, but the user will only make 3 selections. Therefore, one Animal will not be chosen.

D. After the user has entered all 3 selections, execute another loop that cycles through the 3 selections and displays the information about the selected Animal.

a. The first line will display the CommonName.

b. The second line will display the order and age.

c. The third line will invoke the Eat() function.

If you have done it properly, each of your outputs will correspond to the type of Animal the user selected in the order he or she entered them.

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Make a main function that use the animal and derived classes
Reference No:- TGS03354365

Expected delivery within 24 Hours