250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/9.01] 산술 연산자 오버로딩 본문

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