Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1 point) Create a base class named Point, consisting of x and y data members re

ID: 3865946 • Letter: #

Question

(1 point) Create a base class named Point, consisting of x and y data members representing point coordinates.

Also create a class named Rectangle with two members of type Point (1 point). These members represent opposite vertices making up the rectangle, with sides parallel to the x and y axis. The class Rectangle shall have member functions to 1. (1 point) calculate and return the width as the length of the side parallel to the x axis, 2. (1 point) calculate and return the height as the length of the side parallel to the y axis. 3. (1 point) calculate and return the area of the rectangle. 4. (1 point) calculate and return the perimeter of the rectangle. The class Rectangle shall also have appropriate mutator (1 point) and accessor (1 point) functions as well as appropriate constructors (i.e. default constructor (1 point), constructor for one set of coordinates (1 point), two sets(1 point), one (1 point) and two Point (1 point) parameters).

Use these classes in a test program that exercises every constructor and function (5 points).

Provide a screenshot (1 point) of the results of the test run, do not prompt the user for values, use hardcoded values instead.

Plz, use C++ code

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#include<math.h>

using namespace std;

class Point {
    private:
       int x;
       int y;
    public:
    Point(int a, int b){
        x = a;
        y = b;
    }
    int getX(){
       return x;
    }
    int getY(){
       return y;
    }
    void setX(int a){
       x = a;
    }
    void setY(int a){
       y = a;;
    }

};


class Rectangle{

   private:
       Point *p1;
       Point *p2;

   public:
     Rectangle(){
         p1 = new Point(0,0);
         p2 = new Point(0,0);
     }
    
     Rectangle( Point *p){
        p1 = p;
        p2 = new Point(0,0);
     }

     Rectangle(Point *a, Point *b){
        p1 = a;
        p2 = b;
     }
     
     void setP1(Point *p){
          p1 = p;
     }
     void setP2(Point p){
          *p2 = p;
     }
     Point *getP1(){
          return p1;
     }
     Point *getP2(){
          return p2;
     }
     int getWidth(){
         return (abs(p1->getX() - p2->getX()));
     }
     int getHeight(){
         return (abs(p1->getY() - p2->getY()));
     }
     void calculatePerimeter(){
         cout << "Perimeter:" << 2*(getWidth() + getHeight()) << endl;

     }
     void calculateArea(){
         cout << "Area:" << getWidth() * getHeight() << endl;

     }
     void display(){
         cout << "P1.x:" << p1->getX() << endl;
         cout << "P1.y:" << p1->getY() << endl;
         cout << "P2.x:" << p2->getX() << endl;
         cout << "P2.y:" << p2->getY() << endl;
     }
    
};


int main(){
   
    Point *p1 = new Point(2,6);
    Point *p2 = new Point(8,2);
    Rectangle rt;
    Rectangle rt1(p1);
    Rectangle rt2(p1,p2);

    rt.display();
    rt1.display();
    rt2.display();
       
    cout << "Width:" << rt2.getWidth() << endl;
    cout << "Height:" << rt2.getHeight() << endl;
    rt2.calculateArea();
    rt2.calculatePerimeter();
       
    return 0;
}