Explain stream class, stream class hierarchy and stream manipulator of C++.

Stream class: – As a programmer learning OOP, we may want to delve in to the detail of classes in the iostream library, but we do not have to know much about these classes to use the library for simple I/O. To begin with, we should be familiar with the concept of a stream and know the names of the predefined streams. The idea of a stream is a sequence of bytes, features prominently in UNIX and C. As a C programmer, we have heard the term stream in connection with ANSI C’s file I/O functions that are prototype in the header file <stdio.h>. A stream serves as an abstract model of I/O devices such as disk files, the keyboard, the video display, or even a buffer in memory. In ANSI C, each stream has an associated file structure that holds the state of the stream and such data item as the buffer for buffered I/O.

The C++ iostream library is an object-oriented implementation of the abstraction of a stream as a flow of bytes from source to a sink. The iostream library includes input streams, output stream and streams which are istream, ostream an iostream classes respectively. The istream class provides the functionality of scanf and fscanf, and ostream includes capability similar to those of print and fprintf. Like predefined C stream stdin, stdout and stderr, the iostream includes four pre-defined streams.

  1. cin is an input stream connected to the standard input. It is analogous to C’s stdin.
  2. cout is an output stream connected to the standard output and is analogous to stdout in C.
  3. cerr is an output stream set up to provide unbuffered output to the standard error device. This is the same as C’ stderr.
  4. clog is like cerr, but it is fully buffered stream like cin and cout.

Stream class hierarchy: – All the stream classes are derived from the base class ios, which stores the state of the stream and handles error. The ios class has an associated streambuf object that acts as the buffer for the stream. The istream and ostream classes, devided from ios, are meant for input and output, respectively. The iostream class uses multiple inheritance to acquire the capabilities of both istream and ostream class and there for support both input and out. The classes iostream_withassign, ostream_withassign and istream_withassign are derived from istream, ostream and iostream, respectively, by adding the definition of the assignment operator (=) so that we can redirected I/O by assigning one stream to another. The predefined stream cout, cerr and clog are of class ostream_withassign where as cin is an instance of istream_withassign class. A stream class hierarchy is –

stream class hierarchy in C++

Stream manipulator: – Some special functions known as manipulator, which are written in such a way that by placing a manipulator in the chain of << operators, we can alter the of the stream. The flush manipulator forces out to display its output without waiting for its buffer to get filled up, the buffer is flushed. The endl manipulator sends a new line to the stream and also flushed the buffer