11.7 THE MANIPULATORS

Manipulators are the alternate way of formatting data by setting ios flags. Earlier, we have seen the statements like

cout.setf(ios::hex) ;

cout << marks ;

Now these two actions can be combined in one expression. We can write

cout << hex << marks ;

In this case, hex is a manipulator defined in the header file “iostream.h”. Whatever formatting we can do by setting /unsetting flags can be done with the help of manipulators.

You may ask why an alternative is required for something that is so simple and straightforward. There are two reasons.

  1. The syntax of manipulators allows us to insert manipulators in the chained read/write statements. This is clearly a shorthand notation. One of the reasons for the popularity of C/C++ is ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.