运算符重载练习 | |||
| |||
Description | |||
定义一个类Box,有长、宽、高属性。请重载运算符 + ,使得 box1 + box2 能够将对应的长、宽、高相加. (必需按题目要求方式实现) | |||
Input | |||
输入: box1 的长、宽、高 ; box2 的长、宽、高;(多组数据) | |||
Output | |||
相加后的长、宽、高 | |||
Sample Input | |||
1 2 3 4 5 6 | |||
Sample Output | |||
5 7 9 |
#include <bits/stdc++.h>
using namespace std;
class Box
{
public:
void setLength(double len)
{
length = len;
}
void setBreadth(double bre)
{
breadth = bre;
}
void setHeight(double hei)
{
height = hei;
}
Box operator+(const Box &b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
double length;
double breadth;
double height;
private:
}; //类重载符的应用
int main()
{
Box Box1;
Box Box2;
Box Box3;
double a,b,c,d,e,f;
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF)
{
Box1.setLength(a);
Box1.setBreadth(b);
Box1.setHeight(c);
scanf("%lf%lf%lf",&d,&e,&f);
Box2.setLength(d);
Box2.setBreadth(e);
Box2.setHeight(f);
Box3=Box1+Box2;
cout <<Box3.length<<" "<<Box3.breadth<<" "<<Box3.height<<endl;
}
return 0;
}