9.3作业

#include <iostream>

using namespace std;
class RAM
{
   friend const RAM operator-(const RAM &L,const RAM &R);
    friend const RAM operator+(const RAM &L,const RAM &R);
    friend bool operator>(const RAM &L,const RAM &R);
    friend RAM &operator--(RAM &O);
   friend const RAM operator--(RAM &O,int);
private:
    int yuan;
    int jiao;
    int fen;
    static int count;
public:
    RAM()
    {
        count++;
        cout << " 数量:" << count << endl;
    }
    RAM(int y,int j,int f):yuan(y),jiao(j),fen(f)
    {
        count++;
        cout << " 数量:" << count << endl;
    }
    ~RAM()
    {
        count--;
        cout << " 数量:" << count << endl;
    }
    void show()
    {
        cout << " yuan = " << yuan << " jiao = " << jiao << " fen = " << fen << endl;
    }

};
int RAM::count = 0;
//算数运算+
const RAM operator+(const RAM &L,const RAM &R)
{
    RAM temp;
    temp.yuan = L.yuan+R.yuan;
    temp.jiao = L.jiao+R.jiao;
    temp.fen = L.fen+R.fen;
    return temp;
}
//算数运算-
const RAM operator-(const RAM &L,const RAM &R)
{
    RAM t;
    t.yuan = L.yuan-R.yuan;
    t.jiao = L.jiao-R.jiao;
    t.fen = L.fen-R.fen;
    return t;
}
//关系运算
bool operator>(const RAM &L,const RAM &R)
{
    if(L.yuan>R.yuan || (L.yuan==R.yuan&&L.jiao>R.jiao) ||(L.yuan==R.yuan && L.jiao==R.jiao && L.fen>R.fen))
    {
        return true;
    }
    else
    {
        return false;
    }

}
//前缀运算--
RAM &operator--(RAM &O)
{
    --O.yuan;
    --O.jiao;
    --O.fen;
    return O;

}
//后缀运算--
const RAM operator--(RAM &O,int)
{
    RAM temp;
    temp.yuan = O.yuan--;
    temp.jiao = O.jiao--;
    temp.fen = O.fen--;
    return temp;
}

int main()
{
    RAM s1(5,5,5);
    RAM s2(1,1,1);
    RAM s3 = s1+s2;
    s3.show();
    cout << "======================================" << endl;
    RAM s4 = s1-s2;
    s4.show();
    cout << "======================================" << endl;
    if(s1>s2)
    {
        cout << "s1>s2" << endl;
    }
    else
    {
        cout << "s1<s2" << endl;
    }
     cout << "======================================" << endl;
     --s1;
     s1.show();
     cout << "======================================" << endl;
     RAM s5;
     s5 = s2--;
     s2.show();
     s5.show();
     cout << "======================================" << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值