#include <iostream>
class RMB
{
private:
int yuan;
int jiao;
int fen;
static int count;
public:
RMB() = default;
RMB(int yuan, int jiao, int fen):yuan(yuan),jiao(jiao),fen(fen)
{
count++;
}
RMB(const RMB& other) :yuan(other.yuan), jiao(other.jiao), fen(other.fen)
{
count++;
}
const RMB operator+(const RMB& a) const
{
RMB temp;
temp.yuan = this->yuan + a.yuan;
temp.fen = this->fen + a.fen;
temp.jiao = this->jiao + a.jiao;
return temp;
}
const RMB operator-(const RMB& a) const
{
RMB temp;
temp.yuan = this->yuan - a.yuan;
temp.fen = this->fen - a.fen;
temp.jiao = this->jiao - a.jiao;
return temp;
}
bool operator>(const RMB& a) const
{
if (this->yuan > a.yuan) return true;
if (this->yuan == a.yuan && this->jiao > a.jiao) return true;
if (this->yuan == a.yuan && this->jiao == a.jiao && this->fen > a.fen) return true;
return false;
}
RMB& operator--(int)
{
if(this->yuan != 0) this->yuan--;
if (this->jiao != 0) this->jiao--;
if(this->fen != 0) this->fen--;
return *this;
}
RMB& operator--()
{
if (this->yuan != 0) --this->yuan;
if (this->jiao != 0) --this->jiao;
if (this->fen != 0) --this->fen;
return *this;
}
void myprintf()
{
std::cout << this->yuan << " " << this->jiao << " " << this->fen << std::endl;
}
/*int dispaly()
{
return count;
}*/
static int display();
};
int RMB::count = 0;
int RMB::display()
{
return count;
}
int main()
{
RMB R1(2, 0, 5);
RMB R2(R1);
RMB R3(2, 4, 5);
R1--;
R1.myprintf();
std::cout << RMB::display() << std::endl;
return 0;
}
静态变量与函数重载
最新推荐文章于 2024-08-09 10:25:03 发布