C++ Input/Output operations is used the stream concept. Stream is the sequence of bytes or flow of data. It makes the performance fast. If data flow from main memory to device like printer, display screen, etc. This is called as output operator. If the data flow from device like keyboard, joystick, etc to main memory, this is called as input operation.
Standard Output Stream(cout):
The cout is a predefined object of ostream class. It is connected with standard output device , which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console.
- cout is used to display the data on the screen.
- The operator “<<” is called as insertion operator or put to operator.
- The Insertion operator can be overloaded.
- Insertion operator is similar to the printf() operator in C.
- cout is the object of ostream class .
- Data flow direction is from variable to output device.
- We can still use printf() for displaying an output.
- Multiple outputs can be displayed using cout.
Example:
#include<iostream.h>
void main()
{
cout<<“Hello World”;
}
Standard Input Stream (cin):
The cin is a predefined object of istream class. It is connected with the standard input device, such as keyboard. The cin object is used in conjunction with stream extraction operator (>>) to read the input from a console.
- cin is used for accepting data from the keyboard.
- The operator “>>” called as extraction operator or get from operator.
- The extraction operator can be overloaded.
- cin is the object of istream class.
- Extraction operator is similar to the scanf() operator in C.
- cin is the object of istream class.
- Data flow direction is from input device to variable.
- Due to the use of input statement, the program will wait till the user type some input and that input is stored in variable.
- Multiple input can be accepted using cin.
Example:
#include<iostream.h>
void main()
{
int a,b,c;
cout<<“Enter any two numbers”;
cin>>a>>b;
c=a+b;
cout<<“The addition result is :”<<c;
}