本文主要是介绍C++:day5,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思维导图
例题
#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;
}
这篇关于C++:day5的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!