Create a base class called Animal. All animals have a name (i.e. “Fido,” “Thumper,” “Princess,” “Nemo,” etc.) and an age. Provide a default constructor that initializes the age to zero and outputs the message “Invoking the default Animal constructor” and another constructor that allows the name and age to be set by the client. This other constructor should also output the message “Invoking the 2-argument Animal constructor.” Also construct a destructor for this class that outputs the message “Invoking the default Animal destructor.” Your Animal class should have a function called Move that cannot be implemented. That is, it should be declared as a purely virtual function. Your class should also have Get and Set methods to allow the name and age to be accessed. From the Animal class, derive Dog, Rabbit, Fish, and Snake classes. The derived classes should each have constructors and destructors that output an appropriate message (e.g., the Dog constructor outputs “Invoking Dog constructor,” and the Dog destructor outputs “Invoking Dog destructor”). The constructor of each derived class should allow the name and age of the Animal to be set (think member initialization list). The derived classes should each have a member function called Move that overrides the Animal Move version. Dog Move should output “I run,” Rabbit Move should output “I hop,” Fish Move should output “I swim,” and Snake Move should output “I slither.” Write 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). Use the rand() function to generate a random age between 1 and 20 years. Your program should use a seed value of 100 and set the seed only once. Each animal should have its own randomly generated age. (i.e. Don’t generate one age and use it for all of the animals.) Prompt the user to make an animal selection [e.g. (1) for dog, (2) for rabbit, (3) for fish, and (4) for snake] and to enter a name for the animal. Dynamically create a Dog, Rabbit, Fish, or Snake object (depending on what the user entered) and initialize it with a constructor to which is passed its name and age. Save the object (use an array). Repeat steps a. and b. 4 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. After the user has entered all 5 selections, execute another loop that cycles through the 5 selections and invokes the Move function and also displays the name and age of the animal. 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.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Can I get help writing this?
- Create a base class called Animal. All animals have a name (i.e. “Fido,” “Thumper,” “Princess,” “Nemo,” etc.) and an age. Provide a default constructor that initializes the age to zero and outputs the message “Invoking the default Animal constructor” and another constructor that allows the name and age to be set by the client. This other constructor should also output the message “Invoking the 2-argument Animal constructor.” Also construct a destructor for this class that outputs the message “Invoking the default Animal destructor.” Your Animal class should have a function called Move that cannot be implemented. That is, it should be declared as a purely virtual function. Your class should also have Get and Set methods to allow the name and age to be accessed.
- From the Animal class, derive Dog, Rabbit, Fish, and Snake classes. The derived classes should each have constructors and destructors that output an appropriate message (e.g., the Dog constructor outputs “Invoking Dog constructor,” and the Dog destructor outputs “Invoking Dog destructor”). The constructor of each derived class should allow the name and age of the Animal to be set (think member initialization list). The derived classes should each have a member function called Move that overrides the Animal Move version. Dog Move should output “I run,” Rabbit Move should output “I hop,” Fish Move should output “I swim,” and Snake Move should output “I slither.”
- Write 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).
- Use the rand() function to generate a random age between 1 and 20 years. Your program should use a seed value of 100 and set the seed only once. Each animal should have its own randomly generated age. (i.e. Don’t generate one age and use it for all of the animals.)
- Prompt the user to make an animal selection [e.g. (1) for dog, (2) for rabbit, (3) for fish, and (4) for snake] and to enter a name for the animal. Dynamically create a Dog, Rabbit, Fish, or Snake object (depending on what the user entered) and initialize it with a constructor to which is passed its name and age. Save the object (use an array).
- Repeat steps a. and b. 4 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.
After the user has entered all 5 selections, execute another loop that cycles through the 5 selections and invokes the Move function and also displays the name and age of the animal. 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.
****Output must look like picture attached*****
Step by step
Solved in 3 steps with 2 images