运算符重载练习

运算符重载练习

Time Limit: 1000 MSMemory Limit: 32768 K
Special Judge: No
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值