#include
using namespace std;
class Money
{
private:
int m_nYuan;
int m_nJiao;
int m_nFen;
public:
Money() :m_nYuan(0), m_nJiao(0), m_nFen(0){}
Money(int nYuan, int nJiao, int nFen) :
m_nYuan(nYuan),
m_nJiao(nJiao),
m_nFen(nFen)
{
Money::CheckData(*this);
}
void Print()
{
cout << m_nYuan << "元" << m_nJiao << "角" << m_nFen << "分" << endl;
}
Money operator + (const Money& rM)
{
Money m;
m.m_nFen = this->m_nFen + rM.m_nFen;
m.m_nYuan = this->m_nYuan + rM.m_nYuan;
m.m_nJiao = this->m_nJiao + rM.m_nJiao;
Money::CheckData(m);
return m;
}
// 分最多为9,角最多为9,超过了就进位
static void CheckData(Money& money)
{
if (money.m_nFen >= 10)
{
money.m_nJiao += money.m_nFen / 10;
money.m_nFen = money.m_nFen % 10;
}
if (money.m_nJiao >= 10)
{
money.m_nYuan += money.m_nJiao / 10;
money.m_nJiao = money.m_nJiao % 10;
}
}
};
int main()
{
Money m1(1, 9, 3), m2(2, 3, 11);
Money m3;
m3 = m1 + m2;
m3.Print();
return 0;
}
朋友,请【采纳答案】,您的采纳是我答题的动力,如果没有明白,请追问。谢谢。