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
HINT
int main ()
{
Rectangle P1 (1,1,6,3), p2,p3;
P2.input ();
P3=P1+P2;
cout<<p3;
return 0;
}
When committing, add the main function.
The code is as follows:
#include <iostream>using namespace Std;class rectangle{public:rectangle (double A =0,double b=0,double c=0,double d=0); void input (); Friend Rectangle operator+ (Rectangle &,rectangle &); Friend Ostream &operator<< (ostream &output,rectangle &s);p rivate:double x1; Double Y1; Double X2; Double y2;}; Rectangle::rectangle (double a,double b,double c,double d) {x1=a; Y1=b; X2=c; Y2=d;} void Rectangle::input () {cin>>x1>>y1>>x2>>y2;} Rectangle operator+ (Rectangle &s1,rectangle &s2) {Rectangle s; s.x1=s1.x1+s2.x1; S.y1=s1.y1+s2.y1; s.x2=s1.x2+s2.x2; S.y2=s1.y2+s2.y2; return s;} Ostream & operator<< (ostream &output,rectangle &s) {double S; s= (s.x2-s.x1) * (S.Y2-S.Y1); output<<s<<endl; return output;} int main () {Rectangle S1 (1,1,6,3), S2,S3; S2.input (); S3=S1+S2; COUT<<S3;}
Operation Result:
Learning experience:
This topic is the content of this week to learn, not to see the video, but with the help of the book has been done.
The heart is tired, or the format is wrong once,,, the topic provides the main function, the result is also required to add the main function,, but has been a large number of topics do not need to submit the topic of the problem to develop this inertia ... So... Alas. I have to finish reading the question carefully ...
ytuoj-operator overloading in rectangular classes