Question: 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
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?
(1!!!!!!!!!!!!!!!!!!!!!!!How to add Implementing Polymorphism in this program!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! )C++
//application.cpp
#include
#include "rectangle.h"
#include "circle.h"
#include "square.h"
using namespace std;
int main()
{
Shape *shape;
Rectangle rectangle("rectangle","blue",2,3);
Circle circle ("circle","red",3);
Square square ("square","yellow",4);
try{
Rectangle throw_rectangle("rectangle","blue",0,3);
}catch (const invalid_argument& e){
cout << e.what()<
}
try
{
Circle throw_circle("circle","red",0);
}catch (const invalid_argument& e){
cout << e.what()<
}
try
{
Square throw_square("square","yellow",0);
}catch (const invalid_argument& e){
cout<
}
shape = &rectangle;
shape->print_shape_info(*shape);
shape = &circle;
shape->print_shape_info(*shape);
shape =
shape->print_shape_info(*shape);
return 0;
}
//circle.cpp
#include
#include "shape.h"
#include "circle.h"
using namespace std;
Circle::Circle(string n, string c, float r) : Shape(n,c){
radius = r;
if(radius == 0)
throw invalid_argument("Invalid constructor argument");
}
float Circle::area() const{
return (pow(radius, 2) * 3.14);
}
~
//circle.h
#include
using namespace std;
class Circle : public Shape{
private:
float radius;
public:
Circle(string,string,float);
float area() const;
};
//rectangle.cpp
#include
using namespace std;
Rectangle::Rectangle(string n, string c,float w,float h) : Shape (n,c){
this->width =w;
this->height =h;
if(width == 0 || height == 0)
throw invalid_argument("Invlid constructor argument");
}
float Rectangle::area()const{
return (width * height);
}
// rectangle.h
#include
using namespace std;
class Rectangle : public Shape{
private:
float width;
float height;
public:
Rectangle(string,string,float,float);
float area() const;
};
//shape.cpp
#include
Shape::Shape(string n,string c){
this->name = n;
this->color = c;
}
float Shape::area() const {
return 0;
}
void Shape::set_name(string aName){
name = aName;
}
void Shape::set_color(string aColor){
color = aColor;
}
string Shape::get_color()const{
return color;
}
string Shape::get_name()const{
return name;
}
void Shape::print_shape_info(Shape &shape){
cout <<"Shape name: "<< shape.name << endl;
cout <<"Shape color: "<< shape.color << endl;
cout <<"Shape area: "<< shape.area() << endl;
}
//shape.h
#include
using namespace std;
class Shape {
private:
string name;
string color;
public:
Shape (string, string);
string get_name()const;
string get_color()const;
void set_name(string);
void set_color(string);
float area()const;
void print_shape_info(Shape &);
};
//square.cpp
#include
using namespace std;
Square::Square(string n,string c,float s) : Rectangle(n,c,s,s){}
//square.h
#include
using namespace std;
class Square : public Rectangle{
public:
Square(string,string,float);
};
// ///////// Lab 6 ////////////////////
//application.cpp
#include "shape.h" #include "rectangle.h" #include "circle.h" #include "square.h"
int main() { Shape s; s.setColor("green"); s.setName("Test"); cout << s.getName() << " " << s.getColor() << " " << s.area() << endl;
Rectangle r(3.0, 5.0); r.setColor("red"); r.setName("Test2"); cout << r.getName() << " " << r.getColor() << " " << r.getHeight() << " " << r.getWidth() << " " << r.area() << endl;
Circle c(3.0); c.setColor("blue"); c.setName("Test3"); cout << c.getName() << " " << c.getColor() << " " << c.getRadius() << " " << c.area() << endl;
Square sq(4.0); sq.setColor("yellow"); sq.setName("Test4"); cout << sq.getName() << " " << sq.getColor() << " " << sq.getHeight() << " " << sq.getWidth() << " " << sq.area() << endl;
sq.setWidth(5.0); cout << sq.getName() << " " << sq.getColor() << " " << sq.getHeight() << " " << sq.getWidth() << " " << sq.area() << endl;
Rectangle r2(2.0, 8.0); r2.setColor("purple"); r2.setName("Test5"); cout << r2.getName() << " " << r2.getColor() << " " << r2.getHeight() << " " << r2.getWidth() << " " << r2.area() << endl;
cout << (r < r2) << endl; cout << (r > r2) << endl; }
//circle.cpp
#include #include "circle.h"
Circle::Circle(float aRadius) { radius = aRadius; }
float Circle::area() const { return M_PI * radius * radius; }
void Circle::setRadius(float aRadius) { radius = aRadius; } float Circle::getRadius() const { return radius; }
//circle.h
#include "shape.h"
class Circle : public Shape { private: float radius;
public: Circle(float aRadius); float area() const; float getRadius() const; void setRadius(float aRadius); };
//rectangle.cpp
#include "rectangle.h"
Rectangle::Rectangle(float aWidth, float aHeight) { width = aWidth; height = aHeight; }
float Rectangle::area() const { return width*height; }
void Rectangle::setWidth(float aWidth) { width = aWidth; }
void Rectangle::setHeight(float aHeight) { height = aHeight; }
float Rectangle::getWidth() const { return width; }
float Rectangle::getHeight() const { return height; }
bool operator>(const Rectangle &r1, const Rectangle &r2) { return r1.area() > r2.area(); }
bool operator<(const Rectangle &r1, const Rectangle &r2) { return r1.area() < r2.area(); }
//rectangle.h
#ifndef RECTANGLE_H #define RECTANGLE_H #include "shape.h"
class Rectangle : public Shape { private: float width; float height;
public: Rectangle(float aWidth, float aHeight); float area() const; float getWidth() const; float getHeight() const; void setWidth(float aWidth); void setHeight(float aHeight); }; bool operator> (const Rectangle &r1, const Rectangle &r2); bool operator< (const Rectangle &r1, const Rectangle &r2); #endif
//shape.cpp
#include "shape.h"
float Shape::area() const { return 0; }
void Shape::setName(string aName) { name = aName; }
void Shape::setColor(string aColor) { color = aColor; }
string Shape::getColor() const { return color; }
string Shape::getName() const { return name; }
//shape.h
#ifndef SHAPE_H #define SHAPE_H
#include using namespace std;
class Shape { private: string name; string color; public: float area() const; string getName() const; string getColor() const; void setName(string name); void setColor(string color); };
#endif
//square.cpp
#include "square.h"
Square::Square(float aWidth) : Rectangle(aWidth, aWidth) { }
void Square::setWidth(float aWidth) { Rectangle::setWidth(aWidth); Rectangle::setHeight(aWidth); }
void Square::setHeight(float aHeight) { Rectangle::setWidth(aHeight); Rectangle::setHeight(aHeight); }
//square.h
#include "rectangle.h"
class Square : public Rectangle { public: Square(float aWidth); void setWidth(float aWidth); void setHeight(float aHeight); };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
