#include <iostream>
using namespace std;
class RAM
{
friend const RAM operator-(const RAM &L,const RAM &R);
friend const RAM operator+(const RAM &L,const RAM &R);
friend bool operator>(const RAM &L,const RAM &R);
friend RAM &operator--(RAM &O);
friend const RAM operator--(RAM &O,int);
private:
int yuan;
int jiao;
int fen;
static int count;
public:
RAM()
{
count++;
cout << " 数量:" << count << endl;
}
RAM(int y,int j,int f):yuan(y),jiao(j),fen(f)
{
count++;
cout << " 数量:" << count << endl;
}
~RAM()
{
count--;
cout << " 数量:" << count << endl;
}
void show()
{
cout << " yuan = " << yuan << " jiao = " << jiao << " fen = " << fen << endl;
}
};
int RAM::count = 0;
//算数运算+
const RAM operator+(const RAM &L,const RAM &R)
{
RAM temp;
temp.yuan = L.yuan+R.yuan;
temp.jiao = L.jiao+R.jiao;
temp.fen = L.fen+R.fen;
return temp;
}
//算数运算-
const RAM operator-(const RAM &L,const RAM &R)
{
RAM t;
t.yuan = L.yuan-R.yuan;
t.jiao = L.jiao-R.jiao;
t.fen = L.fen-R.fen;
return t;
}
//关系运算
bool operator>(const RAM &L,const RAM &R)
{
if(L.yuan>R.yuan || (L.yuan==R.yuan&&L.jiao>R.jiao) ||(L.yuan==R.yuan && L.jiao==R.jiao && L.fen>R.fen))
{
return true;
}
else
{
return false;
}
}
//前缀运算--
RAM &operator--(RAM &O)
{
--O.yuan;
--O.jiao;
--O.fen;
return O;
}
//后缀运算--
const RAM operator--(RAM &O,int)
{
RAM temp;
temp.yuan = O.yuan--;
temp.jiao = O.jiao--;
temp.fen = O.fen--;
return temp;
}
int main()
{
RAM s1(5,5,5);
RAM s2(1,1,1);
RAM s3 = s1+s2;
s3.show();
cout << "======================================" << endl;
RAM s4 = s1-s2;
s4.show();
cout << "======================================" << endl;
if(s1>s2)
{
cout << "s1>s2" << endl;
}
else
{
cout << "s1<s2" << endl;
}
cout << "======================================" << endl;
--s1;
s1.show();
cout << "======================================" << endl;
RAM s5;
s5 = s2--;
s2.show();
s5.show();
cout << "======================================" << endl;
return 0;
}
9.3作业
最新推荐文章于 2024-10-07 19:01:55 发布