---
title: "Stream class hierarchy"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:stream-class-hierarchy"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/stream-class-hierarchy
site: BCT Engineering
---

# Stream class hierarchy

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2074 Chaitra, 2 marks; 2072 Chaitra, 3 marks; 2072 Kartik, 3 marks; 2068 Baisakh, 2 marks (asked 4 times)
- Video: [Stream Class Hierarchy in C++](https://www.youtube.com/watch?v=Ovr6pNfq-fk) — E- Gurukul
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/stream-class-hierarchy
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Discuss about 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.

## Answer

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.

```text
            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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does the ios class hold?
   A. Only file names
   B. Format flags, state flags and a pointer to streambuf
   C. Only the buffer
   D. Only cin and cout
   Answer: B. ios is the base and holds format and state flags and the streambuf pointer. (Key point: Base class ios)

2. How is iostream related to istream and ostream?
   A. It is their base
   B. It derives only from ostream
   C. It is unrelated
   D. It derives from both
   Answer: D. iostream derives from both for input and output on one stream. (Key point: iostream)

3. Which objects belong to ostream?
   A. cin
   B. cout, cerr and clog
   C. ifstream only
   D. fstream only
   Answer: B. cout, cerr and clog are ostream objects; cin belongs to istream. (Key point: Objects)

## Related in Object Oriented Programming

- [Stream and file stream classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astream-file-classes) — Object Oriented Programming, chapter 8
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5

## Related in other subjects

- [Exception class hierarchy from Throwable](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aexception-class-hierarchy-throwable) — Object Oriented Analysis and Design, chapter 4
