C++/따배C++ 09강 연산자 오버로딩
[C++/9.01] 산술 연산자 오버로딩
polarcompass
2021. 10. 26. 21:54
728x90
#include<iostream>
using namespace std;
class Cents
{
private:
int m_cents;
public:
Cents(int cents = 0) { m_cents = cents; }
int getCents() const { return m_cents; }
int &getCents() { return m_cents; }
Cents operator + (const Cents &c2)
{
return Cents(this->m_cents + c2.getCents());
}
};
int main()
{
Cents cents1(6);
Cents cents2(8);
// Cents sum;
// add(cents1, cents2, sum);
cout << (cents1 + cents2 + Cents(6) + Cents(10) + Cents(10)).getCents() << endl;
// 아래 연산자들은 가급적 연산자 오버로딩하지 않는다.
// ?: :: sizeof . .*
// ^
return 0;
}
728x90