By Stephen R. Davis

Many beginning C++ programs read from the cin input object and output through the cout output object. Perhaps you haven’t really thought about it much, but this input/output technique is a subset of what is known as stream I/O.

Stream I/O is too large a topic to be covered completely in any number of simple articles — entire books are devoted to this one topic. Fortunately, there isn’t all that much that you need to know about stream I/O to write the vast majority of programs.

Stream I/O is based on overloaded versions of operator>>() and operator<<(). The declaration of these overloaded operators is found in the file iostream. The code for these functions is included in the standard library, which your C++ program links with.

The following code shows just a few of the prototypes appearing in iostream:

//for input we have:
istream& operator>>(istream& source, char   *pDest);
istream& operator>>(istream& source, string &sDest);
istream& operator>>(istream& source, int    &dest);
istream& operator>>(istream& source, double &dest);
//...and so forth...
//for output we have:
ostream& operator<<(ostream& dest, char   *pSource);
ostream& operator<<(ostream& dest, string &sDest);
ostream& operator<<(ostream& dest, int     source);
ostream& operator<<(ostream& dest, double  source);
//...and so it goes...

When overloaded to perform I/O, operator>>() is called the extractor and operator<<() is called the inserter. The class istream is the basic class for input from a file or a device such as the keyboard. C++ opens the istream object cin when the program starts. Similarly, ostream is the basis for output.

The prototypes above are for inserters and extractors for pointers to null terminated character strings (like “My name”), for string objects, for ints, and for doubles.