CSIS 112: Lab 8—Mammals

$30.00

Category: Tags: , , , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (3 votes)

This lab’s goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab, implement the following steps:

 

  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 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 weight and name to be accessed.

 

  1. 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.”

 

  1. 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).

 

  1. 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.
  2. 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).
  3. 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.
  4. 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.

 

 

Deliverables:

  • Complete the programming assignment described above and submit your completed assignment in accordance with the lab submission policies.