/*
* 程序的版权和版本声明部分:
* Copyright (c) 2013.烟台大学计算机学院
* All rights reserved.
* 文件名称: 矩形类运算符重载
* 作 者:冯冬影
* 完成日期:2014 年 5月 31日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分:
* 输入描述:测试函数中第一个矩形直接初始化,第二个矩形通过键盘输入。
* 问题描述:两个矩形相加的规则是:决定矩形的对应坐标分别相加
* 程序输出:输出两点相加后得到的点的面积。
*/
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double a=0,double b=0,double c=0,double d=0);
Rectangle operator+(Rectangle &p1);
void input();
friend ostream &operator<<(ostream &output,Rectangle &p);
private:
double x1;
double x2;
double y1;
double y2;
};
Rectangle::Rectangle(double a,double b,double c,double d)
{
x1=a;
y1=b;
x2=c;
y2=d;
}
Rectangle Rectangle::operator+(Rectangle &p1)
{
Rectangle p;
p.x1=x1+p1.x1;
p.x2=x2+p1.x2;
p.y1=y1+p1.y1;
p.y2=y2+p1.y2;
return p;
}
void Rectangle::input()
{
cin>>x1>>y1>>x2>>y2;
}
ostream &operator<<(ostream &output,Rectangle &p)
{
double s;
s=(p.x1-p.x2)*(p.y1-p.y2);
output<<s<<endl;
return output;
}
int main()
{
Rectangle p1(1,1,6,3),p2,p3;
p2.input();
p3=p1+p2;
cout<<p3;
return 0;
}
运行结果
矩形类中的运算符重载
最新推荐文章于 2024-06-04 21:17:30 发布