作业结果
作业代码
#include <iostream>
using namespace std;
class RMB
{
friend const RMB operator-(const RMB &L,const RMB &R);
friend const RMB operator--(RMB &O,int);
private:
int yuan;
int jiao;
int fen;
static int count;
public:
RMB()
{
++count;
cout << "count=" << count <<endl;
}
RMB(int yuan,int jiao,int fen):yuan(yuan),jiao(jiao),fen(fen)
{
this->yuan = yuan;
this->jiao = jiao;
this->fen = fen;
++count;
cout << "count=" << count <<endl;
cout << "调用了RMB有参函数 " << endl;
}
~RMB()
{
--count;
cout << "count=" << count <<endl;
}
void show()
{
cout << "yuan=" << yuan << endl;
cout << "jiao=" << jiao << endl;
cout << "fen=" << fen << endl;
}
//+运算符重载
const RMB operator+(const RMB &R) const
{
RMB temp;
temp.yuan = yuan + R.yuan;
temp.fen = fen + R.fen;
temp.jiao = jiao + R.jiao;
return temp;
}
//>运算符重载
bool operator>(const RMB &R)const
{
if(yuan>R.yuan && jiao>R.jiao && fen>R.fen)
{
return true;
}
else
{
return false;
}
}
//前置自减
RMB &operator--()
{
--yuan;
--jiao;
--fen;
return *this;
}
};
//-运算符
const RMB operator-(const RMB &L,const RMB &R)
{
RMB temp;
temp.yuan = L.yuan - R.yuan;
temp.fen = L.fen - R.fen;
temp.jiao = L.jiao - R.jiao;
return temp;
}
//后置--
const RMB operator--(RMB &O,int)
{
RMB temp1;
temp1.yuan=O.yuan--;
temp1.jiao=O.jiao--;
temp1.fen=O.fen--;
return temp1;
}
int RMB::count;
int main()
{
RMB r0(11,11,11);
RMB r1(21,22,23);
RMB r3=r1+r0;//+运算符重载
r3.show();
cout << "********************************" << endl;
RMB r4=r1-r0;//-运算符重载
r4.show();
cout << "********************************" << endl;
//》运算符重载
if(r1>r0)
{
cout << "r1>r0" << endl;
}
cout << "********************************" << endl;
//前置--
cout << "前置--前的r0:" <<endl;
r0.show();
RMB rr=--r0;
cout << "前置--后的r0:" <<endl;
rr.show();
cout << "********************************" << endl;
//后置--
RMB r5=r1--;
cout << "r5: " <<endl;
r5.show();
cout << "r1: " <<endl;
r1.show();
return 0;
}