C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)

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

입출력 연산자 오버로딩 (I/O Operator Overloading)

  • 멤버 함수로 만들 수 없다.

    • 첫 번째 인자가 stream이기 때문이다.

예제

  • friend를 사용한 출력 연산자 오버로딩 예제

    #include <iostream>
    
    class Point
    {
      double    x_;
      double    y_;
      double    z_;
    
    public:
      Point(double x = 0.0, double y = 0.0, double z = 0.0)
        : x_(x), y_(y), z_(z)
      {}
    
      friend std::ostream& operator << (std::ostream& out, const Point& p)
      {
        out << '(' << p.x_ << ", " << p.y_ << ", " << p.z_ << ')';
        return (out);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Point p1(0.0, 0.1, 0.2), p2(3.4, 1.5, 2.0);
    
      cout << p1 << ' ' << p2 << endl;
    }
    
    /* stdout
    (0, 0.1, 0.2) (3.4, 1.5, 2)
    */
  • stream을 통해 파일 입출력을 할 경우, 이를 이용해서 굉장히 쉽게 할 수 있다.

    • 출력되는 파일은 VS기준 왼쪽 위의 파일명을 우클릭해서 나오는 Open Containing folder를 클릭하면 열리는 폴더에 있다.
      • 단축키는 Ctrl + K를 누른 다음 R을 누르면 된다. (Ctrl + R이 아니다.)
    #include <iostream>
    #include <fstream>
    
    class Point
    {
      double    x_;
      double    y_;
      double    z_;
    
    public:
      Point(double x = 0.0, double y = 0.0, double z = 0.0)
        : x_(x), y_(y), z_(z)
      {}
    
      friend std::ostream& operator << (std::ostream& out, const Point& p)
      {
        out << '(' << p.x_ << ", " << p.y_ << ", " << p.z_ << ')';
        return (out);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Point p1(0.0, 0.1, 0.2), p2(3.4, 1.5, 2.0);
    
      ofstream of("out.txt");
    
      of << p1 << ' ' << p2 << endl;
    
      of.close();
    }
    
    /* out.txt
    (0, 0.1, 0.2) (3.4, 1.5, 2)
    */
  • friend를 사용한 입력 연산자 오버로딩 예제

    #include <iostream>
    
    class Point
    {
      double    x_;
      double    y_;
      double    z_;
    
    public:
      Point(double x = 0.0, double y = 0.0, double z = 0.0)
        : x_(x), y_(y), z_(z)
      {}
    
      friend std::ostream& operator << (std::ostream& out, const Point& p)
      {
        out << '(' << p.x_ << ", " << p.y_ << ", " << p.z_ << ')';
        return (out);
      }
    
      friend std::istream& operator >> (std::istream& in, Point& p)
      {
        in >> p.x_ >> p.y_ >> p.z_;
        return (in);
      }
    };
    
    int        main()
    {
      using namespace std;
    
      Point p1, p2;
    
      cin >> p1 >> p2;
    
      cout << p1 << ' ' << p2 << endl;
    }
    
    /* stdin
    1 2 3 4 5 6
    */
    
    /* stdout
    (1, 2, 3) (4, 5, 6)
    */
반응형
저작자표시 (새창열림)

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

C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)  (0) 2021.03.16
C++ 단항 연산자 오버로딩 (Unary Operator Overloading)  (0) 2021.03.16
C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)  (0) 2021.03.16
C++ 오버로딩 (Overloading)  (0) 2021.03.16
C++ 따라하며 배우는 C++ 9장  (0) 2021.03.16
'SW개발/C++' 카테고리의 다른 글
  • C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)
  • C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
  • C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)
  • C++ 오버로딩 (Overloading)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (321) N
      • SW개발 (269) N
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (3) N
        • Windows Device Driver (6)
      • IT(개발아님) (47)
        • Windows (26)
        • MacOS (8)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 입출력 연산자 오버로딩 (I/O Operator Overloading)
상단으로

티스토리툴바