CSC166 ยท 2075 ยท 10 marks
Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.
Briefly explain the hierarchy of stream classes. Write a program that overloads extraction and insertion operators.[10]
Object Oriented ProgrammingChapter 8: File handlingStream Class Hierarchy for Console Input/Output
Quick answer
C++ provides a rich set of stream classes for performing input/output operations. These classes are organized in a hierarchy, all defined in the header files <iostream>, <fstream>, and <sstream>.
Hierarchy of Stream Classes and Operator Overloading
Part 1: Hierarchy of Stream Classes
Stream Class Hierarchy Diagram
ios (base class)
|
istream ostream
(input) (output)
\ /
\ /
iostream
(input+output)
/ | \
/ | \
ifstream fstream ofstream
(file in) (file in/out) (file out)
Description of Each Class
| Class | Header | Description |
|---|---|---|
ios | <ios> | Base class for all stream classes. Provides common properties like format flags, error state bits, etc. |
istream | <iostream> | Derived from ios. Handles input operations. Contains cin object and defines >> (extraction operator). |
ostream | <iostream> | Derived from ios. Handles output operations. Contains cout, cerr, clog objects and defines << (insertion operator). |
iostream | <iostream> | Derived from both istream and ostream. Supports both input and output. |
ifstream | <fstream> | Derived from istream. Used for reading from files. |
ofstream | <fstream> | Derived from ostream. Used for writing to files. |
fstream | <fstream> | Derived from iostream. Used for both reading and writing files. |
Key Points
coutis an object ofostreamclasscinis an object ofistreamclass- Writing to a file uses an
ofstreamorfstreamobject instead ofcout, but uses the same<<operator - Reading from a file uses an
ifstreamorfstreamobject instead ofcin, but uses the same>>operator
Part 2: Overloading Extraction (>>) and Insertion (<<) Operators
Important Rules (from notes)
coutis an object ofostreamclass andcinis an object ofistreamclass.- These operators must be overloaded as global functions (not member functions).
- To allow access to private data members, they must be declared as friend functions inside the class.
Syntax
friend ostream& operator<<(ostream& out, const ClassName& obj);
friend istream& operator>>(istream& in, ClassName& obj);
Complete Program
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor
Complex() {
real = 0;
imag = 0;
}
Complex(float r, float i) {
real = r;
imag = i;
}
// Friend function to overload insertion operator (<<)
// Used for output: cout << object
friend ostream& operator<<(ostream& out, const Complex& c) {
out << "Real Part : " << c.real << endl;
out << "Imaginary Part: " << c.imag << endl;
return out;
}
// Friend function to overload extraction operator (>>)
// Used for input: cin >> object
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, c2;
// Using overloaded >> operator
cout << "--- Input for Complex Number 1 ---" << endl;
cin >> c1;
cout << "--- Input for Complex Number 2 ---" << endl;
cin >> c2;
// Using overloaded << operator
cout << "\n--- Complex Number 1 ---" << endl;
cout << c1;
cout << "--- Complex Number 2 ---" << endl;
cout << c2;
return 0;
}
Sample Output
--- Input for Complex Number 1 ---
Enter real part: 3
Enter imaginary part: 4
--- Input for Complex Number 2 ---
Enter real part: 1
Enter imaginary part: 2
--- Complex Number 1 ---
Real Part : 3
Imaginary Part: 4
--- Complex Number 2 ---
Real Part : 1
Imaginary Part: 2
Explanation of Key Points
| Point | Explanation |
|---|---|
friend keyword | Allows the global function to access private members real and imag |
ostream& out | Reference to the output stream object (cout) |
istream& in | Reference to the input stream object (cin) |
return out / return in | Returning the stream reference allows chaining like cout << c1 << c2 |
| Global function | These are NOT member functions; they are defined outside the class |
Note: The operators must return a reference to the stream (
ostream&oristream&) so that chained operations likecout << c1 << c2work correctly.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.