Stream I

Stream I - Stream I/O INTRODUCTION TO STREAM I/O PART 1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Stream I/O INTRODUCTION TO STREAM I/O PART 1 - OVERLOADING << In this issue we will begin discussing the stream I/O package that comes with C++. The first four sections of this issue are related and present several aspects of stream I/O along with some related topics. If you've used C++ at all, you've probably seen a simple example of how to do output: cout << "Hello, world" << "\n"; instead of: printf("Hello, world\n"); cout is an output stream, kind of like stdout in C. The C example could be written as: fprintf(stdout, "Hello, world\n"); which makes this correspondence a bit clearer. Once you get beyond simple input/output usage, what is the stream I/O package good for? One quite useful thing it can do is to allow the programmer to take control of I/O for particular C++ types such as classes. This end is achieved by the use of operator overloading. Suppose that we have a Date class: class Date { int month; int day; int year; public: Date(char*); Date(int, int, int); }; with an internal representation of a Date using three integers for month, day, and year, and a couple of constructors to create a Date object. How would we output the value of a Date object? One way would be to devise a member function: void out(); implemented as: void Date::out() { printf("%d/%d/%d", month, day, year); } This function would operate on an object instance of a Date and would access the month/day/year members and display them. This approach will certainly work and may be suitable in some kinds of applications. But this scheme doesn't integrate very well with stream I/O. For example, I cannot say: Date d(9, 25, 1956); cout << "Today's date is " << d;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
but must say: printf("Today's date is "); d.out(); For this purpose it is necessary to overload the << operator. We can add a friend function to Date: friend ostream& operator with definition: { return os << d.month << "/" << d.day << "/" << d.year; } With this definition, it is possible to say: Date d(9, 25, 1956); cout << "Today's date is " << d << "\n"; Several aspects of this example need explanation. An overloaded operator in C++ is an operator like "+" or "<<" that is given a special meaning for certain kinds of arguments, and turns into a function. Wherever the operator is used with these arguments, a function is called. So, for example, an output statement: cout << "xxx"; is actually: cout.operator<<("xxx"); which is a valid function call in C++ if you're using stream I/O. cout is an instance of class ostream (at least conceptually; the actual hierarchy is a bit complicated). When we wrote the actual statement to output a formatted Date: return os << d.month << "/" << d.day << "/" << d.year; we returned the ostream reference so as to allow << usage to be chained. Because << operators group left to right, a sequence like: cout << "x" << "y"; actually means: cout.operator<<("x").operator<<("y"); Finally, the reason that we declared operator<<(ostream&, const Date&) as a friend
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 12

Stream I - Stream I/O INTRODUCTION TO STREAM I/O PART 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online