Saturday 9 October 2010

C++ I/O OPERATORS AND GIVE THEIR MEANINGS

3) C++ I/O OPERATORS AND GIVE THEIR MEANINGS:
ANS:
cin       :standard input
cout     :standard output
cerr     : standard error output
clog     : Buffered output of standard error.



Console Input: (cin)

Let us consider an example.

#include<iostream.h>
#include<conio.h>
void main()
{
cout<<”enter a number: “;
cin>>x;                 //console input in C++
cout<<”you entered: “<<x;
}


Output:
Enter a number: 10
You entered: 10

1.      ‘cin’ (pronounce see-in) is actually an object of the class ‘istream_withassign’. It stands as an alias for the console input device, that is, the keyboard (hence the name).
2.      The >>symbol, originally the right-shift operator, has had its definition extended in C++. In the given context, it operates as the ‘extraction’ operator. It is a binary operator and takes two operands. The operand on its left must be some object of the ‘istream_withassign’ class. The operand on its right must be a variable of some fundamental data type. The value for the variable on the right side of the ‘extraction’ operator is extracted from the stream originating device associated with the object on the left. Consequently, the value of the ‘x’ is obtained from the keyboard.
3.      The file ‘iostream.h’ needs to be included in the source code to ensure successful compilation because the object ‘cin’and the ‘extraction’ operator have been declared in that file.
4.      Again, just like the ‘insertion’ operator, the ‘extraction’ operator works equally well with variables of all fundamental types as its right-hand operand. It doesn’t need the format specifies that are needed in the scanf() family of functions. The following listing exemplifies this.

#include<iostream.h>
#include<conio.h>
void main()
{
int ivar;
float fvar;
char cvar;
cout<<”enter a whole number:”;
cin>>ivar;
cout<<enter a character: “;
cin>>cvar;
cout<<”enter a real number: “;
cin>>fvar;
cout<<”you entered: “<<ivar<<” “<<cvar<<” “<<fvar;
}

Output:
Enter a whole number: 10
Enter a character: x
Enter a real number: 2.3
You entered: 10 x 2.3

5.      Just like insertion operator, it is possible to cascade the ‘extraction’ operator. For example;

#include<iostream.h>
void main()
{
int x;
float y;
cout<<”enter the values for x and y: “;
cin>>x>>endl>>y;   //cascading the extraction operator
}

Output:
80
90

Console output: (cout)

Let us consider an example.

#include<iostream.h>
void mai()
{
int x;
x=10;
cout<<x;
}

Output: 
10

1.      ‘cout’ (pronounce see-out) is actually an object of the class ‘ostream_withassign’. It stands as an alias for the console output device that is, the monitor (hence the name).
2.      The <<symbol, originally the left shift operator, has had its definition extended in C++. In the given context it acts as the ‘insertion’ operator. It is binary operator. It takes two operands. The operand on its left must be an object of ‘ostream’ class. The operand on its right must be a value of some fundamental data type. The value on the right side of the ‘insertion operator’ is ‘inserted’ into the stream headed towards the device associated with the object on the left. Consequently, the value of ‘x’ is displayed on the monitor.
3.      The file ‘iostream.h’ needs to be included in the source code to ensure successful compilation because the object ‘cout’ and the ‘insertion’ operator have been declared in that file.
4.      Another object ‘endl’ allow us to insert a new line into the output stream. The following example illustrates this.

#include<iostream.h>
void main()
{
int x,y;
x=10;
y=20;
cout<<x;
cout<<endl;    //inserting anew line
cout<<y;
}

Output:
10
20

5.      Just like arithmetic addition, it is possible to cascade the ‘insertion’ operator. The following example is a case in point.

#include<iostream.h>
void main()
{
int x;
float y;
x=10;
y=2.2;
cout<<x<<endl<<y;        //cascading the insertion operator
}

Output:
10
2.2

6.      It is needless to say that we can pass constants instead of variables as operands to the ‘insertion’ operator.
Ex:

#include<iostream.h>
void main()
{
cout<<10<<endl<<”hello world\n”<3.4;
}
Output:
10
Hello world
3.4



 

No comments:

Post a Comment