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

A Static Counter Inside operator<<

easyOperator Overloading

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <string>
3using namespace std;
4 
5class Ticket {
6 string m_event;
7public:
8 Ticket(const string& event) : m_event(event) {}
9 friend ostream& operator<<(ostream& os, const Ticket& t);
10};
11 
12ostream& operator<<(ostream& os, const Ticket& t) {
13 static int calls = 0;
14 calls++;
15 os << "Print " << calls << ": " << t.m_event;
16 return os;
17}
18 
19int main() {
20 Ticket a("Jazz"), b("Rock");
21 cout << a << endl;
22 cout << a << endl;
23 Ticket c = b;
24 cout << c << " | " << b << endl;
25 return 0;
26}

🎯 Your Answer