C++

What are the Stream Classes in C++?

C++ streams are the characters that are transferred between the input or output of that process. Many C++ stream classes are eligible and defined; files and their streams provide operations regarding input/output. All the classes and structures containing all the files and the folders with their hierarchies are mentioned inside the file with the standard library STREAM.h. The classes that are associated with the stream of C++ contain the IOS class, OSTREAM class, and ISTREAM class. We will discuss all possible examples regarding the classes of streams in C++.

C++ is a programming language that supports two input/output systems: One is taken from the C language and the other through an object-oriented input/output system described by C++. Both the I/O streams of C and C++ are integrated completely. Every program that we use always takes some content as input and releases the processed data in the form of output by using the input to process to output cycle.

A system may act like a source or a destination. The stream that acts as a source to provide any content in the form of input is called a san input stream. The destination stream that gets/receives the output from the process is known as the output stream.

How do the streams work?

The working of streams is described as following:

  • The first step is to initialize a stream with the right type used to execute a program.
  • Secondly, you mention the occurrence of I/O, which will occur using the get or put pointers.
  • Search for the accurate location. After finding the right location in a stream, you can perform input and output tasks by using the forward and reverse angular brackets ‘>>’ and ‘<<‘ operators.

Hierarchy of stream classes

The topmost class in the hierarchy of a stream class is the ‘IOS’ class. It acts as a base class for OSTREAM, ISTREAM, and STREAMBUF classes. The IOSTREAM class, by using ISTREAM and OSTREAM, indirectly inherits the IOS class. If you want to neglect the data and the member function of the IOS class to be duplicated, we declared this class as a virtual base class while inheriting OSTREAM an instream as:

class istream/ostream : virtual public ios

{

};

Services/advantages of stream classes

  • The ios class provides all input and output facilities to all other stream classes, either formatted or unformatted I/O operations.
  • The ISTREAM handles the input streams. It uses many functions to handle chars, strings, and objects like get, getline, read, ignore and putback, etc.
  • The input and output operators are type-safe. These are easier to use as compared to scanf() and printf(). Moreover, both operators are least effective as compared to those that are used in streams.

Now we will explain each type of stream class with the help of examples.

Istream class

Istream is a part of the ios class and deals with all the input streams present within the stream. Here we will first introduce an IOSTREAM library.

# include <iostream>

In the main program, we introduce a variable and then get its value through the get().

# Cin.get();

The user is asked to enter a value that is stored in the variable, and then the value will be displayed from this particular variable.

We will need a compiler to compile and execute the code, that is “g++” in the case of C++ language.

$ g++ -o s s.c

$ ./s

When we execute the commands to get the desired output, the system waits for the user to enter a word to be displayed later. As it is an input stream, we will enter a value. As in this example, we have entered ‘H’.

If a similar concept is applied on an array, in the case when you are interested in taking more than a single value from the user, we will initiate an array with the help of a ‘FOR’ loop by applying the array size of 10 words.

Then we will enter 10 characters; they are saved and then displayed by the code.

Ostream class

This class is also a part of the ios class, here the first char that is initialized is scanned, and then this character is displayed through the ostream function to write or put the input value to the function. Take an example in which we get the value in a string, but it takes the first character as we have a single variable to store the value. This value is displayed by using the put() function.

# Cout.put(a);

The system will prompt with a blank space for the user to enter any word. We have entered a 5 letter word, but the system will display only the first one.

Iostream class

This class is next to the ios class in the hierarchy of streams, which is needed for both the input and the output streams. Because these both stream classes inherit into the main base class, we will use an example to display an iostream class that has functions of writing and printing the values in the input stream.

# Cout.write("Aqsa_Yasin_12996", 10);

In the same statement, we have taken the input and then displayed it. Input is taken through the ‘write’ function; a condition is applied to display the string up to 10 bytes, while the rest of the string will be discarded. The output is displayed below.

Ios class

This class is at the top of the whole hierarchical structure of the C++ stream. This ‘ios’ class provides all the input and output services to all other classes in the stream of C++. We will take an example in which the user will not enter the value. We take a string in the code to be printed when the code is executed.

You can see the result.

Istream_with assign class

This class is a variant for the istream class that provides access to provide the class with the objects. This class acts as a primary class to the other classes in the istream class. This example will show the object assignment using std.

# Std :: cin.get(istream_withassign, 7);

When the code is executed, the screen waits for the value. After that, the string is partitioned at the byte we have mentioned in the code. Like we have used ‘7’, so the string will be partitioned at 7.

Ostream_withassign class

Assigns the ostream class object. All built-in functions like ‘cout’, ‘cerr’, ‘clog’ are already present objects for the same class. The example is used here to explain ostream_withassign class that creates an object of the class.

The same concept of the previous example is applied here with 10 bytes.

Conclusion

This article explains all the classes of streams in C++. The usage and advantages of using these streams are also mentioned. If the user involvement is not needed, only an output stream can be utilized; otherwise, both I/O streams are essential to execute a source code in C++. In this guide, you will learn about the hierarchy of streams and their dependencies with the help of examples.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.