OOPortal OOPortal

Operator Overloading  «Prev

Output class ostream

Overloading input-output Operators

Output is returned to an object of type ostream as described in iostream.h. The operator << is overloaded in this class to perform output conversions from standard types. The operator is left associative and returns a value of type ostream&. The standard output ostream corresponding to stdout is cout.
The class ostream contains public members such as:

ostream& operator<<(int i);
ostream& operator<<(long i);
ostream& operator<<(double x);
ostream& operator<<(char c);
ostream& operator<<(const char* s);
ostream& put(char c);
ostream& write(const char* p, int n);
ostream& flush();

The member function put outputs the character representation of c. The member function write outputs the string of length n pointed at by p. The member function flush forces the stream to be written, and since these are member functions, they can be used as follows:

int c = 'A';
cout.put(c);     //output A
cout.put(66);    //output B ASCII value 66
cout.put(c + 2); //output C
char* str = 'ABCDEFGHI';
cout.write(str + 2, 3);  //output CDE
cout.flush();  //write contents of buffered stream