C++ 단항 연산자 오버로딩 (Unary Operator Overloading)

2021. 3. 16. 00:24·SW개발/C++
반응형

단항 연산자 오버로딩 (Unary Operator Overloading)

  • -, ! 오버로딩

    #include <iostream>
    
    class Cents
    {
      int    cents_;
    
    public:
      Cents(int cents = 0) { cents_ = cents; }
    
      Cents operator - () const
      {
        return Cents(-cents_);
      }
    
      bool operator ! () const
      {
        return (cents_ == 0) ? true : false;
      }
    
      friend std::ostream& operator << (std::ostream& out, const Cents& cents)
      {
        out << cents.cents_;
        return (out);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Cents    c1{ 6 };
      Cents    c2{ 0 };
    
      cout << -c1 << ' ' << -c2 << endl;
      cout << std::boolalpha;
      cout << !c1 << ' ' << !c2 << endl;
    }
    
    /* stdout
    -6 0
    false true
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)  (0) 2021.03.16
C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)  (0) 2021.03.16
C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)  (0) 2021.03.16
C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)  (0) 2021.03.16
C++ 오버로딩 (Overloading)  (0) 2021.03.16
'SW개발/C++' 카테고리의 다른 글
  • C++ 증감 연산자 오버로딩 (Increment and Decrement Operator Overloading)
  • C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)
  • C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)
  • C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    맥북 카카오톡 알림 안뜸
    logi options
    시스템 복구
    윈도우
    KakaoTalk
    Solaris 10
    dism
    spring
    mspaint
    EXCLUDE
    windows
    스프링
    로지텍 마우스 제스처
    그림판
    java
    Workspace
    백기선
    citrix workspace
    MacOS
    vscode
    unix
    SFC
    알림
    Windows 11
    제외
    SunOS 5.1
    스프링 프레임워크 핵심 기술
    윈도우 명령어
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
상단으로

티스토리툴바