Description
Defines a rectangular class that includes the lower-left and upper-right coordinates, defined member functions that include the necessary constructors, functions for input coordinates, implementation of the rectangle addition, and functions to calculate and output the rectangular area. Requires that the test function given in the hint be used and must not be altered.
The rules for adding two rectangles are: the corresponding coordinates of the decision rectangle are added separately, as
The lower-left corner (3,4), the rectangle in the upper-right corner, and
The lower-left corner (2,3), the upper-right corner (4,5) of the rectangle is added, the resulting rectangle is
The lower-left corner (3,5), the upper-right corner (7,9) of the rectangle.
This rule has no geometrical meaning, so it is defined.
The function of the output area is accomplished by overloading the "<<" operation.
This can be done on the basis of 2383.
Input
The first rectangle in the test function is initialized directly, and the second rectangle is entered by the keyboard. Enter four numbers, representing the coordinates of the lower-left and upper-right vertices of the second rectangle, such as input 2.5 1.8 4.3 2.5, which represent the lower-left corner coordinates (2.5, 1.8), and the upper-right coordinate (4.3, 2.5).
Output
The area of the point that is obtained when the output is two points. When you run the test function, the vertex of the P1 is 1 1 6 3, if the input P2 is 2.5 1.8 4.3 2.5, the computed rectangle P3 has the lower-left coordinate (3.5, 2.8), the upper-right coordinate (10.3, 5.5), and the output is P3 area of 18.36.
Sample Input
2.5 1.8 4.3 2.5
Sample Output
18.36
/* All rights reserved. * File Name: Test.cpp * Chen Dani * Completion date: June 21, 2015 * Version number: v1.0 */#include & Lt;iostream> #include <cmath>using namespace Std;class rectangle{private:double x1; Double Y1; Double X2; Double Y2;public:rectangle (double a1,double b1,double a2,double B2): X1 (A1), Y1 (B1), X2 (A2), y2 (b2) {} Rectangle () {} Rectangle operator + (Rectangle &p1); Friend Ostream & operator<< (Ostream&,rectangle &p); void input ();}; Rectangle Rectangle::operator + (Rectangle &p1) {Rectangle p; p.x1=x1+p1.x1; P.y1=y1+p1.y1; p.x2=x2+p1.x2; P.y2=y2+p1.y2; return p;} Ostream & operator<< (Ostream&output,rectangle &p) {double S; S=abs (p.x2-p.x1) *abs (p.y2-p.y1); output<<s; return output;} void Rectangle::input () {cin>>x1>>y1>>x2>>y2;} int main () {Rectangle P1 (1,1,6,3), p2,p3; P2.input (); P3=P1+P2; cout<<p3; return 0;}
Learning experience: Nothing can make you stop, except yourself.
Now the brush problem, I found a feeling, good good, is so, constantly in the process of doing the problem, found the mistake, correct the mistake, oneself can become more powerful!!
15th Week OJ Brush title--problem C: operator overloading "C + +" in a rectangular class