Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammals weight and name. Provide

1. Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal’s weight and name. Provide a default constructor that sets the mammal’s weight to 0 and name to null, and another constructor that allows the weight and name to be set by the client. To allow tracking of how your code executes, your constructors and destructors should each output a message when they are called (e.g., “Invoking Mammal default constructor” for the default constructor). Your Mammal class should also have a method called Speak that outputs a message (e.g., “Mammal is speaking”) when called. Your class should also have Get and Set methods to allow the weight and name to be accessed. 2. From the Mammal class, derive Dog, Cat, Horse, and Pig classes. The derived classes should each have constructor and destructors that output an appropriate message (e.g., the Dog constructor outputs “Invoking Dog constructor”), and that allow the weight and name of the Mammal to be initialized (think member initialization list). The derived classes should each have a member function called Speak that overrides the Mammal Speak version. Dog Speak should output “Woof,” Cat Speak should output “Meow,” Horse Speak should output “I’m Mr. Ed,” and Pig Speak should output “Oink.” 3. Write a main function that uses the Mammal 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 to generate a random weight between 0 and 150 pounds. (Think modulus. Note: they are small horses and pigs.) Your program should use a seed value of 100 and set the seed only once. b. Prompt the user to make an animal selection (e.g., (1) for dog, (2) for cat, (3) for horse, and (4) for pig) and to enter a name for the animal. Dynamically create a Dog, Cat, Horse, or Pig object (depending on what the user entered) and initialize it with a constructor to which is passed its weight and name. Save the object (think array). c. 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. d. After the user has entered all 5 selections, execute another loop that cycles through the 5 selections and invokes the Speak method and also displays the name and weight for the animal. If you have done it properly, each of your outputs will correspond to the type of Mammal the user selected in the order he or she entered them. e. Finally, figure out how to have the Mammal “speak,” and add the code that causes it to happen.

Step by Step Solution

3.44 Rating (131 Votes )

There are 3 Steps involved in it

Step: 1

Include Includes Include Using stdpace naming Class Protect String name Weight int Public Mammals Name Weight 0 cout Call default mammal builder endl ... blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistics For Psychology

Authors: Arthur Aron, Elaine N. Aron, Elliot J. Coups

6th Edition

205258158, 978-0205258154

More Books

Students also viewed these Programming questions

Question

Describe the BellMagendie Law and how it was discovered.

Answered: 1 week ago

Question

13.18 Identify risk factors for suicide in adolescents.

Answered: 1 week ago