0% found this document useful (0 votes)
5 views8 pages

Stream 00

In C++, streams are used for input and output operations and are defined in the iostream.h header file. There are classes like ios, istream, and ostream that define the hierarchy for handling input, output, and both. The istream class handles input streams while ostream handles output streams.

Uploaded by

bxscrwn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Stream 00

In C++, streams are used for input and output operations and are defined in the iostream.h header file. There are classes like ios, istream, and ostream that define the hierarchy for handling input, output, and both. The istream class handles input streams while ostream handles output streams.

Uploaded by

bxscrwn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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.

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.
1. ios class is topmost class in the stream classes
hierarchy. It is the base class for istream,
ostream, and streambuf class.

2. istream and ostream serves the base classes


for iostream class. The class istream is used for
input and ostream for the output.

3. 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.

1. The ios class: The ios class is responsible for


providing all input and output facilities to all other
stream classes.

2. 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:

 CPP14
#include <iostream>
using namespace std;

int main()
{
char x;

// used to scan a single char


cin.get(x);

cout << x;
}

Input:
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:

 CPP14
#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);
}

1. Input:
g

Output:
g

The iostream: This class is responsible for handling


both input and output stream as both istream class and
ostream class is inherited into it. It provides function
of both istream class and ostream class for handling
chars, strings and objects such as get, getline, read,
ignore, putback, put, write etc..
Example:

 CPP14
#include <iostream>
using namespace std;

int main()
{

// this function display


// ncount character from array
cout.write("geeksforgeeks", 5);
}

Output:
geeks

istream_withassign class: This class is variant


of istream that allows object assignment. 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.

 CPP14

#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:
45

Output:
Enter two numbers dx and dy

45

dx = 4 dy = 5

ostream_withassign class: This class is variant


of ostream that allows object assignment. 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.

 CPP14
#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

45

You might also like