C++ Stream Classes

Stream in C++:

A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. The source stream that provides data to the program is called Input Stream and the destination stream that receives output from the program is called Output Stream.

In C++, the I/O system contains a hierarchy of classes that are used to define various streams to deal with both the console and disk files. These classes are called stream classes. The stream classes are used for input and output operations with the console unit. These classes are declared in the header file iostream. This file should be included in all the programs that communicate with the console unit.

The class ios is declared as the virtual base class so that only one copy of its members is inherited by the iostream. The class ios provides the basic support for formatted and unformatted I/O operations. The class istream provides the facilities for formatted and unformatted input while the class ostream provides the facilities for formatted output.

The class iostream provides the facilities for handling both input and output streams. Three classes namely, istream_withassign, ostream_withassign and iostream_withassign is used to add assignment operators to these classes.

Stream Classes for Console Operations:

ios: It contains basic facilities that are used by all other input and output classes. It also contains a pointer to a buffer object (streambuf object). It declares constants and functions that are necessary for handling formatted input and output operations.

istream: It inherits the properties of ios. It declares input functions such as get(), getline() and read(). It contains overloaded extraction operator >>.

ostream: It inherits the properties of ios. It declares output functions put() and write(). It contains overloaded insertion operator <<.

iostream: It inherits the properties of ios istream and ostream through multiple inheritances and thus contains all the input and output functions.

streambuf: It provides an interface to physical devices through buffers. It acts as a base for filebuf class used ios files.