250x250
Notice
Recent Posts
«   2024/11   »
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
관리 메뉴

일상 코딩

[C++/9.03] 단항 연산자 오버로딩 본문

C++/따배C++ 09강 연산자 오버로딩

[C++/9.03] 단항 연산자 오버로딩

polarcompass 2021. 10. 28. 01:26
728x90
#include <iostream>
#include <fstream>
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());
    }

    bool operator ! () const
    {
        return (m_cents == 0) ? true : false;
    }

    friend std::ostream &operator<<(std::ostream &out, const Cents &cents)
    {
        out << cents.m_cents;
        return out;
    }
};

int main()
{
    Cents cents1(6);
    Cents cents2(0);

    auto temp = !cents1;
    auto temp2 = cents1;

    cout << !cents1 << ' ' << !cents2 << endl;
}
728x90