Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.

This answer is restricted. Please login to view the answer of this question.

Login Now

In C++ stream refers to the stream of characters that are transferred between the program thread and i/o.

Stream classes in C++ are used to input and output operations on files and io devices. These classes have specific features and to handle input and output of the program.

The iostream.h library holds all the stream classes in the C++ programming language.

Heirarchy of Stream Classess in iostream.h

  1. ios class is topmost class in the stream classes hierarchy. It is the base class for istream, ostream, and streambuf class.
  2. istream and ostream serves the base classes for iostream class. The class istream is used for input and ostream for the output.
  3. Class ios is indirectly inherited to iostream class using istream and ostream. To avoid the duplicity of data and member functions of ios class, it is declared as virtual base class when inheriting in istream and ostream.

Program Part:

Below is the program that overwrite the extraction and insertion operator

#include <iostream>
using namespace std;

class Complex
        int real, imag;

        Complex(int r = 0, int i = 0)
            real = r;
            imag = i;

    friend ostream &operator<<(ostream &out, const Complex &c){
        out << c.real;
        out << "+i" << c.imag << endl;
        return out;

    friend istream &operator>>(istream &in, Complex &c){
        cout << "Enter Real Part ";
        in >> c.real;
        cout << "Enter Imaginary Part ";
        in >> c.imag;
        return in;

int main()
    Complex c1;
    cin >> c1;
    cout << "The complex object is ";
    cout << c1;
    return 0;

The output of above program is

Enter Real Part 1
Enter Imaginary Part 2
The complex object is 1+i2



If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

  Loading . . .