/*烟台大学计算机学院学生
*All right reserved.
*文件名称:acm~矩阵类中运算符的重载
*作者:杨飞
*完成日期:2014年6月11日
*版本号:v1.0
*对任务及求解方法的描述部分: 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数、输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数。要求使用提示中给出的测试函数并不得改动。
两个矩形相加的规则是:决定矩形的对应坐标分别相加,如
左下角(1,2),右上角(3,4)的矩形,与
左下角(2,3),右上角(4,5)的矩形相加,得到的矩形是
左下角(3,5),右上角(7,9)的矩形。
这个规则没有几何意义,就这么定义好了。
输出面积的功能通过重载"<<"运算完成。
本题可以在2383的基础上扩展完成。
*我的程序:*/
#include <iostream>
using namespace std;
class Rectangle
{
private:
double x1,y1;
double x2,y2;
public:
Rectangle():x1(0),y1(0),x2(0),y2(0){}
Rectangle(double xx1,double yy1,double xx2,double yy2)
{
x1=xx1;
y1=yy1;
x2=xx2;
y2=yy2;
}
void input()
{
cin>>x1>>y1>>x2>>y2;
}
Rectangle operator +(Rectangle &b)
{
Rectangle c;
c.x1=x1+b.x1;
c.y1=y1+b.y1;
c.x2=x2+b.x2;
c.y2=y2+b.y2;
return c;
}
double area()
{
double s;
s=(x1-x2)*(y1-y2);
if(s<0)
{
return (-s);
}
return s;
}
friend ostream&operator <<(ostream & output, Rectangle & t)
{
output<<t.area()<<endl;
return output;
}
};
int main()
{
Rectangle p1(1,1,6,3),p2,p3;
p2.input();
p3=p1+p2;
cout<<p3;
return 0;
}
心得体会:无