Stream Insertion and Extraction Operators
Teach your types to interoperate with std::cout via >.
Stream Insertion and Extraction Operators is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Why Overload << and >>
Teach your types to interoperate with std::cout and std::cin by overloading the stream operators.
The Insertion Operator <<
Define as a free function taking an output stream by reference. Return the stream to enable chaining.
struct Point { int x, y; };
std::ostream& operator<<(std::ostream& os, const Point& p) {
return os << "(" << p.x << ", " << p.y << ")";
}
Point p{3, 4};
std::cout << p << "\n"; // (3, 4)Why a Free Function?
If you wrote it as a member, the left operand would have to be a Point — impossible because we need std::ostream on the left. Free functions support either ordering.
The Extraction Operator >>
Define for reading from input streams. Validate and set the failbit on failure.
std::istream& operator>>(std::istream& is, Point& p) {
char open, comma, close;
is >> open >> p.x >> comma >> p.y >> close;
if (open != '(' || comma != ',' || close != ')')
is.setstate(std::ios::failbit);
return is;
}Setting Stream Failure State
If parsing fails, call is.setstate(std::ios::failbit). The caller can detect failure with !is or is.fail().
Friend Declarations
If the operator needs access to private members, declare it as a friend inside the class.
class Point {
int x_, y_;
public:
friend std::ostream& operator<<(std::ostream& os, const Point& p);
};Templated Stream Types
For maximum portability, template on the character type. Useful for wide-character streams.
template <typename CharT>
std::basic_ostream<CharT>& operator<<(
std::basic_ostream<CharT>& os, const Point& p) {
return os << "(" << p.x << ", " << p.y << ")";
}Chaining
Returning the stream lets users chain operations.
std::cout << "Result: " << point << ", distance: " << dist;Custom Formatting
Use std::ios::fmtflags or the C++20 std::format for advanced formatting. Streams are simpler but harder to localize.
JSON-style Output
You can layer formatting choices in your operator. For complex output, write helper functions.
Reading Multiple Lines
Use std::getline for line-based reading, then parse with a stringstream — keeps your operator>> simple.
Performance Tip
For very high-throughput logging, prefer dedicated libraries (spdlog, fmt) — they outperform streams significantly.
Quick Check
Why is operator<< for a user type defined as a free function rather than a member?
Recap
Overload operator<< and operator>> as free functions taking the stream by reference. Return the stream for chaining. Set the failbit on parsing errors. Use friend when private access is needed.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Stream Insertion and Extraction Operators” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Stream Insertion and Extraction Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Stream Insertion and Extraction Operators”?
Teach your types to interoperate with std::cout via >. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Stream Insertion and Extraction Operators” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arithmetic and Comparison Operators
- Stream Insertion and Extraction Operators
- Subscript and Function Call Operators
- Three-Way Comparison Operator C++20