day5 C++

作业

1>
搭建一个货币的场景,创建一个名为 RMB 的类,该类具有整型私有成员变量 yuan(元)、jiao(角)和 fen(分),并且具有以下功能:
(1)重载算术运算符 + 和 -,使得可以对两个 RMB 对象进行加法和减法运算,并返回一个新的 RMB 对象作为结果。
(2)重载关系运算符 >,判断一个 RMB 对象是否大于另一个 RMB 对象,并返回 true 或 false。
(3)重载前置减减运算符 --,使得每次调用时 RMB 对象的 yuan、jiao 和 fen 分别减 1
(4)重载后置减减运算符 --,使得每次调用时 RMB 对象的 yuan、jiao 和 fen 分别减 1
(5)另外, RMB 类还包含一个静态整型成员变量 count,用于记录当前已创建的 RMB 对象的数量。每当创建一个新的 RMB 对象时,count 应该自增 1;每当销毁一个 RMB 对象时,count 应该自减 1。
要求,需要在main 函数中测试上述RMB 类的功能。

#include 

using namespace std;

class RMB
{
private:
    int yuan;
    int jiao;
    int fen;
    static int count;
public:
    //无参构造函数
    RMB(){
        set_add_count();
    }
    //有参构造函数
    RMB(int yuan, int jiao, int fen):yuan(yuan),jiao(jiao),fen(fen){
        set_add_count();
    }
    //拷贝构造函数
    RMB(const RMB &other):yuan(other.yuan),jiao(other.jiao),fen(other.fen){
        set_add_count();
    }
    //拷贝赋值函数
    RMB &operator=(const RMB &other)
    {
        if(this != &other)
        {
            yuan=other.yuan;
            jiao=other.jiao;
            fen=other.fen;
        }
        return *this;
    }

    //+算术符重载函数
    const RMB operator+(const RMB &other) const
    {
        RMB temp;
        temp.yuan=yuan+other.yuan;
        temp.jiao=jiao+other.jiao;
        temp.fen=fen+other.fen;
        if(temp.fen >= 10)
        {
            temp.jiao+=1;
            temp.fen=temp.fen%10;
        }
        if(temp.jiao >= 10)
        {
            temp.yuan+=1;
            temp.jiao=temp.jiao%10;
        }
        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;
        if(temp.fen < 0)
        {
            temp.jiao-=1;
            temp.fen+=10;
        }
        if(temp.jiao < 0)
        {
            temp.yuan-=1;
            temp.jiao+=10;
        }
        return temp;
    }

    // >关系运算符重载
    bool operator>(const RMB &other) const
    {
        if(yuan > other.yuan)
        {
            return true;
        }
        else if(jiao > other.jiao && yuan == other.yuan)
        {
            return true;
        }
        else if(fen > other.fen && yuan == other.yuan && jiao == other.jiao)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    //前置减减
    RMB &operator--()
    {
        yuan--;
        jiao--;
        fen--;
        return *this;
    }

    //后置减减
    const RMB operator--(int)
    {
        RMB temp;
        temp.yuan=yuan--;
        temp.jiao=jiao--;
        temp.fen=fen--;
        return temp;
    }
    //静态自增count
    void static set_add_count()
    {
        count++;
        cout << "count=" << count <<endl;
    }
    //静态自减count
    void static set_reduce_count()
    {
        count--;
        cout << "count=" << count <<endl;
    }
    //展示
    void show()
    {
        cout << "yuan=" << yuan << " jiao=" << jiao << " fen=" << fen << endl;
    }
    //析构函数
    ~RMB()
    {
        set_reduce_count();
    }
};

//静态变量赋值
int RMB::count=0;

int main()
{
    RMB r1(5,5,5);
    RMB r2(1,7,9);
    RMB r3 = r1 - r2;
    RMB r4 = r1 + r2;
    r3.show();
    r4.show();
    cout << "====================" <<endl;
    --r1;
    r1.show();
    cout << "====================" <<endl;
    RMB r5;
    r5=r2--;
    r2.show();
    r5.show();
    return 0;
}

day5 C++_第1张图片

你可能感兴趣的:(c++,开发语言)