Question
USE C++ Polymorphism and Exceptions For this lab we will continue working with the shapes developed for Lab 6. (4 pts) Implementing Polymorphism Use the
USE C++
Polymorphism and Exceptions
For this lab we will continue working with the shapes developed for Lab 6.
(4 pts) Implementing Polymorphism
Use the classes from Lab 6 (Shape, Rectangle, Circle, and Square) to create polymorphism. Answer the questions below and implement the polymorphism in the appropriate location of the interface and implementation files.
- Which function(s) are you going to make polymorphic?
- How will you make it polymorphic?
- Can it be a pure virtual function?
- Which class have you made an abstract base class?
(3 pts) Testing Polymorphism
Modify your application.cpp file to show polymorphism by making a function that prints the information of a shape. In this function, you will print the name, color, and area of the shape. You should pass your shape by reference (or address explicitly) to have polymorphism.
void print_shape_info(Shape &);
(3 pts) Exceptions
After you've convinced yourself and the TAs that you have successfully implemented polymorphism for your shapes, it is time to add some error handling. Since constructors don't return anything, throwing an exception during object construction is one of the best ways to signal an error with the construction.
Throw an exception in your constructors if the user does not provide a valid argument for their member variables. The constructors should throw an invalid_argument exception, when the user tries to create an object that would result in an area of zero. You'll need to add the statement below to your function
throw invalid_argument("Invalid constructor argument");
First, run your program with trying to pass an invalid argument to your constructor to see what it does now that your function throws an exception, and you are not catching it.
Now, catch the exception so that it doesnt have an error! Remember, you can use the what() member function to see your message from the invalid argument exception.
This is lab 6
The following is a list of header and implementation files of Shape, Rectangle and Circle
//Shape.h #ifndef SHAPE_H #define SHAPE_H #include
float area(); }; #endif SHAPE_H
------------------------------------------------------------------------------------------------------------------------
//Shape.cpp #include
float Shape::area() { return 0; }
------------------------------------------------------------------------------------------------------------------------
//Rectangle.h #ifndef RECT_H #define RECT_H #include
//overload >, (Rectangle ); bool operator
}; #endif RECT_H
------------------------------------------------------------------------------------------------------------------------
//Rectangle.cpp #include ------------------------------------------------------------------------------------------------------------------------ //Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include ------------------------------------------------------------------------------------------------------------------------ //Circle.cpp #include ------------------------------------------------------------------------------------------------------------------------ //Test.cpp #include //create two Rectangle objects of name, color ,width and height Rectangle r1("Rectangle1","Blue",5,5); Rectangle r2("Rectangle2","Red",6,6); cout //checking if r1 r2) cout //create two circle objects of name, color and radius Circle c1("Circle1","Blue",5); Circle c2("Circle2","Red",6); //display area of c1 and c2 cout //pause program output on console system("pause"); return 0; } ------------------------------------------------------------------------------------------------------------------------ Sample Output:
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