CodeBlanks Logo Icon CodeBlanks

Stream operators

Learn to overload << and >> operators.

Stream operators

operator<< and operator>> let you write an object to a stream and read from a stream. These operators are defined as global functions, not as members of the class.

Example: std::ostream& operator<<(std::ostream& os, const Point& p) { os << p.x << " " << p.y; return os; }. This lets you write std::cout << p;. The function returns std::ostream&; this enables chaining (std::cout << a << b). Why global? Because the left side is not your class but std::ostream. A member operator would need to be added inside std::ostream, which is not possible.

Chaining and return os

The operator<< function returns the stream with return os;. This lets you write multiple objects in sequence.

Example: std::cout << a << " " << b; works like this: first operator<<(std::cout, a) is called and returns std::cout. Then " " is written to the returned stream and the stream is returned again. Finally b is written. If the function returned void, chaining would not work; std::cout << a << b would be a compile error. That is why return os; is mandatory.

Practice Add operator<< for Point

Add a global operator<< function for the Point class in the starting code: std::ostream& operator<<(std::ostream& os, const Point& p) { os << p.x << " " << p.y; return os; }. In main print the point with std::cout << p;.

Starter code

main.cpp
#include <iostream>

class Point {
public:
    int x;
    int y;
    Point(int x_, int y_) : x(x_), y(y_) {}
    // The output operator is defined outside the class
};

// Add the output operator here

int main() {
    Point p(3, 7);
    // Print p with std::cout
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Point {
public:
    int x;
    int y;
    Point(int x_, int y_) : x(x_), y(y_) {}
};

std::ostream& operator<<(std::ostream& os, const Point& p) {
    os << p.x << " " << p.y;
    return os;
}

int main() {
    Point p(3, 7);
    std::cout << p;
    return 0;
}

Expected output

3 7

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Stream operator signature

Question
std::ostream& ____(std::ostream& os, const Point& p) { os << p.x; return os; }
Answer
std::ostream& operator<<(std::ostream& os, const Point& p) { os << p.x; return os; }

Explanation: operator<< is the output stream operator.

2 Match each piece to its role

Term Meaning
operator<< Writing to output stream
operator>> Reading from input stream
std::ostream& Return type
return os Returning the stream

3 Why is operator<< defined as a global function?

main.cpp
std::ostream& operator<<(std::ostream& os, const Point& p);
  • Because it is faster
  • Because the compiler requires it
  • Because it cannot be written inside the class
  • Because the left side is std::ostream Correct answer

Explanation: The left side is not your class but std::ostream.

All lessons in Intermediate