## 运算符重载

RMB.h

```class RMB {
public:
RMB(int _yuan, int _jiao, int _fen);
~RMB();

private:
int yuan = 0;
int jiao = 0;
int fen = 0;
};
```

RMB.cpp

```#include "RMB.h"

RMB::RMB(int _yuan, int _jiao, int _fen)
{
yuan = _yuan;
jiao = _jiao;
fen = _fen;
}

RMB::~RMB()
{

}
```

RMB.h

```class RMB {
public:
RMB(int _yuan, int _jiao, int _fen);
~RMB();

private:
int yuan = 0;
int jiao = 0;
int fen = 0;
};
```

RMB.cpp

```#include "RMB.h"

RMB::RMB(int _yuan, int _jiao, int _fen)
{
yuan = _yuan;
jiao = _jiao;
fen = _fen;
}

RMB::~RMB()
{

}

{
RMB rmbRes(0, 0, 0);

// 分
int f = rmb.fen + fen;
int f_ = f / 10;
rmbRes.fen = f % 10;

// 角
int j = rmb.jiao + jiao + f_;
int j_ = j / 10;
rmbRes.jiao = j % 10;

// 元
int y = rmb.yuan + yuan + j_;
int y_ = y / 10;
rmbRes.yuan = y % 10;

return rmbRes;
}
```

```int main(int argc,char **argv)
{
RMB rmbA(1, 9, 0);
RMB rmbB(2, 5, 0);

return 0;
}
```

RMB.h

```class RMB {
public:
RMB(int _yuan, int _jiao, int _fen);
~RMB();

// RMB & Add(const RMB & rmb);
RMB operator + (const RMB & rmb);

private:
int yuan = 0;
int jiao = 0;
int fen = 0;
};
```

RMB.cpp

```#include "RMB.h"

RMB::RMB(int _yuan, int _jiao, int _fen)
{
yuan = _yuan;
jiao = _jiao;
fen = _fen;
}

RMB::~RMB()
{

}

// RMB & RMB::Add(const RMB & rmb)
RMB RMB::operator + (const RMB & rmb)
{
RMB rmbRes(0, 0, 0);

// 分
int f = rmb.fen + fen;
int f_ = f / 10;
rmbRes.fen = f % 10;

// 角
int j = rmb.jiao + jiao + f_;
int j_ = j / 10;
rmbRes.jiao = j % 10;

// 元
int y = rmb.yuan + yuan + j_;
int y_ = y / 10;
rmbRes.yuan = y % 10;

return rmbRes;
}
```

```int main(int argc,char **argv)
{
RMB rmbA(1, 9, 0);
RMB rmbB(2, 5, 0);

RMB rmbC = rmbA + rmbB;
return 0;
}
```

```.
.* ->*
::
sizeof
?:
#
```

?不会了怎么办
||

||

## 您还未绑定服务号

• · 粉丝专属优惠福利
• · 大咖直播交流干货
• · 课程更新,问题答复提醒
• · 账号支付安全提醒

0/150

• 问答
• 评论
• 笔记
• 代码