当前位置: 首页 > news >正文

8.9 C++

1.思维导图

2.

搭建一个货币的场景,创建一个名为 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 <iostream>using namespace std;class RMB
{
private:int yuan;int jiao;int fen;static int count;
public:RMB(){cout << "RMB::无参构造函数" << endl;count++;}RMB(int y,int j,int f):yuan(y),jiao(j),fen(f){cout << "RMB::有参构造函数" << endl;count++;}RMB(const RMB &other):yuan(other.yuan),jiao(other.jiao),fen(other.fen){cout << "RMB::拷贝构造函数" << endl;count++;}~RMB(){cout << "RMB::析构函数" << endl;count--;}const RMB operator-(const RMB &other){int sum1 = yuan*100 + jiao*10 + fen;int sum2 = other.yuan + other.jiao*10 + other.fen;int sub = sum1 - sum2;int sub_yuan = sub/100;sub %= 100;int sub_jiao = sub/10;int sub_fen = sub%10;return RMB(sub_yuan,sub_jiao,sub_fen);}const RMB operator+(const RMB &other){int sum1 = yuan*100 + jiao*10 + fen;int sum2 = other.yuan + other.jiao*10 + other.fen;int sum = sum1 + sum2;int sum_yuan = sum/100;sum %= 100;int sum_jiao = sum/10;int sum_fen = sum%10;return RMB(sum_yuan,sum_jiao,sum_fen);}bool operator>(const RMB &other)const{int sum1 = yuan*100 + jiao*10 + fen;int sum2 = other.yuan + other.jiao*10 + other.fen;if(sum1 > sum2){return true;}else{return false;}}RMB &operator--(){if(fen > 0){fen--;}else{if(jiao > 0){jiao--;fen = 9;}else{if(yuan > 0){yuan--;jiao = 9;fen = 9;}else{cout << "有错误!!" << endl;}}}return *this;}const RMB operator--(int){RMB temp(*this);--(*this);return temp;}void show(){cout << yuan << "元" << jiao << "角" << fen << "分" << endl;}void count_show(){cout << "count=" << count << endl;}
};int RMB::count = 0;int main()
{RMB r1(3,3,3);RMB r2(8,8,8);RMB r3 = r1 + r2;RMB r4 = r2 - r1;r1--;--r2;if(r3>r4){cout << "r3>r4" << endl;}r1.show();r2.show();r3.show();r4.show();r3.count_show();r4.count_show();return 0;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 快速基于 ClickHouse + Grafana 搭建可观测性解决方案 - 分布式链路追踪篇(ClickHouse 官方博客)...
  • 8.8 day bug
  • Python pandas常见函数
  • 【数据结构初阶】队列
  • K8S Ingress 常用配置
  • vue项目打包问题
  • 题解:力扣1567 - 返回乘积为正数的最长子数组
  • 串行并行数据转换
  • WEB渗透Bypass篇-常规函数绕过
  • 网络安全与国家安全
  • 力扣Hot100-994腐烂的橘子
  • 007 | 期权定价与布莱克-斯科尔斯计算
  • git pull 注意事项
  • 【hadoop】常用命令
  • 四、数字图像处理Matlab实验 第二章 数字图像基础
  • ----------
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • Intervention/image 图片处理扩展包的安装和使用
  • laravel5.5 视图共享数据
  • node.js
  • sessionStorage和localStorage
  • Vue 动态创建 component
  • 近期前端发展计划
  • 批量截取pdf文件
  • 删除表内多余的重复数据
  • 思维导图—你不知道的JavaScript中卷
  • 提醒我喝水chrome插件开发指南
  • 小程序01:wepy框架整合iview webapp UI
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 2017年360最后一道编程题
  • Semaphore
  • 国内开源镜像站点
  • 交换综合实验一
  • 选择阿里云数据库HBase版十大理由
  • ​数据链路层——流量控制可靠传输机制 ​
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • #微信小程序(布局、渲染层基础知识)
  • (06)金属布线——为半导体注入生命的连接
  • (1)bark-ml
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (10)STL算法之搜索(二) 二分查找
  • (HAL库版)freeRTOS移植STMF103
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (二十四)Flask之flask-session组件
  • (分享)自己整理的一些简单awk实用语句
  • (六)DockerCompose安装与配置
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (十一)c52学习之旅-动态数码管
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (转)jdk与jre的区别
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .NET Micro Framework 4.2 beta 源码探析
  • .NET 材料检测系统崩溃分析
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明