This the output I am supposedv to get but I am getteing many errors . Please help me
int main()
{
int size = 5;
Shape** shapes = new Shape*[size];
Point* centre = new Point(5,5);
Rectangle* rectangle1 = new Rectangle(*centre, 4, 3);
shapes[0] = rectangle1;
shapes[1] = new Rectangle (*new Point(10, -10), 4, 4);
Rectangle* rectangle3 = new Square(*new Point(-8.5,-10), 2.5);
shapes[2] = rectangle3;
Square* square2 = new Square (*new Point(-1,-1), 1.5);
shapes[3] = square2;
Circle* circle = new Circle(*new Point(100,100), 25.4);
shapes[4] = circle;
cout << "\n11111111111111111111111111\n";
for (int i = 0; i < size; ++i)
shapes[i] -> display();
cout << "\n22222222222222222222222222\n";
cout << "\nthe area of the first rectangle is: ";
cout << rectangle1->area() << endl;
cout << "It is a square, the area is: ";
Square* square = dynamic_cast<Square*> (rectangle3);
cout << square->area() << endl;
cout << "the perimeter of the circle is: ";
cout << circle->perimeter() << endl;
cout << "\n33333333333333333333333333\n";
cout << "\nThe four vertices of the first rectangle are: (clockwise from top-left)\n";
rectangle1->printFourVertices();
cout << "\nThe four vertices of the third rectangle are:\n";
rectangle3->printFourVertices();
cout << "\n44444444444444444444444444\n";
rectangle1->reflectX();
shapes[2] -> translate(1.5, 3);
shapes[4] -> translate(-100,-100);
cout << "\nAfter reflection and translation, here are the moved shapes:\n" ;
for (int i = 0; i < size; ++i)
shapes[i] -> display();
cout << "\n55555555555555555555555555\n";
cout << "\nNow, the four vertices of the first rectangle are:\n";
rectangle1->printFourVertices();
cout << "\nNow, the four vertices of the third rectangle are:\n";
rectangle3->printFourVertices();
cout << "\n66666666666666666666666666\n\n";
for (int i = 0; i < size; ++i)
cout << shapes[i] -> area() << endl;
cout << "\n777777777777777777777777777\n";
shapes[1] -> display();
dynamic_cast<Rectangle*> (shapes[1]) -> setLength(8.2);
cout << "\nAfter setLength(8.2), the rectangle is: ";
shapes[1] -> display();
dynamic_cast<Rectangle*> (shapes[1]) -> setWidth(5);
cout << "\nAfter setWidth(5), the rectangle is: ";
shapes[1] -> display();
square2 -> display();
square2 -> setLength(8.2);
cout << "\nAfter setLength(8.2), the rectangle is: ";
square2 -> display();
square2 -> setWidth(5);
cout << "\nAfter setWidth(5), the rectangle is: ";
square2 -> display();
system("pause");
return 0;
}
What I have tried:
#include "stdafx.h"
# include<iostream>
# include<cmath>
# include<string>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point();
Point(const double &x,const double &y);
void setX(const double &x);
void setY(const double &y);
double getX()const;
double getY()const;
double DisFromOrigin()const;
virtual void translate(const double &xDis, const double &yDis);
void reflectX(); void reflectY();
virtual void PrintName()const;
};
Point::Point()
{
x=0;
y=0;
}
Point:: Point(const double &x,const double &y)
{
this->x=x;
this->y=y;
}
void Point::setX(const double &x)
{
this->x=x;
this->y=y;
}
void Point:: setY(const double &y)
{
this->y =y;
}
double Point:: getX()const
{
return x;
}
double Point:: getY()const
{
return y;
}
double Point::DisFromOrigin()const
{
return (sqrt(x*x + y*y));
}
void Point:: translate(const double &xDis, const double &yDis)
{
double a;
double b;
a = this->x + xDis;
b= this->y + yDis;
this->x= a;
this->y=b;
}
void Point::reflectX()
{
this->y = -1 *y;
}
void Point::reflectY()
{
this->x= -1*x;
}
void Point::PrintName()const
{
cout<<"Point ("<<getX()<<" , "<<getY()<<")"<<endl;
}
class Shape
{
public:
virtual double Peri()const=0;
virtual void PrintName()const=0;
virtual void translate(double x1,double y1)=0;
virtual void reflectX()=0;
virtual void reflectY()=0;
virtual double getArea()const=0;
virtual double getPeri()=0;
virtual string getName()const=0;
}
class Circle:public Shape
{
public:
Circle();
Circle(const Point &p, const double &r);
double getR()const;
void setR(const double &r);
virtual double getPeri()const;
virtual void PrintName()const;
virtual void translate(const double &x1, const double &y1);
virtual void reflectX();
virtual void reflectY();
virtual double getArea()const;
virtual string getName()const;
protected:
Point center;
string name;
double radius;
};
Circle::Circle()
{
name = "Circle";
center = Point(0,0);
radius = 1;
}
Circle::Circle(const Point &p, const double &r)
{
name = "Circle";
this->center= p;
this->radius=r;
}
double Circle::getPeri()const
{
return (2*3.14*getR());
}
double Circle::getR()const
{
return radius;
}
void Circle::setR(const double &r)
{
this->radius = r;
}
void Circle:: PrintName()const
{
cout<<"Circle"<<endl;
}
void Circle::translate(const double &x1, const double &y1)
{
this->center.translate(x1 , y1);
}
void Circle::reflectX()
{
this->center.reflectX();
}
void Circle::reflectY()
{
this->center.reflectY();
}
double Circle::getArea()const
{
return (3.14*this->getR()*this->getR());
}
string Circle::getName()const
{
return this->name;
}
class Rectangle:public Shape
{
private:
double len;
double width;
Point center;
string name;
public:
Rectangle();
Rectangle(const Point &p, const double &len , const double &width);
void setLen(double len);
void setWid(double width);
double getLen()const;
double getWid()const;
virtual double getPeri()const;
virtual void PrintName()const;
virtual void translate(const double &x1, const double &y1);
virtual void reflectX();
virtual void reflectY();
virtual double getArea()const;
virtual string getName()const;
};
Rectangle::Rectangle()
{
len =1;
width=1;
this->name = "Rectangle";
center = Point(0,0);
}
Rectangle:: Rectangle(const Point &p, const double &len , const double &width)
{
this->name ="Rectangle";
this->center = p;
this->len = len;
this->width = width;
}
double Rectangle:: getPeri()const
{
return (2*getLen() + 2*getWid());
}
void Rectangle::PrintName()const
{
cout<<"Rectangle"<<endl;
}
void Rectangle::translate(const double &x1, const double &y1)
{
this->center.translate(x1, y1);
}
void Rectangle::reflectX()
{
this->center.reflectX();
}
void Rectangle::reflectY()
{
this->center.reflectY();
}
double Rectangle::getArea()const
{
return(getLen()*getWid());
}
string Rectangle::getName()const
{
return name;
}
void Rectangle::setLen(double len)
{
this->len = len;
}
void Rectangle::setWid(double width)
{
this->width = width;
}
double Rectangle::getLen()const
{
return len;
}
double Rectangle::getWid()const
{
return width;
}
class Square:public Rectangle
{
private:
Point center;
string name;
double side;
public:
Square();
Square(const double &side);
void setSide(const double &side);
double getSide()const;
virtual double getPeri()const;
virtual void PrintName()const;
virtual void translate(const double &x1, const double &y1);
virtual void reflectX();
virtual void reflectY();
virtual double getArea()const;
virtual string getName()const;
};
Square::Square()
{
side=0;
}
Square::Square(const double &side)
{
this->side = side;
}
void Square::setSide(const double &side)
{
this->side = side;
this->Rectangle::setLen(side);
this->Rectangle::setWid(side);
}
double Square::getSide()const
{
return this->Rectangle::getLen();
}
double Square:: getPeri()const
{
return(this->Rectangle::getLen()* 4);
}
void Square::PrintName()const
{
cout<<"Square"<<endl;
}
void Square::translate(const double &x1, const double &y1)
{
this->Rectangle::translate(x1, y1);
}
void Square::reflectX()
{
this->Rectangle::reflectX();
}
void Square::reflectY()
{
this->Rectangle::reflectY();
}
double Square::getArea()const
{
return(this->Rectangle::getLen()* this->Rectangle::getLen());
}
string Square::getName()const
{
return name;
}
int _tmain(int argc, _TCHAR* argv[])
{
int size = 5;
Shape** shapes = new Shape*[size];
Point* centre = new Point(5,5);
Rectangle* rectangle1 = new Rectangle(*centre, 4, 3); shapes[0] = rectangle1;
shapes[1] = new Rectangle (*new Point(10, -10), 4, 4);
Rectangle* rectangle3 = new Square(*new Point(-8.5,-10), 2.5);
shapes[2] = rectangle3;
Square* square2 = new Square (*new Point(-1,-1), 1.5);
shapes[3] = square2;
Circle* circle = new Circle(*new Point(100,100), 25.4);
shapes[4] = circle;
cout << "\n11111111111111111111111111\n";
for (int i = 0; i < size; ++i)
shapes[i] -> display();
return 0;
}