#include <iostream>
#include <cmath>
using namespace std;
class Rectangle
{
public:
Rectangle() {}
Rectangle(double xx1,double yy1,double xx2,double yy2):x1(xx1),y1(yy1),x2(xx2),y2(yy2) {}
void input()
{
cin>>x1>>y1>>x2>>y2;
}
friend Rectangle operator+ (Rectangle &,Rectangle &);
friend ostream& operator<<(ostream &,Rectangle &);
private:
double x1,y1,x2,y2;
};
ostream& operator<<(ostream &output,Rectangle &p)
{
double l=fabs(p.x1-p.x2);
double w=fabs(p.y1-p.y2);
output<<l*w<<endl;
return output;
}
Rectangle operator+ (Rectangle &p1,Rectangle &p2)
{
Rectangle p3;
p3.x1=p1.x1+p2.x1;
p3.x2=p1.x2+p2.x2;
p3.y1=p1.y1+p2.y1;
p3.y2=p1.y2+p2.y2;
return p3;
}
int main()
{
Rectangle p1(1,1,6,3),p2,p3;
p2.input();
p3=p1+p2;
cout<<p3;
return 0;
}
矩形类中的运算符重载
最新推荐文章于 2021-12-02 18:09:56 发布