Question
1. construct a simple shape hierarchy: a base class called Shape and derived classes called Circle, Square, and Triangle. In the base class, make a
1. construct a simple "shape" hierarchy: a base class called Shape and derived classes called Circle, Square, and Triangle. In the base class, make a virtual function called draw( ), and override this in the derived classes. Make an array of pointers to Shape objects that you create on the heap (and thus perform upcasting of the pointers), and call draw( ) through the base-class pointers, to verify the behavior of the virtual function. If your debugger supports it, single-step through the code.
2. Modify Exercise 1 so draw( ) is a pure virtual function. Try creating an object of type Shape. Try to call the pure virtual function inside the constructor and see what happens. Leaving it as a pure virtual, give draw( ) a definition.
3. Expanding on Exercise 2, construct a function that takes a Shape object by value and try to upcast a derived object in as an argument. See what happens. Fix the function by taking a reference to the Shape object.
4. construct an inheritance hierarchy of Rodent: Mouse, Gerbil, Hamster, etc. In the base class, provide methods that are common to all Rodents, and redefine these in the derived classes to perform different behaviors depending on the specific type of Rodent. construct an array of pointers to Rodent, fill it with different specific types of Rodents, and call your base-class methods to see what happens.
5. Modify Exercise 4 so that you use a vector instead of an array of pointers. Make sure that memory is cleaned up properly.
6. Starting with the previous Rodent hierarchy, inherit BlueHamster from Hamster (yes, there is such a thing; I had one when I was a kid), override the base-class methods, and show that the code that calls the base-class methods doesn't need to change in order to accommodate the new type.
7. Starting with the previous Rodent hierarchy, add a non virtual destructor, construct an object of class Hamster using new, upcast the pointer to a Rodent*, and delete the pointer to show that it doesn't call all the destructors in the hierarchy. Change the destructor to be virtual and demonstrate that the behavior is now correct.
8. Starting with the previous Rodent hierarchy, modify Rodent so it is a pure abstract base class.
9. construct an air-traffic control system with base-class Aircraft and various derived types. construct a Tower class with a vector that sends the appropriate messages to the various aircraft under its control.
10.construct a model of a greenhouse by inheriting various types of Plant and building mechanisms into your greenhouse that take care of the plants.
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