CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #78

Manipulators That Leak Out of operator<<

mediumStreams

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <iomanip>
3using namespace std;
4 
5class Price {
6 double m_value;
7public:
8 Price(double value) : m_value(value) {}
9 friend ostream& operator<<(ostream& os, const Price& p);
10};
11 
12ostream& operator<<(ostream& os, const Price& p) {
13 os << setfill('.') << setw(8) << fixed << setprecision(2) << p.m_value;
14 return os;
15}
16 
17int main() {
18 Price p(3.5);
19 cout << p << endl;
20 cout << setw(6) << 42 << endl;
21 cout << 2.0 / 3 << endl;
22 cout << 7 << endl;
23 return 0;
24}

🎯 Your Answer