C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)

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

산술 연산자 오버로딩 (Arithmetic Operator Overloading)

  • 클래스 안에서 정의해도 되고, 밖에서 정의해도 된다.

    • 안에서 정의할 때는 this로 첫 번째 파라미터를 받게되므로 파라미터가 하나 줄어든다.

    • 밖에서 정의할 때는 클래스에서 friend 키워드로 선언해주는게 편할 수도 있다.


예제

  • 오버로딩을 하지 않은 예제

    #include <iostream>
    
    class Cents
    {
      int    cents_;
    
    public:
      Cents(const int& cents = 0) { cents_ = cents; }
      int        getCents() const { return cents_; }
      int&    getCents() { return cents_; }
    };
    
    Cents    add(const Cents& c1, const Cents& c2)
    {
      return Cents(c1.getCents() + c2.getCents());
    }
    
    int        main()
    {
      using namespace std;
    
      Cents cents1(6);
      Cents cents2(8);
    
      cout << add(cents1, cents2).getCents() << endl;
    }
    
    /* stdout
    14
    */
  • + 연산자 오버로딩을 하면 다음과 같다.

    #include <iostream>
    
    class Cents
    {
      int    cents_;
    
    public:
      Cents(const int& cents = 0) { cents_ = cents; }
      int        getCents() const { return cents_; }
      int&    getCents() { return cents_; }
    };
    
    Cents    operator + (const Cents& c1, const Cents& c2)
    {
      return Cents(c1.getCents() + c2.getCents());
    }
    
    int        main()
    {
      using namespace std;
    
      Cents cents1(6);
      Cents cents2(8);
    
      cout << (cents1 + cents2 + Cents(6)).getCents() << endl;
    }
    
    /* stdout
    20
    */
  • 클래스 안에서 정의하면 다음과 같다.

    #include <iostream>
    
    class Cents
    {
      int    cents_;
    
    public:
      Cents(const int& cents = 0) { cents_ = cents; }
      int        getCents() const { return cents_; }
      int&    getCents() { return cents_; }
    
      Cents    operator + (const Cents& c2)
      {
        return Cents(cents_ + c2.cents_);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Cents cents1(6);
      Cents cents2(8);
    
      cout << (cents1 + cents2 + Cents(16)).getCents() << endl;
    }
    
    /* stdout
    30
    */
반응형
저작자표시 (새창열림)

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

C++ 단항 연산자 오버로딩 (Unary Operator Overloading)  (0) 2021.03.16
C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)  (0) 2021.03.16
C++ 오버로딩 (Overloading)  (0) 2021.03.16
C++ 따라하며 배우는 C++ 9장  (0) 2021.03.16
C++ chrono  (0) 2021.03.16
'SW개발/C++' 카테고리의 다른 글
  • C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
  • C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)
  • C++ 오버로딩 (Overloading)
  • C++ 따라하며 배우는 C++ 9장
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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바