作业:
#include <iostream>
using namespace std;
class RMB
{
friend const RMB operator+(RMB &L,RMB &R);
friend const RMB operator-(RMB &L,RMB &R);
friend bool operator>(RMB &L,RMB &R);
friend RMB &operator--(RMB &R);
friend const RMB operator--(RMB &L,int);
private:
int yuan;
int jiao;
int fen;
static int count;
public:
static int RMBadd()
{
return ++count;
}
static int RMBdel()
{
return --count;
}
RMB()
{
RMBadd();
}
RMB(int y,int j,int f):yuan(y),jiao(j),fen(f)
{
RMBadd();
}
~RMB()
{
RMBdel();
}
void show()
{
cout << "total money:" << yuan << "yuan" << jiao << "jiao" << fen << "fen" << endl;
}
static void countshow()
{
cout << "total count:" << count << endl;
}
};
int RMB::count=0;
const RMB operator+(RMB &L,RMB &R)
{
RMB temp;
temp.yuan=L.yuan+R.yuan;
temp.jiao=L.jiao+R.jiao;
if(temp.jiao>=10)
{
temp.yuan+=temp.jiao/10;
temp.jiao%=10;
}
temp.fen=L.fen+R.fen;
if(temp.fen>=10)
{
temp.jiao+=temp.fen/10;
temp.fen%=10;
}
return temp;
}
const RMB operator-(RMB &L,RMB &R)
{
RMB temp;
temp.yuan=L.yuan-R.yuan;
temp.jiao=L.jiao-R.jiao;
if(temp.jiao<0)
{
temp.yuan--;
temp.jiao+=10;
}
temp.fen=L.fen-R.fen;
if(temp.fen<0)
{
temp.jiao--;
temp.fen+=10;
}
return temp;
}
bool operator>(RMB &L,RMB &R)
{
if(L.yuan>R.yuan && L.jiao>R.jiao && L.fen>R.fen)
{
return true;
}
else
{
return false;
}
}
RMB &operator--(RMB &R)
{
--R.yuan;
--R.jiao;
--R.fen;
return R;
}
const RMB operator--(RMB &L,int)
{
RMB temp;
temp.yuan=L.yuan--;
temp.jiao=L.jiao--;
temp.fen=L.fen--;
return temp;
}
int main()
{
RMB r;
r.countshow();
RMB r1(9,9,9);
r1.countshow();
RMB *r2=new RMB;
r2->countshow();
delete r2;
r2=nullptr;
r1.countshow();
RMB r3(8,8,8);
r=r1+r3;
r.show();
RMB r4=r-r1;
r4.show();
--r4;
r4.show();
RMB r5=r4--;
r5.show();
r4.show();
return 0;
}
结果: