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

2021. 3. 16. 00:20·SW개발/C++
목차
  1. 예제
반응형

입출력 연산자 오버로딩 (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
  1. 예제
'SW개발/C++' 카테고리의 다른 글
  • C++ 비교 연산자 오버로딩 (Comparison Operator Overloading)
  • C++ 단항 연산자 오버로딩 (Unary Operator Overloading)
  • C++ 산술 연산자 오버로딩 (Arithmetic Operator Overloading)
  • C++ 오버로딩 (Overloading)
Caniro
Caniro
MinimalismCaniro 님의 블로그입니다.
  • 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)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.