9.03.

#include <iostream>

using namespace std;

/*-------------------------------------------------------------*/
class RMB
{
    static int count;
private:
    int yuan;
    int jiao;
    int fen;
public:
    //获得当前RMB数量
    static int RMBNUM()
    {
        return count;
    }


    RMB()
    {
        count++;
    }
    RMB(int yuan,int jiao,int fen):yuan(yuan),jiao(jiao),fen(fen)
    {
        count++;
    }

    //重载运算符“+”
    const RMB operator+(const RMB &other)const
    {
        RMB temp;
        temp.yuan = yuan + other.yuan;
        temp.jiao = jiao + other.jiao;
        temp.fen = fen + other.fen;
        count--;
        return temp;
    }

    //重载运算符“-”
    const RMB operator-(const RMB &other)const
    {
        RMB temp;
        temp.yuan = yuan - other.yuan;
        temp.jiao = jiao - other.jiao;
        temp.fen = fen - other.fen;
        count--;
        return temp;
    }

    //判断大小
    bool operator>(const RMB &other)
    {
        int rental,other_rental;
        rental = 100*yuan+10*jiao+fen;
        other_rental = 100*other.yuan+10*other.jiao+other.fen;
        return rental > other_rental;
    }
    //重载运算符前缀运算“--”
    RMB operator--()
    {
        --yuan;
        --fen;
        --jiao;
        return *this;
    }
    //重载运算符后缀运算“--”
    const RMB operator--(int)
    {
        RMB temp;
        temp.yuan = yuan--;
        temp.jiao = jiao--;
        temp.fen = fen--;
        count--;
        return temp;
    }
    //若有类成员调用析构函数,RMB数量--
    void show()
    {
        cout << "yuan:" << yuan << "\tjiao:" << jiao << "\tfen:" << fen << endl;
    }
//    ~RMB()
//    {
//          count--;
//    }
};

int RMB::count=0;
int main()
{
    RMB RB1(100,5,7);
    RMB RB2(100,9,5);
    cout << "RB1:";
    RB1.show();
    cout << "RB2:";
    RB2.show();
    if(RB1>RB2)
    {
        cout << "RB1>RB2\n";
    }
    else
    {
        cout << "RB1<=RB2\n";
    }
    cout << "--------------------------------------------------\n";
    RMB RB3;
    RB3 = RB1+RB2;
    cout << "RB3:";
    RB3.show();
    cout << "------------------------------------------------------\n";
    --RB3;
    cout << "--RB3:";
    RB3.show();
    RMB RB4;
    RB4=RB3--;
    cout <<"RB4(RB3--):";
    RB4.show();
    cout << "---------------------------------------------------\n";
    cout << "count:" <<  RMB::RMBNUM() << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值