Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include using namespace std; class Shape { private: string name; string color; public: Shape(string name, string color); void setName(string); void setColor(string); string getName(); string getColor();

float area(); }; #endif SHAPE_H

------------------------------------------------------------------------------------------------------------------------

//Shape.cpp #include #include #include "Shape.h" using namespace std; Shape::Shape(string name, string color) { setName(name); setColor(color); } void Shape::setName(string name) { this->name=name; } void Shape::setColor(string color) { this->color=color; } string Shape::getName() { return name; } string Shape::getColor() { return color; }

float Shape::area() { return 0; }

------------------------------------------------------------------------------------------------------------------------

//Rectangle.h #ifndef RECT_H #define RECT_H #include #include "Shape.h" class Rectangle : public Shape { private: float width; float height; public: Rectangle(string name, string color,float width, float height) ; float area();

//overload >, (Rectangle ); bool operator

}; #endif RECT_H

------------------------------------------------------------------------------------------------------------------------

//Rectangle.cpp #include #include #include "Rectangle.h" using namespace std; Rectangle::Rectangle(string name, string color,float width, float height) : Shape(name,color) { this->width=width; this->height=height; } float Rectangle::area() { return width*height; } bool Rectangle::operator> (Rectangle other) { return this->area()>other.area(); } bool Rectangle::operatorarea()

------------------------------------------------------------------------------------------------------------------------

//Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include #include "Shape.h" class Circle : public Shape { private: float radius; public: Circle(string name, string color,float radius); float area(); }; #endif CIRCLE_H

------------------------------------------------------------------------------------------------------------------------

//Circle.cpp #include #include #include "Circle.h" using namespace std; Circle::Circle(string name, string color,float radius) :Shape(name,color) { this->radius=radius; } float Circle::area() { return 3.1415*radius*radius; }

------------------------------------------------------------------------------------------------------------------------

//Test.cpp #include #include #include #include "Shape.h" #include "Circle.h" #include "Rectangle.h" using namespace std; int main() {

//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:

image text in transcribed

Rectangle1 area is 25.00 Rectangle2 area is 36.00 Rectangle 1 is less than Rectangle2 Circle1 area is 78.54 Circle2 area is 113.09 Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Are there any questions that you want to ask?

Answered: 1 week ago