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;
}
相关推荐
爱和冰阔落3 分钟前
【Linux网络】HTTP 服务器怎么返回网页和图片?从 404、301 到 Content-Type 与静态资源完整实战
linux·c++·http·网络编程·web服务器
yiangL_14 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
JosieBook14 小时前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
Huangjin007_16 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
geovindu16 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk816317 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾17 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
炸膛坦客18 小时前
单片机/C/C++八股:(三十三)取别名
c语言·c++·单片机
听情歌落俗18 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力18 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端