Updated April 14, 2023
Definition of C++ Stream
Stream in C++ means a stream of characters that gets transferred between the program thread and input or output. There are a number of C++ stream classes eligible and defined which is related to the files and streams for providing input-output operations. All the classes and structures maintaining the file and folders with hierarchies are defined within the file with iostream.h standard library. Classes associated with the C++ stream include ios class, istream class, and ostream class. Class ios is indirectly inherited from the base class involving iostream class using istream class and ostream class which is declared virtually.
C++ Stream Classes with Examples
There is a number of stream classes in the hierarchy which is defining and giving different flows for the varied objects in the class. The hierarchy is maintained in a way where it gets started from the top class which is the ios class followed by all the other classes involving istream class, ostream class, iostream class, istream_withassign class, and ostream_withassign class. The iosclass in the hierarchy is the parent class which is considered as a class from where both the istream and ostream class gets inherited. Both the istream class and ostream class constitute the ios class which is the highest level of the entire hierarchy of C++ stream classes. The other classes which include functions for the operations include assignment operation like _withassign classes.
Various stream classes in C++ are as follows:
1. istream Class
istream being a part of the ios class which is responsible for tackling all the input stream present within the stream. It provides all the necessary and important functions with the number of functions for handling all the strings, chars, and objects within the istream class which comprises all these functions such as get, read, put, etc.
Example
This program illustrates the istream class which takes a variable as an input then it makes use of the inbuilt functions like get to tackle and handle the input stream with the output value as an input being provided to the function as shown in the output.
#include <iostream>
using namespace std;
intmain()
{
char p;
cin.get(p);
cout<< p;
}
Output:
2. ostream Class
This class as part of the ios class is also considered as a base class that is responsible for handling output stream and provides all the necessary functions for handling chars, strings, and objects such as put, write, etc.
Example
This program demonstrates the ostream class as part of the ios class where the first initialized char defined is scanned and then it gets the scanned character and the ostream function takes care to write or put the value to the function.
#include <iostream>
using namespace std;
intmain()
{
char r_t;
cin.get(r_t);
cout.put(r_t);
}
Output:
3. iostream Class
iostream class is the next hierarchy for the ios class which is essential for input stream as well as output stream because istream class and ostream class gets inherited into the main base class. As the name suggests it provides functionality to tackle the objects, strings, and chars which includes inbuild functions of put, puts, get, etc.
Example
This program is used to demonstrate the iostream class which comprises functions like write to print the input stream with the required number of values as input as shown in the output.
#include <iostream>
using namespace std;
intmain()
{
cout.write("educba_portal", 9);
}
Output:
4. ios Class
ios class is the highest class in the entire hierarchical structure of the C++ stream. It is also considered as a base class for istream, stream, and streambuf class. It can be said that the ios class is basically responsible for providing all the input and output facilities to all the other classes in the stream class of C++.
Example
This program demonstrates the ios class which comprises of the iostream.h as a standard library to derive the values for input and output stream which is part of the ios class as shown in the output.
#include <iostream>
using namespace std;
intmain()
{
cout<<"Get the value for the _io_stream generation";
return 0;
}
Output:
5. istream_withassign Class
This class is considered as a variant for the istream class that provides the class privilege for the class to assign object. The predefined object which can be called a build in the function of this class is used which is responsible for providing getting the stream facility and thus allows the object to reassign at the run time for different stream objects. Istream_withassign class acts as the primary class for the other classes as part of the istream class.
Example
This program demonstrates the istream_withassign class which is responsible for creating the object of the class as shown in the given output.
#include <iostream>
using namespace std;
intmain()
{
char istream_withassign[8];
std::cin.get(istream_withassign, 8);
std::cout<< istream_withassign << '\n';
std::cin.get(istream_withassign, 8);
std::cout<< istream_withassign << '\n';
return 0;
}
Output:
6. ostream_withassign Class
This class is responsible for providing object assigned to the class and is considered as a variant itself for the ostream class of the C++ stream. All the build-in functions such as cout, cerr, clog is the already present objects of the same class and are reassigned at execution time for the different ostream object.
Example
This program demonstrates the ostream_withassign class which is responsible for creating the object of the class as shown in the given output.
#include <iostream>
using namespace std;
intmain()
{
char ostream_withassign[10];
std::cin.get(ostream_withassign, 10);
std::cout<<ostream_withassign<< '\n';
std::cin.get(ostream_withassign, 10);
std::cout<<ostream_withassign<< '\n';
return 0;
}
Output:
Conclusion
C++ Stream is a very powerful and versatile functionality of the stream classes. They provide programmers an insight for using the predefined and in build functions by modification in the object and the standard libraries of the class for various manipulations and arrangements of the files and folders thus maintaining the hierarchical structure intact for the C++ stream.
Recommended Articles
This is a guide to C++ Stream. Here we also discuss the definition and c++ stream classes along with examples and its code implementation. You may also have a look at the following articles to learn more –