2.7练习

1

2

#include <iostream>

using namespace std;
class Complex
{
private:
    double a;
    double b;
public:
    Complex(){}
    Complex(double a,double b):a(a),b(b)
    {}
    const Complex operator+(const Complex &R)const
    {
        Complex temp;
        temp.a=a+R.a;
        temp.b=b+R.b;
        return temp;
    }
    const Complex operator+(double s)const
    {
        Complex temp;
        temp.a=a+s;
        temp.b=b;
        return temp;
    }
    friend const Complex operator+(double s,Complex &R);
    void show()
    {
        cout << "(" << a << "," << b << ")" << endl;
    }
};
const Complex operator+(double s,Complex &R)
{
    Complex temp;
    temp.a=s+R.a;
    temp.b=R.b;
    return temp;
}
int main()
{
    Complex a(2,5);
    Complex b(7,8);
    Complex c(0,0);
    c=a+b;
    c.show();
    c=4.1+a;
    c.show();
    c=b+5.6;
    c.show();
    return 0;
}

3

#include <iostream>

using namespace std;
class Time
{
private:
    int hour;
    int minute;
    int second;
public:
    Time(){}
    Time(int h,int m,int s):hour(h),minute(m),second(s)
    {}
    const Time operator+(const Time &R)
    {
        int s;
        Time temp;
        s=second+R.second+minute*60+R.minute*60+hour*3600+R.hour*3600;
        temp.second=s%60;
        temp.minute=(s/60)%60;
        temp.hour=s/3600;
        return temp;
    }
    const Time operator-(const Time &R)
    {
        int s;
        Time temp;
        s=(second+minute*60+hour*3600)-(R.second+R.minute*60+R.hour*3600);
        if(s<0)
        {
            s=s+24*3600;
        }
        temp.second=s%60;
        temp.minute=(s/60)%60;
        temp.hour=s/3600;
        return temp;
    }
    void show()
    {
        cout << hour << ":" << minute << ":" << second << endl;
    }
};
int main()
{
   Time a(10,40,55);
   Time b(5,23,23);
   Time c;
   c=a+b;
   c.show();
   c=a-b;
   c.show();
    return 0;
}

4

#include <iostream>

using namespace std;
class Money
{
private:
    int m;
public:
    Money(){}
    Money(int m):m(m){}
    friend Money operator*(const Money &R,double s);
    friend Money operator*(double s,const Money &R);
    void show()
    {
        cout << m << endl;
    }
};
Money operator*(const Money &L,double s)
{
    Money temp;
    temp.m=L.m*s;
    return temp;
}
Money operator*(double s,const Money &R)
{
    Money temp;
    temp.m=s*R.m;
    return temp;
}
int main()
{
    Money a(12);
    Money b;
    b=a*2.4;
    b.show();
    b=1.2*a;
    b.show();
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值