18.36
/* All rights reserved. * 文件名称:test.cpp * 作者:陈丹妮 * 完成日期:2015年 6 月 21 日 * 版 本 号:v1.0 */ #include <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; }
现在的刷题,自己找到了感觉,好棒好棒,就是这样,不断的在做题的过程中,发现错误,改正错误,自己才能变得更加强大!!
第十五周oj刷题—— Problem C: 矩形类中运算符重载【C++】
原文地址:http://blog.csdn.net/nufangdongde/article/details/46581347