#include
using namespace std;
class RMB{
int yuan,jiao,fen;
public:
RMB(int y=0,int j=0,int f=0)
{
yuan=y;jiao=j;fen=f;
if(jiao>=10) //讓分,角逢十進一
{
jiao=jiao-10;yuan=yuan+1;
}
if(fen>=10)
{
fen=fen-10;
jiao=jiao+1;
}
}
RMB operator+(RMB b);
RMB operator-(RMB b);
void display();
};
RMB RMB::operator +(RMB b)
{
yuan=yuan+b.yuan;
jiao=jiao+b.jiao;
fen=fen+b.fen;
if(fen>=10)
{
fen=fen-10;
jiao=jiao+1;
if(jiao>=10)
{
jiao=jiao-10;
yuan=yuan+1;
}
}
return RMB(yuan,jiao,fen);
}
RMB RMB::operator -(RMB b)
{
if(fen
{
if(jiao>0)
{
fen=fen+10;
jiao=jiao-1;
}
else
if(yuan>0)
{
yuan=yuan-1;
jiao=jiao+9;
fen=fen+10;
}
}
fen=fen-b.fen;
if(jiao
if(yuan>0)
{
yuan=yuan-1;
jiao=jiao+10;
}
jiao=jiao-b.jiao;
yuan=yuan-b.yuan;
return RMB(yuan,jiao,fen);
}
void RMB::display()
{
// if(yuan>=0)
cout<<yuan<<"元"<<jiao<<"角"<<fen<<"分"<<endl;
// else cout<<"輸入有誤!"<<endl;
}
void main()
{
RMB c(1,2,1);RMB d(3,3,3);
RMB e;RMB f;
c.display ();
d.display ();
e=c+d;
e.display ();
f=d-c;
// RMB c1(1,2,1);RMB d1(5,3,3);
// f=d1-c1;
f.display ();
}
如果把f=d-c注釋掉,然後後兩行不要注釋,得出的結果又是對的?
e=c+d; //這裡重載之後 C的值已經改變了。。
e.display ();
f=d-c;