Sunday 9 March 2014

What is Stream in C++?

Streams in C++


Streams:


A stream is an interface between the user and I/O devices. Streams flow the data between the user and I/O devices in bytes in sequence. Streams are divided into two types i.e. input streams and output streams. The input stream is also called source stream and the output stream is called destination stream. The input stream sends data from the input devices to computer system while the output stream sends data from computer system to the output devices. C++ provides various stream classes in which different member functions and  member data are declared for different input/output operations on the stream. The object cin of class istream is used for console input operations while the object cout of class ostream is used for console output operations on the stream. The out stream class ostream also provides two other objects clog and cerr. These objects control the error messages. The object sin is usually associated with the monitor. The I/O stream classes are declared in the header file iostream.h. So you must include his header file while using the I/O stream classes.



The Stream Extraction and Insertion Operators:


The stream extraction means reading data from the input devices while the stream insertion means sending data to output devices. The extraction operator >> is used for the stream extraction and the insertion operator << is used for the stream insertion. The stream extraction operation >> is implemented as a set of overloaded members of the istream class to support reading basic types of any input stream, while the stream insertion operator << is implemented as a set of overloaded members of the ostream class to support writing basic types of data from any any output stream. These operators read and write the data and ignore the white space character. A white space character is always a delimiter between values when using the stream extraction and insertion operators. The stream extraction operator when reads the string and during reading when it founds a white space character then it stops the reading process and ignores the next after the white space character. The stream extraction and insertion operators are used with the istream and ostream objects.


The cin Stream object:


This object is used for input. Whenever you want to send data to the variables from the keyboard then this object is a source through which you can send data to the variables.This object takes input values from the keyboard and sends them to the variables. The processor then processes the input of cin object and produces output. Following is the general syntax of cin stream object:

cin >> variable1 >> variable2 >> variable3 >> ------------------->>variable-n;

When you want to enter values to more variables using a single cin stream object then these variables should be distinguished from each other using the stream etraction operator >> . You can also use a separate cin stream object for each variable.


The cout stream object:


This object is used for output. Whenever you want to receive the output from the variables or to display your own message then for such purposes this object is used. This objects takes output values from the variables and send them to the standard output devices.
Following are the general syntaxes of cout stream object:

cout << variable1 << variable2 << variable3 << ------------------- << variable-n;
                                               or
cout << "message1" << "message2" << "message3" << ------------------- << "message-n";

Whenever you want to display a message through cout stream object then the message should be enclosed with double quotes and when you want to display value of variables then in such case the using of double quotes should be ignored because double quotes instruct cout stream object to display only this message enclosed with me. When you write the name of variable without double quotes then cout stream object displays the contents of that variable.

                                        Program # 1
Write a C++ Program to read two integer values from the keyboard and display them on the screen.


                                         Program
#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
cout<<”Enter the value for x and y =”;
cin>>x>>y;
cout<<”your values are =”;
cout<<x<<y;
getch();
}

Since for each variable we can also use a separate cin and cout stream objects. So the above program can also be written as:


#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
cout<<”Enter the value for x and y =”;
cin>>x;
ccin>>y;
cout<<”your values are =”;
cout<<x;
cout<<y;
getch();
}




Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.

0 comments:

 
Search Engine Submission - AddMe url submit Casino us org Seo Packages http://blogsiteslist.com