Write a crect class to indicate a rectangle. The member variables of this rectangle class are x1, Y1, X2, Y2, And the coordinates in the upper left corner and lower right corner of the rectangle. Complete the following member functions:
1. crect (); // constructor without Parameters
2. crect (double X1 _, double Y1 _, double X2 _, double Y2 _); // constructor with four parameters (coordinates in the upper left and lower right corner of the rectangle)
3. bool issquare (); // determines whether the rectangle is square.
4. Double area (); // calculate the area of the rectangle.
5. bool intersecting (crect rect); // determines whether the rectangle and the other rectangle are intersecting, that is, the current rectangle and the rect have overlapping parts.
Implementation:
# Include <iostream. h>
Class crect {
PRIVATE:
Double x1;
Double X2;
Double Y1;
Double Y2;
Public:
Crect ();
Crect (double X1 _, double Y1 _, double X2 _, double Y2 _);
Bool issquare ();
Double area ();
Bool intersecting (crect rect );
};
Crect: crect ()
{X1 = Y1 = 0;
X2 = Y2 = 1;
}
Crect: :( double X1 _, double Y1 _, double X2 _, double Y2 _)
{X1 = x1 _;
Y1 = Y1 _;
X2 = X2 _;
Y2 = Y2 _;
}
Bool crect: issquare ()
{If (x2-x1) = (y2-y1 ))
Return true;
Else return false;
}
Double crect: Area ()
{Return (x2-x1) * (y2-y1 ));
}
Bool intersecting (crect rect );
{If (rect. X1 <X2 | rect. Y1 <Y2 | rect. X2> X1 | rect. Y2> Y1)
Return true;
Else return false;
}
Int main ()
{Double x1, x2, Y1, Y2;
Cout <"Please input two endpoints:" <Endl;
Cin> x1> Y1> X2> Y2;
Crect C1 (x1, Y1, X2, Y2 );
Crect C2;
If (c1.issquare ())
Cout <"The rect is a square! "<Endl;
Else cout <"The rect is not a square! "<Endl;
Cout <"the area of the rect is:" <c1.area () <Endl;
If (c1.intersecting (C2 ))
Cout <"rect C1 intersecting with rect C2! "<Endl;
Else cout <"rect C1 not intersecting with rect C2! "<Endl;
Return 0;
}