C++ Stream and Stream Classes

C++ supports two complete I/O systems: the one inherited from C and the objectoriented I/O system defined by C++ (hereafter called simply the C++ I/O system). Like C-based I/O, C++’s I/O system is fully integrated. The different aspects of C++’s I/O system, such as console I/O and disk I/O, are actually just different perspectives on the same mechanism.Every program takes some data as input and generates processed data as output following the input-process-output cycle.

C++ supports all of C’s rich set of I/O functions that can be used in the C++ programs.But these are restrained from using due to two reasons ,first I/O methods in C++ supports the concept of OOP and secondly I/O methods in c can not handle the user defined data types such as class objects.C++ uses the concept of streams and stream classes to implement its I/O operation with the console and disk fils is linked to a physical device by the I/O system.

All streams behave in the same way even though the actual physical devices they are connected to may differ substantially. Because all streams behave the same, the same I/O functions can operate on virtually any type of physical device. For example, one can use the same function that writes to a file to write to the printer or to the screen. The advantage to this approach is that you need learn only one I/O system.

A stream act like a source or destination.The source stream that provide data to the program is called the input stream and the destination stream that receive output from the program is called the output stream.C++ containes cin and cout predefined streamsthat opens automatically when a program begins its execution.cin represents the
input stream connected to the standard input device and cout represents the output stream

The C++ 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.Figure below shows the hierarchy of the stream classes used for input and  output operstions with the console unit.

These classes are declared in the header file iostrem. The file should be included in all programs that communicate with the console unit.


As in figure above ios is the base class for istream(input stream) and ostream(output stream) which are base classes for iostream(input/output stream).The class ios is declared as the virtual base class so that only one copy of its members are inherited by the iostream.

The class ios provides the basic support for formatted and unformatted input/output operations.The class istream provides the facilities for formatted and unformatted input while the class ostream(through inheritance) provides the facilities for formatted output.

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

Stream classes for console operations