Question

In: Computer Science

Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y...

Programming Language: C++

Create a base class called Shape which has 2 attributes: X and Y (positions on a Cartesian coordinate system). Since a shape is amorphous, set the class up so that an object of type Shape can not be instantiated.

Create three derived classes of your choice whose base class is Shape. These derived classes should have accessors/mutators for their class specific attributes, as well as methods to compute the area and the perimeter of the shape.

In main(), create a stack of pointers to Shape and push one object of each derived class onto the stack. Then, pop each shape pointer off of the stack and output that shape's area and perimeter, demonstrating polymorphism.

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <stack>
using namespace std;
const double pi = 22.0/7.0;
class Shape
{
public:
   string color;
   bool filled;
   Shape();
   Shape(string color,bool filled){
       this->color = color;
       this->filled =filled;
   }
   string getColor(){
       return this->color;
   }
   void setColor(string color){
       this->color = color;
   }
   bool isFilled(){
       return this->filled;
   }
   void setFilled(bool filled){
       this->filled = filled;
   }
   string toString(){
       return "Color : "+this->color+" filled : "+((this->filled)?"True ":"False")+"\n";
   }
   virtual double getArea() = 0;
   virtual double getPerimeter() = 0;

};
class Circle:public Shape
{
public:
   double radius;
   Circle();
   Circle(double radius,string color,bool filled) : Shape(color,filled){
       this->radius = radius;
   }
   double getRadius(){
       return this->radius;
   }
   void setRadius(double radius){
       this->radius = radius;
   }
   double getDiagmeter(){
       return this->radius*2;
   }
   double getArea(){
       return this->radius * this->radius * pi;
   }
   double getPerimeter(){
       return 2*this->radius*pi;
   }

};

class Rectangle:public Shape
{
public:
   double width;
   double height;
   Rectangle();
   Rectangle(double width,double height, string color,bool filled) : Shape(color,filled){
       this->width = width;
       this->height = height;
   }
   double getWidth(){
       return this->width;
   }
   void setWidth(double width){
       this->width = width;
   }
   double getHeight(){
       return this->height;
   }
   void setHeight(double height){
       this->height = height;
   }
   double getArea(){
       return this->width * this->height;
   }
   double getPerimeter(){
       return 2*(this->width + this->height);
   }
};
class Square:public Shape
{
public:
   double side;
   Square();
   Square(double side, string color,bool filled) : Shape(color,filled){
       this->side = side;
   }
   double getSide(){
       return this->side;
   }
   void setSide(double side){
       this->side = side;
   }
   double getArea(){
       return this->side * this->side;
   }
   double getPerimeter(){
       return 4*(this->side);
   }
};
int main(){
   stack<Shape *> shapes;
   Shape *a = new Circle(2.3,"blue",true);
   Shape *b = new Rectangle(2,5,"black",false);
   Shape *c = new Square(5,"red",true);
   shapes.push(a);
   shapes.push(b);
   shapes.push(c);
   while(!shapes.empty()){
       Shape *shape = shapes.top();
       cout<<shape->getArea()<<endl;
       cout<<shape->getPerimeter()<<endl;
       shapes.pop();
   }
}


Related Solutions

C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
C# programming Create a class called A with private integer field x, protected integer field y,...
C# programming Create a class called A with private integer field x, protected integer field y, public integer field z. Create a class B derived from class A with public integer field d and protected integer field e and private field f. Write a main (in a THIRD class called Program) that create an object B and assign all publicly accessible fields of the object with value of 1. Which fields will have a value of 1? Create a method...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
(In Matlab) Create a base class named Point that has the properties for x and y...
(In Matlab) Create a base class named Point that has the properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class, the x and y properties represent the center coordinates of a circle. The methods of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points. The derived class should have...
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer...
Programming Problem 2 - Cycle [A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list. [B] Edit your class...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class...
Language C++ Student-Report Card Generator: create a class called student and class called course. student class this class should contain the following private data types: - name (string) - id number (string) - email (s) - phone number (string) - number of courses (int) - a dynamic array of course objects. the user will specify how many courses are there in the array. the following are public members of the student class: - default constructor (in this constructor, prompt the...
hello i have question in c++ language Q1: create a class called RightTriangleShape, and it has...
hello i have question in c++ language Q1: create a class called RightTriangleShape, and it has data member called height which initialized to 3 by the constructor of the class. It has also the following function members: Void setHeight() to read, and set the height. Void getHeight() to print the value of height. void leftBottom_RTraingle(), prints shape a void leftTop_RTraingle(), prints shape b. void RightTop_RTraingle(), prints shape c. void RigtBottom_RTraingle(), prints shape d. The functions from 3 to 6 have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT