Laboratory: Create a class Rectangle, which stores only Cartesian coordinates of
ID: 3912760 • Letter: L
Question
Laboratory: Create a class Rectangle, which stores only Cartesian coordinates of four corners of a rectangle as arrays shown below: p4lx4.y4] pllxl,y1] p3 x3,y3] p2lx2,y2] The constructor calls a set function that accepts four sets of coordinates. The set function verifies that the supplied coordinates do, in fact, form a rectangle Provide member functions that calculate length, width, perimeter, and area. The length is larger of the two dimensions. Include a predicate function square that determines whether the rectangle is a square. Write a program for the above described rectangle class, compile it, execute it, and demonstrate toExplanation / Answer
#include<iostream>
using namespace std;
class Point//to store point
{
public:
int x,y;
//constructor
Point(int xx,int yy)
{
x=xx;
y=yy;
}
};
class Rectangle
{
/*
assuming
p4-----------p3
| |
| |
p1-----------p2
*/
public:
Point *p1,*p2,*p3,*p4;
Rectangle()
{
//reading points...
int x,y;
cout<<"Enter p1 x:";
cin>>x;
cout<<"Enter p1 y:";
cin>>y;
p1 = new Point(x,y);
cout<<"Enter p2 x:";
cin>>x;
cout<<"Enter p2 y:";
cin>>y;
p2 = new Point(x,y);
cout<<"Enter p3 x:";
cin>>x;
cout<<"Enter p3 y:";
cin>>y;
p3 = new Point(x,y);
cout<<"Enter p4 x:";
cin>>x;
cout<<"Enter p4 y:";
cin>>y;
p4 = new Point(x,y);
if(set(p1,p2,p3,p4))
{
caculate();
}
}
//set method checks whether it forms rectangle or not...
int set(Point *p1,Point *p2,Point *p3,Point *p4)
{
if(p1->x==p4->x && p2->x==p3->x && p1->y==p2->y && p4->y ==p3->y)
{
return 1;//if it forms rectangle
}
return 0;//if not forms rectangel
}
//method to calculate length,width,area,perimeter
void caculate()
{
double length = p1->x - p2->x;
double width = p1->y - p4->y;
if(length<0)length = length*-1;
if(width<0)width = width*-1;
if(length<width){
double k = length;
length = width;
width = k;
}
cout<<"Length of Rectangle:"<<length<<endl;
cout<<"Width of Rectangle:"<<width<<endl;
cout<<"Area of Rectangle:"<<length*width<<endl;
cout<<"Perimeter of Rectangle:"<<2*(length+width)<<endl;
}
//method to find out..whether it is square or not..
//returns 1 if square
//else returns 0
int square()
{
double length = p1->x - p2->x;
double width = p1->y - p4->y;
if(length<0)length = length*-1;
if(width<0)width = width*-1;
if(length==width)return 1;//if square
return 0; //if not a square
}
};
int main()
{
Rectangle *r = new Rectangle();
return 0;
}
output:
Enter p1 x:1
Enter p1 y:2
Enter p2 x:5
Enter p2 y:2
Enter p3 x:5
Enter p3 y:4
Enter p4 x:1
Enter p4 y:4
Length of Rectangle:4
Width of Rectangle:2
Area of Rectangle:8
Perimeter of Rectangle:12
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.