Question: 1. Define the class Shape with two private data members width and height (both double). The Shape class should have a default constructor as well
1. Define the class Shape with two private data members width and height (both double). The Shape class should have a default constructor as well as constructor with parameters. The class should have get and set methods for its private data members. The Shape class must also have a method called area() that returns an integer. For the implementation of the area() method simply use cout to print the base class and return 0.
a. Include the exception handling routine to set methods for width and height. The set methods should throw runtime_error exception with the message bad input.
b. Define the class Rectangle derived from shape. Rectangle has no additional private data members. But it overrides the method area() in Shape class by computing and returning the area of the rectangle.
c. Define the class Triangle derived from shape. Triangle has no additional private data members. But it overrides the method area() in Shape class by computing and returning the area of the Triangle. The Triangle object interprets the width as the base of the triangle b and height as the height h of the triangle. The area is given by b*h/2.
d. Call the set method of the shape class to set its value to a negative number. Catch the exception. No need to allow user to reenter value.
e. Demonstrate polymorphism for the classes shape, rectangle, and Triangle. Note that the class has no print method, just call and print the value returned by the area() method when demonstrating polymorphism.
2. Write a function template displayList() that receives a list container as a parameter and prints its elements. Write a main program to test displayList() by passing it the list created in part 1.
3. Write a function template displayVector() that receives a vector container as a parameter and prints its elements. Write a main program to test displayVector() by passing it the vector created in part 1.
In problem 2 and 3 we had to write two function templates that really did the same thing, which was printing containers. Use the for_each() method to implement a strategy that can print both a vector and a list.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
