Write a class Rectangle to represent a rectangle with given

Write a class Rectangle to represent a rectangle with given end points and center point – so your constructor must have five arguments of type MyPoint (4 end points and the center). Assume the end points are given counter clockwise.

Include methods in your class:

double getWidth() – to get the width of the rectangle.

double getHeight() – to get the height of the rectangle.

MyPoint getCenter() – to get the center of the rectangle.

double getArea() – to compute the area of the rectangle.

double getPerimeter() – to compute the perimeter of the rectangle.

Write a main program that creates two distinct rectangles and prints their areas.

Write a class Square, which inherits from class Rectangle, to represent a square with given end points and center point. Class Square should have two constructors:

A constructor to make a square with given end points, centered at a given point (so the constructor must have five arguments, all of type MyPoint).

A constructor with no arguments that makes the unit square – this constructor must call the first constructor (the unit square has side length 1, center (0,0)).

Write a main program that creates the unit square, and prints its width and area by calling the getWidth and getArea methods. Carefully explain in detail how the square is constructed, and how the getWidth and getArea methods are called.

Solution

#include <iostream>
#include <cmath>
using namespace std;

class MyPoint
{
   private:
   double x;
   double y;
  
   public:
   MyPoint()
   {
   }
   MyPoint(double x,double y)
   {
       this->x = x;
       this->y = y;
   }
   MyPoint(const MyPoint &P)
   {
       this->x = P.x;
       this->y = P.y;
   }
   double getX()
   {
       return this->x;
   }
   double getY()
   {
       return this->y;
   }
  
};
class Rectangle
{
   protected:
   MyPoint p1;
   MyPoint p2;
   MyPoint p3;
   MyPoint p4;
   MyPoint center;
   public:
   Rectangle(){}
   Rectangle(MyPoint p1,MyPoint p2,MyPoint p3,MyPoint p4,MyPoint center)
   {
       this->p1 = p1;
       this->p2 = p2;
       this->p3 = p3;
       this->p4 = p4;
       this->center = center;
   }
   double getWidth() //– to get the width of the rectangle.
   {
       double width;
       width = p1.getY() - p4.getY();
       return width;
      
   }
   double getHeight() //– to get the height of the rectangle.
   {
   double height;
   height = p2.getX() - p4.getX();
   return height;
   }
   MyPoint getCenter() //– to get the center of the rectangle.
   {
       MyPoint p;
       p = center;
       return p;
   }
   double getArea() //– to compute the area of the rectangle.
   {
       double area;
       area = getWidth() * getHeight();
       return area;
   }
   double getPerimeter()
   {
       double perimeter;
       perimeter = 2*(getWidth()+getHeight());
       return perimeter;
   }
   };
class Square :Rectangle
{
  
   public:
   Square(MyPoint p1,MyPoint p2,MyPoint p3,MyPoint p4,MyPoint center)
   {
       this->p1 = p1;
       this->p2 = p2;
       this->p3 = p3;
       this->p4 = p4;
       this->center = center;
   }
  
   double getWidth()   
   {
       double width;
       width = p1.getY() + p4.getY(); //opposite directions will get added
       return abs(width);
      
   }
  
   MyPoint getCenter() //– to get the center of the rectangle.
   {
       MyPoint p;
       p = center;
       return p;
   }
   double getArea() //– to compute the area of the rectangle.
   {
       double area;
       area = getWidth() * getWidth();
       return area;
   }
   double getPerimeter()
   {
       double perimeter;
       perimeter = 4*(getWidth());
       return perimeter;
   }
   };

int main()
{
   MyPoint p1(0,6);
   MyPoint p2(4,6);
   MyPoint p3(4,0);
   MyPoint p4(0,0);
   MyPoint center(3,2);
   Rectangle r(p1,p2,p3,p4,center);
   cout<<\"\ height of rectangle : \"<<r.getHeight();
   cout<<\"\ Width of rectangle : \"<<r.getWidth();
   cout<<\"\ Area of Rectangle : \"<<r.getArea();
   cout<<\"\ Perimeter of Rectangle : \"<<r.getPerimeter();
  
  
   MyPoint p11(0.5,-0.5);
   MyPoint p22(0.5,0.5);
   MyPoint p33(0.5,-0.5);
   MyPoint p44(-0.5,-0.5);
   MyPoint center1(0,0);
   Square s(p11,p22,p33,p44,center1);
   cout<<\"\ Width of square : \"<<s.getWidth();
   cout<<\"\ Area of square : \"<<s.getArea();
   cout<<\"\ Perimeter of square : \"<<s.getPerimeter();
   return 0;
}

output:

Write a class Rectangle to represent a rectangle with given end points and center point – so your constructor must have five arguments of type MyPoint (4 end po
Write a class Rectangle to represent a rectangle with given end points and center point – so your constructor must have five arguments of type MyPoint (4 end po
Write a class Rectangle to represent a rectangle with given end points and center point – so your constructor must have five arguments of type MyPoint (4 end po

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site