13.4 Overloading the I/O operators

주홍영·2022년 3월 18일
0

Learncpp.com

목록 보기
147/199

https://www.learncpp.com/cpp-tutorial/overloading-the-io-operators/

For classes that have multiple member variables, printing each of the individual variables on the screen can get tiresome fast.
복수의 멤버변수가 있는 경우 각각 변수를 프린트 하는 것은 번거로울 수 있다

class Point
{
private:
    double m_x{};
    double m_y{};
    double m_z{};

public:
    Point(double x=0.0, double y=0.0, double z=0.0)
      : m_x{x}, m_y{y}, m_z{z}
    {
    }

    double getX() const { return m_x; }
    double getY() const { return m_y; }
    double getZ() const { return m_z; }
};

위의 코드를 보면 각 변수에 접근하기 위해 member function을 각각 정의하고 있음을 알 수 있다

만약 우리가 print를 하고 싶다면 우리는 이러한 방식으로 print를 진행해야 한다

Point point{5.0, 6.0, 7.0};

std::cout << "Point(" << point.getX() << ", " <<
    point.getY() << ", " <<
    point.getZ() << ')';

member function으로 print라는 함수를 만들어서 출력을 할 수도 있겠지만
우리는 <<operator 에 overloading 또한 할 수 있다
이를 활용해서 출력을 해보자

Overloading operator<<

operator<< overloading은 operator+와 마찬가지로 binary operator이다
대신 parameter type이 다르다

std::cout << point를 보자면 std::cout과 point가 operand이다
std::cout은 실제로 std::ostream의 object이다 따라서 overalod function은 다음과 같다

// std::ostream is the type for object std::cout
friend std::ostream& operator<< (std::ostream& out, const Point& point);

이를 활용해서 operator<< 함수를 구현하면 다음과 같이 코드를 작성할 수 있다

#include <iostream>

class Point
{
private:
    double m_x{};
    double m_y{};
    double m_z{};

public:
    Point(double x=0.0, double y=0.0, double z=0.0)
      : m_x{x}, m_y{y}, m_z{z}
    {
    }

    friend std::ostream& operator<< (std::ostream& out, const Point& point);
};

std::ostream& operator<< (std::ostream& out, const Point& point)
{
    // Since operator<< is a friend of the Point class, we can access Point's members directly.
    out << "Point(" << point.m_x << ", " << point.m_y << ", " << point.m_z << ')'; // actual output done here

    return out; // return std::ostream so we can chain calls to operator<<
}

int main()
{
    const Point point1{2.0, 3.0, 4.0};

    std::cout << point1 << '\n';

    return 0;
}

std::cout과 마찬가지로 std::cin도 operator overloading을 통해서 구현 가능하다
cin의 경우 operator>>을 사용하므로 다음과 같이 작성할 수 있다

#include <iostream>

class Point
{
private:
    double m_x{};
    double m_y{};
    double m_z{};

public:
    Point(double x=0.0, double y=0.0, double z=0.0)
      : m_x{x}, m_y{y}, m_z{z}
    {
    }

    friend std::ostream& operator<< (std::ostream& out, const Point& point);
    friend std::istream& operator>> (std::istream& in, Point& point);
};

std::ostream& operator<< (std::ostream& out, const Point& point)
{
    // Since operator<< is a friend of the Point class, we can access Point's members directly.
    out << "Point(" << point.m_x << ", " << point.m_y << ", " << point.m_z << ')';

    return out;
}

std::istream& operator>> (std::istream& in, Point& point)
{
    // Since operator>> is a friend of the Point class, we can access Point's members directly.
    // note that parameter point must be non-const so we can modify the class members with the input values
    in >> point.m_x;
    in >> point.m_y;
    in >> point.m_z;

    return in;
}

이때 타입은 std::istream& 임을 확인할 수 있다
두 operator를 활용해 main함수에서는 다음과 같이 간단하게 작성할 수 있다

int main()
{
    std::cout << "Enter a point: ";

    Point point;
    std::cin >> point;

    std::cout << "You entered: " << point << '\n';

    return 0;
}
profile
청룡동거주민

0개의 댓글