#include <iostream>
using namespace std;
class Rmb
{
friend bool operator>(const Rmb &L, const Rmb &R);
friend const Rmb operator+(const Rmb &L, const Rmb &R);
friend const Rmb operator-(const Rmb &L, const Rmb &R);
friend const Rmb operator--(Rmb &O, int);
friend Rmb operator--(Rmb &O);
private:
int yuan;
int jiao;
int fen;
static int count;//记录当前已创建的RMB对象的数量
public:
Rmb() //无参构造,每调用一次 count加1
{
count++;
cout << "count =" << count << endl;
}
Rmb(int x,int y,int z):yuan(x),jiao(y),fen(z)//有参构造,每调用一次 count加1
{
count++;
cout << "count =" << count << endl;
}
Rmb(const Rmb &other):yuan(other.yuan),jiao(other.jiao),fen(other.fen)//拷贝构造,每调用一次 count加1
{
count++;
cout << "count =" << count << endl;
}
Rmb &operator=(const Rmb &other) // 拷贝赋值函数, 已经调用过无参构造,count不用加1
{
if (this != &other)
{
yuan = other.yuan;
jiao = other.jiao;
fen = other.fen;
}
return *this;
}
~Rmb()//析构,每调用一次 count-1
{
count--;
cout << "count =" << count << endl;
}
void show_rmb()
{
cout << yuan << "," << jiao << "," << fen << endl;
}
};
//全局函数后置--重载
const Rmb operator--(Rmb &O, int)
{
Rmb temp;
int money = O.yuan*100+O.jiao*10+O.fen;
if (money >= 111)
{
money -= 111;
temp.yuan = money/100;
temp.jiao = (money%100)/10;
temp.fen = money%10;
}
else if(money < 111 && money >= 11)
{
money -= 11;
temp.yuan = 0;
temp.jiao = (money%100)/10;
temp.fen = money%10;
}
else
{
money -= 1;
temp.yuan = 0;
temp.jiao = 0;
temp.fen = money%10;
}
return temp;
}
//全局函数前置--重载
Rmb operator--(Rmb &O)
{
int money = O.yuan*100+O.jiao*10+O.fen;
if (money >= 111)
{
money -= 111;
O.yuan = money/100;
O.jiao = (money%100)/10;
O.fen = money%10;
}
else if(money < 111 && money >= 11)
{
money -= 11;
O.yuan = 0;
O.jiao = (money%100)/10;
O.fen = money%10;
}
else
{
money -= 1;
O.yuan = 0;
O.jiao = 0;
O.fen = money%10;
}
return O;
}
//全局函数-号重载
const Rmb operator-(const Rmb &L, const Rmb &R)
{
Rmb temp;
int Left = L.yuan*100+L.jiao*10+L.fen;//计算左值的分
int Right = R.yuan*100+R.jiao*10+R.fen;//计算右值的分
int add = Left - Right;//相-
temp.yuan = add/100;//计算相-后的yuan
temp.jiao = (add%100)/10;//计算相-后的角
temp.fen = add%10;//计算相-后的分
return temp;
}
//全局函数+号重载
const Rmb operator+(const Rmb &L, const Rmb &R)
{
Rmb temp;
int Left = L.yuan*100+L.jiao*10+L.fen;//计算左值的分
int Right = R.yuan*100+R.jiao*10+R.fen;//计算右值的分
int add = Left + Right;//相加
temp.yuan = add/100;//计算相加后的yuan
temp.jiao = (add%100)/10;//计算相加后的角
temp.fen = add%10;//计算相加后的分
return temp;
}
//全局函数>号重载
bool operator>(const Rmb &L, const Rmb &R)
{
int Left = L.yuan*100+L.jiao*10+L.fen;//计算左值的分
int Right = R.yuan*100+R.jiao*10+R.fen;//计算右值的分
if(Left > Right)
{
return true;
}
else
{
return false;
}
}
int Rmb::count = 0;
int main()
{
Rmb R1(3,4,5);
Rmb R2(1,2,3);
Rmb R3;
if(R1 > R2)
{
cout << "R1 > R2" << endl;
cout << "======================" << endl;
R3 = R1 - R2;
cout << "R3::";
R3.show_rmb();
cout << "======================" << endl;
}
R3 = R1 + R2;
R3.show_rmb();
cout << "======================" << endl;
R3 = R1--;
R1.show_rmb();
R3.show_rmb();
cout << "======================" << endl;
R3 = --R1;
R1.show_rmb();
R3.show_rmb();
cout << "======================" << endl;
return 0;
}
20240903 作业
于 2024-09-03 21:00:56 首次发布