11.9 OUR OWN MANIPULATORS

We can write our own manipulators. As they are written for specific situations, they are also called as custom manipulators.

Manipulators are designed as functions. It must return a reference to ostream. As we cannot return a local variable as reference, we have to pass reference to ostream as a parameter. It helps it to take part in a chained output.

Consider the problem of printing currency. We need the precision of two. In addition, we want trailing zeros, if any. To define a manipulator, a particular format is needed. It is shown below.

Problem: Write a program to define a manipulator “standard”. It should help in setting suitable flags for printing floating point number as amount.

Solution: Amounts are written only ...

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.