

#include <iostream>
using namespace std;
typedef long long LL;
class RMB{
LL a,b,c;
static int count;
public:
RMB():a(0),b(0),c(0){
count++;
cout<<count<<endl;
};
RMB(LL a,LL b,LL c):a(a),b(b),c(c){
count++;
cout<<count<<endl;
};
~RMB(){
count--;
cout<<"count="<<count<<endl;
};
LL convert()const{
return this->a*100+this->b*10+this->c;
}
void convertToRMB(LL money){
this->a=money/100;
this->b=money/10%10;
this->c=money%10;
}
bool operator>(const RMB &other)const{
return this->convert()>other.convert();
}
RMB operator+(const RMB &rmb)const{
RMB temp;
temp.convertToRMB(this->convert()+rmb.convert());
return temp;
}
RMB operator-(const RMB &rmb)const{
RMB temp;
temp.convertToRMB(this->convert()-rmb.convert());
return temp;
}
RMB &operator--(){
this->convertToRMB(this->convert()-111);
return *this;
}
RMB operator--(int){
// RMB rmb=*this;//这里调用默认的拷贝构造函数,导致count不会增加,但是会调用析构函数,导致count多减一次;
RMB rmb;
rmb.convertToRMB(this->convert());
this->convertToRMB(this->convert()-111);
return rmb;
}
void show(){
cout<<"the object count="<<count<<endl;
cout<<"object[yuan,jiao,fen]=";
cout<<"["<<"a="<<a<<",b="<<b<<",c="<<c<<"]"<<endl;
}
};
int RMB::count=0;
int main()
{
RMB a;
a.show();
cout<<"========="<<endl;
RMB b(100,99,9);
b.show();
cout<<"=========="<<endl;
cout<<"+重载"<<endl;
a=a+b;
a.show();
cout<<"=========="<<endl;
cout<<"-重载"<<endl;
a=a-b;
a.show();
cout<<"=========="<<endl;
cout<<"前--重载"<<endl;
--b;
b.show();
cout<<"后--重载"<<endl;
b--;
b.show();
cout<<"==========="<<endl;
return 0;
}
1
the object count=1
object[yuan,jiao,fen]=[a=0,b=0,c=0]
=========
2
the object count=2
object[yuan,jiao,fen]=[a=100,b=99,c=9]
==========
+重载
3
count=2
the object count=2
object[yuan,jiao,fen]=[a=109,b=9,c=9]
==========
-重载
3
count=2
the object count=2
object[yuan,jiao,fen]=[a=0,b=0,c=0]
==========
前--重载
the object count=2
object[yuan,jiao,fen]=[a=108,b=8,c=8]
后--重载
3
count=2
the object count=2
object[yuan,jiao,fen]=[a=107,b=7,c=7]
===========
count=1
count=0