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

C++(day6)

一、思维导图

二、搭建一个货币的场景,创建一个名为 RMB 的类,该类具有整型私有成员变量 yuan(元)、jiao(角)和 fen(分),并且具有以下功能;
(1)重载算术运算符,和一,使得可以对两个 RMB 刘象进行加法和减法运算,并返回一个新的RMB 对象作为结果。
(2)重载关系运算符>,判断一个 RMB 刘象是否大于另一个 RMB 耐象,并返回 true 或 false。
(3)重载前置減減运算符一,使得每次调用时 RMB 列象的yuan、 jao 和 fen 分別减1
(4)重载后置减減运算符一,使得每次调用时 RMB 對象的yuan.jao 和 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(){count++;}RMB(int yuan,int jiao,int fen):yuan(yuan),jiao(jiao),fen(fen){count++;}~RMB(){count--;}const RMB operator+(const RMB &R) const{RMB temp;temp.yuan=this->yuan+R.yuan;temp.jiao=this->jiao+R.jiao;temp.fen=this->fen+R.fen;return temp;}const RMB operator-(const RMB &R) const{RMB temp;if((temp.fen=this->fen-R.fen)>=0){if((temp.jiao=this->jiao-R.jiao)>=0){temp.yuan=this->yuan-R.yuan;return temp;}else{temp.jiao+=10;temp.yuan=this->yuan-R.yuan-1;return temp;}}else{temp.fen+=10;if((temp.jiao=this->jiao-R.jiao-1)>=0){temp.yuan=this->yuan-R.yuan;return temp;}else{temp.jiao+=10;temp.yuan=this->yuan-R.yuan-1;return temp;}}}bool operator>(const RMB &R) const{if(this->yuan>R.yuan){return true;}else if(this->yuan==R.yuan){if(this->jiao>R.jiao){return true;}else if(this->jiao==R.jiao){if(this->fen>R.fen){return true;}elsereturn false;}elsereturn false;}elsereturn false;}RMB &operator--(){--this->yuan;--this->jiao;--this->fen;return *this;}RMB &operator--(int){static RMB temp;temp.yuan=this->yuan--;temp.jiao=this->jiao--;temp.fen=this->fen--;return temp;}void show(){cout<<this->yuan<<"元"<<this->jiao<<"角"<<this->fen<<"分"<<endl;}
};int RMB::count=0;int main()
{RMB p1(102,8,7);cout<<"p1=";p1.show();RMB p2(88,4,7);cout<<"p2=";p2.show();RMB p3=p1+p2;cout<<"p3=p1+p2=";p3.show();RMB p4=p1-p2;cout<<"p4=p1-p2";p4.show();RMB p5=p2-p1;cout<<"p5=p2-p1=";p5.show();if(p1>p2){cout<<"p1>p2"<<endl;}elsecout<<"p1<=p2"<<endl;RMB p6=--p1;cout<<"p6=--p1=";p6.show();RMB p7=p2--;cout<<"p7=p2--=";p7.show();return 0;
}

测试结果

http://www.dtcms.com/a/460697.html

相关文章:

  • MySQL InnoDB存储引擎CheckPoint技术底层实现原理详细介绍
  • HikariCP与Spring Boot集成使用指南
  • java-代码随想录第23天|39. 组合总和、40.组合总和II、131.分割回文串
  • 【LangChain】P18 LangChain 之 Chain 深度解析(三):基于 LCEL 语法的数据库与文档处理新型 Chain
  • 2.0 Labview自定义控件中的队列引用句柄从哪拖来?
  • 【LabVIEW实用开发】--- LabVIEW调用python脚本
  • 碰一碰系统源码搭建与发视频、发文案和写好评定制化开发:支持OEM
  • 安徽建设工程信息网站简单的个人网页制作html
  • iBizModel 全文检索体系(PSSYSSEARCHSCHEME)模型详解
  • Django REST Framework `Request` 对象
  • 易班班级网站建设展示PPT公司网站建设怎么做
  • 易讯网站建设凤翔网站制作
  • 湘潭找工作网站如何将wordpress上传
  • 做企业网站注意些啥太原网络营销
  • Redis 缓存与数据库谁先更新?
  • 网站备案要营业执照原件吗怎么做一个小程序app
  • 网站里的横幅广告怎么做商城网站 html模板
  • 金坛网站开发河南瑞达建设工程有限公司网站
  • 临淄网站设计可以做网站的公司
  • 做视频网站程序多少钱网站 注册模块怎么做
  • 做电影网站需要告诉网络网络营销方式有哪几种有哪些
  • 免保证金入驻电商平台wordpress摘要seo
  • 最低价网站建设建设企业网站公司
  • 旅游网站开发的背景及意义怎么在手机上设计网站
  • 网站搜索引擎收录百度一下百度首页官网
  • 南京品牌网站设计网站权重是什么
  • 有没有免费注册域名的网站淄博网站建设
  • php英文商城网站建设株洲新区发布
  • 长丰县建设局网站郑州做网站推广电
  • 谷歌有做网站建设合肥网站专业制作