C++ Stream Classes Structure
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:
#include <iostream>
using namespace std;
int main()
{
char x;
// used to scan a single char
cin.get(x);
cout << x;
}
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:
#include <iostream>
using namespace std;
int main()
{
char x;
// used to scan a single char
cin.get(x);
// used to put a single char onto the screen.
cout.put(x);
}
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:
#include <iostream>
using namespace std;
int main()
{
// this function display
// ncount character from array
cout.write("geeksforgeeks", 5);
}
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:
#include <iostream>
using namespace std;
class demo {
public:
int dx, dy;
// operator overloading using friend function
friend void operator>>(demo& d, istream& mycin)
{
// cin assigned to another object mycin
mycin >> d.dx >> d.dy;
}
};
int main()
{
demo d;
cout << "Enter two numbers dx and dy\n";
// calls operator >> function and
// pass d and cin as reference
d >> cin; // can also be written as operator >> (d, cin) ;
cout << "dx = " << d.dx << "\tdy = " << d.dy;
}
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:
#include <iostream>
using namespace std;
class demo {
public:
int dx, dy;
demo()
{
dx = 4;
dy = 5;
}
// operator overloading using friend function
friend void operator<<(demo& d, ostream& mycout)
{
// cout assigned to another object mycout
mycout << "Value of dx and dy are \n";
mycout << d.dx << " " << d.dy;
}
};
int main()
{
demo d; // default constructor is called
// calls operator << function and
// pass d and cout as reference
d << cout; // can also be written as operator << (d, cout) ;
}
Output
Value of dx and dy are 4 5