#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(const RMB &other):yuan(other.yuan), jiao(other.jiao), fen(other.fen)
{
count++;
}
// 析构函数
~RMB()
{
count--;
}
// 静态成员函数
static int existCount()
{
return count;
}
RMB operator+(const RMB &R)
{
RMB temp;
temp.yuan = yuan + R.yuan;
temp.jiao = jiao + R.jiao;
temp.fen = fen + R.fen;
return temp;
}
RMB operator-(const RMB &R)
{
RMB temp;
temp.yuan = yuan - R.yuan;
temp.jiao = jiao - R.jiao;
temp.fen = fen - R.fen;
return temp;
}
RMB &operator--()
{
--yuan;
--jiao;
--fen;
return *this;
}
RMB operator--(int)
{
RMB temp;
temp.yuan = yuan--;
temp.jiao = jiao--;
temp.fen = fen--;
return temp;
}
bool operator>(const RMB &other)
{
return yuan*100+jiao*10+fen > other.yuan*100+other.jiao*10+other.fen;
}
void show()
{
cout << "yuan " << yuan << endl;
cout << "jiao " << jiao << endl;
cout << "fen " << fen << endl;
}
};
// 静态变量初始化
int RMB::count;
int main()
{
// 测试数据
RMB r1(200, 2, 2);
RMB r2(100, 1, 1);
cout << "=====\"+\"重载测试=====" << endl;
RMB r3 = r1 + r2;
r3.show();
cout << "=====\"-\"重载测试=====" << endl;
r3 = r1 - r2;
r3.show();
cout << "=====\"前置--\"重载测试=====" << endl;
RMB r4 = --r3;
r4.show();
r3.show();
cout << "=====\"后置--\"重载测试=====" << endl;
r4 = r3--;
r4.show();
r3.show();
cout << "=====\">\"重载测试=====" << endl;
if(r1 > r2)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
cout<< "===静态成员变量功能测试===" << endl;
cout << "现存RMB对象: " << RMB::existCount() << endl;
RMB* r5 = new RMB(300, 3, 3);
cout << "现存RMB对象: " << RMB::existCount() << endl;
delete r5;
cout << "现存RMB对象: " << RMB::existCount() << endl;
return 0;
}

