Cpp - cout Output Streams

Introduction

The streams cin and cout are instances of the istream or ostream classes.

When a program is launched these objects are automatically created to read standard input or write to standard output.

Standard input is normally the keyboard and standard output the screen.

The standard input and output can be redirected to files: data is not read from the keyboard but from a file, or data is not displayed on screen but written to a file.

The other two standard streams cerr and clog are used to display messages when errors occur.

Error messages are displayed on screen even if standard output has been redirected to a file.

Manipulator

Here the manipulator showpos is called.

cout << showpos << 123;   // Output:  +123 

The above statement is equivalent to

cout.setf( ios::showpos); 
cout << 123; 

The other positive numbers are printed with their sign as well:

cout << 22;               // Output:  +22 

The output of a positive sign can be canceled by the manipulator noshowpos:

cout << noshowpos << 123;  // Output:  123 

The last statement is equivalent to

cout.unsetf( ios::showpos); 
cout << 123; 

The operators >> and << format the input and/or output according to how the flags in the base class ios are set.

The manipulator showpos is a function that calls the method cout.setf(ios::showpos);