C++ Stream Classes Structure
In C++ there are number of stream classes for defining various streams related with files and for doing input-output operations. All these classes are defined in the file iostream.h. Figure given below shows the hierarchy of these classes.
- ios class is topmost class in the stream classes hierarchy. It is the base class for istream, ostream, and streambuf class.
- istream and ostream serves the base classes for iostream class. The class istream is used for input and ostream for the output.
- 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 as
class istream: virtual public ios { }; class ostream: virtual public ios { };
- The _withassign classes are provided with extra functionality for the assignment operations that’s why _withassign classes.
Facilities provided by these stream classes.
- The ios class: The ios class is responsible for providing all input and output facilities to all other stream classes.
- The istream class: This class is responsible for handling input stream. It provides number of function for handling chars, strings and objects such as get, getline, read, ignore, putback etc..
Example:#include <iostream>
using
namespace
std;
int
main()
{
char
x;
// used to scan a single char
cin.get(x);
cout << x;
}
chevron_rightfilter_noneInput:
g
Output:
g
- The ostream class: This class is responsible for handling output stream. It provides number of function for handling chars, strings and objects such as write, put etc..
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);
}
chevron_rightfilter_noneInput:
g
Output:
g
- The iostream: This class is responsible for handling both input and output stream as both istream class and istream class is inherited into it. It provides function of both istream class and istream class for handling chars, strings and objects such as get, getline, read, ignore, putback, put, write etc..
Example:#include <iostream>
using
namespace
std;
int
main()
{
// this function display
// ncount character from array
cout.write(
"geeksforgeeks"
, 5);
}
chevron_rightfilter_noneOutput:
geeks
- istream_withassign class: This class is variant of istream that allows object assigment. The predefined object cin is an object of this class and thus may be reassigned at run time to a different istream object.
Example:To show that cin is object of istream class.#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;
}
chevron_rightfilter_noneInput:
4 5
Output:
Enter two numbers dx and dy 4 5 dx = 4 dy = 5
- ostream_withassign class: This class is variant of ostream that allows object assigment. The predefined objects cout, cerr, clog are objects of this class and thus may be reassigned at run time to a different ostream object.
Example:To show that cout is object of ostream class.#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, cin) ;
}
chevron_rightfilter_noneOutput:
Value of dx and dy are 4 5
Related Articles:
Recommended Posts:
- Anonymous classes in C++
- Trivial classes in C++
- Nested Classes in C++
- C++ Classes and Objects
- Local Classes in C++
- Storage Classes in C++ with Examples
- File Handling through C++ Classes
- Virtual functions in derived classes
- Overloading stream insertion (<>) operators in C++
- Pure Virtual Functions and Abstract Classes in C++
- Catching base and derived classes as exceptions
- Enum Classes in C++ and Their Advantage over Enum DataType
- Fork CPP | Course Structure
- Structure vs class in C++
- STL Priority Queue for Structure or Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.