VOOZH about

URL: https://www.geeksforgeeks.org/cpp/c-stream-classes-structure/

⇱ C++ Stream Classes Structure - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ Stream Classes Structure

Last Updated : 26 Aug, 2025

Stream classes form the foundation for handling input or output (I/O) operations. They provide a clean and efficient way to read data from sources like the keyboard or files and write data to destinations like the screen or files.

Stream Classes Hierarchy in C++

The stream classes are organized in a class hierarchy ( a family tree of classes). This helps the language handle different types of input/output operations using common features, while still supporting special cases like file I/O.

At the top of this hierarchy is the ios class, which serves as the base class for other key stream classes:

  • istream (input stream)
  • ostream (output stream)
  • streambuf (buffer management class)

Key Stream Classes

1. The ios Class

It is the topmost base class that provides essential input or output facilities to all other stream classes. It manages shared properties such as formatting and error states.

2.The istream Class

It is responsible for input operations. It provides functions for reading characters, strings, and objects such as:

  • get()
  • getline()
  • read()
  • ignore()
  • putback()

Example:

Input:

g

3. The ostream class

It is responsible for output operations. It provides functions to write characters, strings, and objects, such as:

  • write()
  • put()

Example:

Input:

g

Output:

g

4. The iostream Class

It inherits from both istream and ostream, making it capable of handling both input and output operations. It supports all functions from istream and ostream.

Example:


Output
geeks

The _withassign Variants

The stream library also provides _withassign versions of the input and output stream classes. These variants allow objects to be assigned and reassigned at runtime.

1. istream_withassign

  • A variant of istream that supports object assignment.
  • The predefined object cin is an instance of this class, allowing it to be reassigned to another input stream.

Example:

Input:

4 5

Output:

Enter two numbers dx and dy
4 5
dx=4 dy=5

2. ostream_withassign

  • A variant of ostream that supports object assignment.
  • The predefined objects cout, cerr, and clog are instances of this class, enabling reassignment to different output streams.

Example:


Output
Value of dx and dy are 
4 5


Comment
Article Tags:
Article Tags: