Question
1. Create a class called Mammal. All mammals have a weight, so its data should be the mammals weight. Provide a default constructor that sets
1. Create a class called Mammal. All mammals have a weight, so its data should be the mammals weight. Provide a default constructor that sets the mammals weight to 0, and another constructor that allows the weight to be set by the client. To allow tracking of how your code executes, your constructors 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 to be accessed.
2. From the Mammal class derive Dog, Cat, Horse, and Pig classes. The derived classes should each have a constructor that outputs an appropriate message (e.g., the Dog constructor outputs Invoking Dog constructor). The derived classes should each have a data member to hold the animals name, and they should each have a method called Speak that overrides the Mammal Speak method. Dog Speak should output Woof, Cat Speak should output Meow, Horse Speak should output Im Mr. Ed, and Pig Speak should output Oink. The class should include Get and Set methods to allow the name to be accessed.
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., dont take a shortcut to using dynamic memory allocation 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: theyre 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) using the constructor that allows you to initialize the weight (use the randomly calculated weight). Save the object (Think array).
c. Repeat steps a and b five 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 five selections, execute another loop that cycles through the five selections and invokes the Speak method and also displays the name and weight for the animal. If youve done it properly, each of your outputs will correspond to the type of Mammal the user selected in the order they entered them.
e. Finally, figure out how to have the Mammal speak, and add the code that causes it to happen.
C++
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started