Skip to main content

Stream class hierarchy

Short answer

The stream hierarchy has ios as the base with formatting and state flags. istream (input, cin) and ostream (output, cout, cerr) derive from it, and iostream inherits both. File streams ifstream, ofstream, fstream extend istream/ostream/iostream, while streambuf manages the buffer.

Discuss about stream class hierarchy.

A stream is a flow of data between a program and a device (keyboard, screen, file). C++ stream classes are arranged in a hierarchy, declared in <iostream>, <fstream>, etc.

            ios_base
               |
              ios
       ________|________
      |                 |
   istream           ostream
      |  \           /   |
      |   \_iostream_/   |
  ifstream     |      ofstream
               |
            fstream
  • ios – base class; holds format flags, state flags (eofbit, failbit, badbit) and a pointer to streambuf.
  • istream – input class; provides >>, get(), getline(), read(). Object cin belongs to it.
  • ostream – output class; provides <<, put(), write(). Objects cout, cerr, clog belong to it.
  • iostream – derives from both istream and ostream for input and output on one stream.
  • ifstream, ofstream, fstream – file input, output and input/output classes (<fstream>).
  • streambuf – manages the buffer between the program and the device.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 8 questions →