C++学习记录--day5

学习记录:

 练习题:

#include <iostream>
using namespace std;

class RMB
{
private:
    int yuan;
    int jiao;
    int fen;
    static int count;
public:
    RMB(int yuan = 0, int jiao = 0, int fen = 0)
        : yuan(yuan),jiao(jiao),fen(fen){ ++count;}
    ~RMB(){--count;}

    static void getcountNUM()
    {
        cout << "当下账户数量:" << count << endl;
    }

    const RMB operator+(const RMB &c)const;//成员函数实现 + 重载
    const RMB operator-(const RMB &c)const;//成员函数实现 - 重载
    bool operator>(const RMB &c)const;//成员函数实现 > 重载
    RMB &operator--(); //成员函数实现 前置-- 重载  返回一个左值
    RMB operator--(int);//成员函数实现 后置-- 重载  返回一个右值

    void show()
    {
        cout << yuan << "元" << jiao << "角" << fen <<"分" << endl;
    }
};
int RMB::count = 0; //静态数据成员需要在类外进行初始化!!!!

const RMB RMB::operator+(const RMB &c)const
{
    RMB temp;
    temp.yuan = this->yuan + c.yuan;
    temp.jiao = this->jiao + c.jiao;
    temp.fen  = this->fen  + c.fen;
    return temp ;
}

const RMB RMB::operator-(const RMB &c)const//成员函数实现 - 重载
{
    RMB temp;
    temp.yuan = this->yuan - c.yuan;
    temp.jiao = this->jiao - c.jiao;
    temp.fen  = this->fen  - c.fen;
    return temp ;
}

bool RMB::operator>(const RMB &c)const
{
    bool ret = this->yuan > c.yuan ||
            (this->yuan == c.yuan && this->jiao > c.jiao )||
            (this->yuan == c.yuan && this->jiao == c.jiao && this->fen > c.fen);
    return ret;
}

RMB &RMB::operator--() //前置-- 返回一个左值
{
    --this->yuan;
    --this->jiao;
    --this->fen;
    return  *this;
}

RMB RMB::operator--(int) //后置-- 返回一个右值
{
    RMB temp;
    temp.fen = this->fen--;
    temp.jiao = this->jiao--;
    temp.yuan = this->yuan--;
    return  temp;
}

int main()
{
    RMB c1 (101,6,5);
    RMB c2(50,5,4);

    RMB c3 = c1 + c2;
    c3.show();

    if(c1 > c2)
    {
        cout << "c1 > c2" << endl;
    }

    c2 = c1--;
    c2.show();
    c1.show();

    c2 = --c1;
    c2.show();
    c1.show();

    RMB::getcountNUM();  // 3个
    RMB *c4 = new RMB(25,5,6);
    RMB::getcountNUM();  //4个
    delete c4;
    c4 = nullptr;
    RMB::getcountNUM();  //3个

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值