C++:day5

思维导图

例题

cpp 复制代码
#include <iostream>
using namespace std;
class RMB
{
private:
    int yuan;
    int jiao;
    int fen;
    static int count;

public:
    RMB()
    {
        count++;
    }
    RMB(int yuan, int jiao, int fen) : yuan(yuan), jiao(jiao), fen(fen)
    {
        count++;
    }
    const RMB operator+(const RMB &R) const
    {
        RMB temp;
        temp.yuan = yuan + R.yuan;
        temp.jiao = jiao + R.jiao;
        temp.fen = fen + R.fen;
        return temp;
    }
    const RMB operator-(const RMB &R) const
    {
        RMB temp;
        temp.yuan = yuan - R.yuan;
        temp.jiao = jiao - R.jiao;
        temp.fen = fen - R.fen;
        return temp;
    }
    bool operator>(const RMB &R) const
    {
        if (yuan > R.yuan)
        {
            if (jiao > R.jiao)
            {
                if (fen > R.fen)
                {
                    return true;
                }
                return true;
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    RMB &operator--()
    {
        --yuan;
        --jiao;
        --fen;
        return *this;
    }
    RMB &operator--(int)
    {
        RMB temp;
        temp.yuan = yuan--;
        temp.jiao = jiao--;
        temp.fen = fen--;
        return *this;
    }
    void show()
    {
        cout << yuan << "元\t" << jiao << "角\t" << fen << "分" << endl;
    }
    static int getCount()
    {
        return count;
    }
    ~RMB()
    {
        count--;
    }
};
int RMB::count = 0;
int main(int argc, char const *argv[])
{
    cout << "现在的RMB对象数量为:" << RMB::getCount() << endl;
    RMB r1(5, 5, 1);
    cout << "现在的RMB对象数量为:" << RMB::getCount() << endl;
    RMB r2(5, 9, 5);
    cout << "现在的RMB对象数量为:" << RMB::getCount() << endl;
    RMB r3 = r1 + r2;
    r3.show();
    RMB r4 = r1 - r2;
    r4.show();

    if (r1 > r2)
    {
        cout << "yes" << endl;
    }
    else
    {
        cout << "no" << endl;
    }

    r1--;
    r1.show();

    cout << "现在的RMB对象数量为:" << RMB::getCount() << endl;
    return 0;
}
相关推荐
DYS_房东的猫13 分钟前
《 C++ 零基础入门教程》第6章:模板与 STL 算法 —— 写一次,用万次
开发语言·c++·算法
诗意地回家20 分钟前
淘宝小游戏反编译
开发语言·前端·javascript
wangkay8827 分钟前
【Java 转运营】Day04:抖音新号起号前准备全指南
java·开发语言·新媒体运营
点云SLAM27 分钟前
C++ 静态初始化顺序问题(SIOF)和SLAM / ROS 工程实战问题
开发语言·c++·slam·静态初始化顺序问题·工程实战技术·c++static 关键字
D3bugRealm28 分钟前
MATLAB解决物理问题:从基础运动学到进阶力学的实战指南
开发语言·其他·matlab
小李独爱秋31 分钟前
计算机网络经典问题透视:TLS协议工作过程全景解析
运维·服务器·开发语言·网络协议·计算机网络·php
pen-ai1 小时前
打通 Python 与 C++ 的参数传递机制
开发语言·c++·python
亲爱的非洲野猪1 小时前
深入解析享元模式:用Java实现高性能对象复用
java·开发语言·享元模式
qq_401700411 小时前
Qt的.pro文件
开发语言·qt
FAFU_kyp1 小时前
Rust 的 引用与借用
开发语言·算法·rust