Home | | Computer Science 11th std | C++ program: I/O Operators

Chapter: 11th Computer Science : Introduction to C++

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

C++ program: I/O Operators

Computer Science : Introduction to C++ : Input operator, Output Operator, Cascading of I/O operators

I/O Operators

 

Input operator:

 

C++ provides the operator >> to get input. It extracts the value through the keyboard and assigns it to the variable on its right; hence, it is called as “Stream extraction” or “get from” operator.

It is a binary operator i.e., it requires two operands. The first operand is the pre-defined identifier cin (pronounced as C-In) that identifies keyboard as the input device. The second operand must be a variable.


To receive or extract more than one value at a time, >> operator should be used for each variable. This is called cascading of operator.


Example:



• cin >> num;

Pre-defined object cin extracts a value typed on keyboard and stores  it in variable num.

•  cin >>x >> y;

This is used to extract two values. cin reads thefirst value and immediately assigns that to variable x; next, it reads the second value which is typed after a space and assigns that to y. Space is used as a separator for each input.

 

Output Operator:

 

C++ provides << operator to perform output operation. The operator << is called the “Stream insertion” or “put to” operator. It is used to send the strings or values of the variables on its right to the object on its left. << is a binary operator.

The first operand is the pre-defined identifier cout (pronounced as C-Out) that identifies monitor as the standard output object. The second operand may be a constant, variable or an expression.


To send more than one value at a time, << operator should be used for each constant/ variable/expression. This is called cascading of operator.

Example:


• cout << “Welcome”;

Pre-defined object cout sends the given string “Welcome” to screen.

 

• cout << “The sum = “ << sum;

First, cout sends the string “The Sum = “ to the screen and then sends the value of the variable sum; Usually, cout sends everything specified within double quotes or single quotes i.e., string or character constants, except non-graphical characters.

 

• cout <<“\n The Area: “ <<3.14*r*r;

First, cout sends everything specified within double quotes except \n to the screen, and then it evaluates the expression 3.14*r*r and sends the result to the screen.

\n – is a non graphical character constant to feed a new line.

 

• cout << a + b ;

cout sends the sum of a and b to the output console (monitor)

 

Cascading of I/O operators

 

The multiple use of input and output operators such as >> and << in a single statement is known as cascading of I/O operators.

 

Cascading cout:

int Num=20;

cout << “A=” << Num;

The Figure 9.6 is used to understand the working of Cascading cout statement



Cascading cin - Example:

cout >> “Enter two number: ”;

cin >> a >> b;

The Figure 9.7 is used to understanding the working of Cascading cin statement



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.