146. Stream Operator Overloading

In C++, we often want to print objects directly to the console or a log stream using std::cout << obj, rather than calling a specific method like obj.print(). To achieve this, we must overload the << operator. Since the << operator is a global function (the left side is std::ostream, not our class), it cannot be a member function. Therefore, it must be declared as a Friend to access the private data it needs to print.

Your task is to implement a class SystemStatus.

  1. Private members: int code, std::string message.
  2. Constructor to initialize them.
  3. Friend Function: Overload operator<< to print the object in the format: [Status: <code>] <message>.

Program Flow:

  1. Read integer N.
  2. Loop N times.
  3. Read integer code and string msg.
  4. Instantiate a SystemStatus object.
  5. Print it using std::cout << obj << std::endl;.

Input Format:

  • First line: Integer N.
  • Next N lines: Integer code, followed by string msg.
  • Input is provided via standard input (stdin).

Output Format:

  • [Status: <code>] <message>
  • Each output must be on a new line.

Example: 

Example 1

Input:

2
404 NotFound
200 OK

Output:

[Status: 404] NotFound
[Status: 200] OK

Constraints:

  • Must use friend std::ostream& operator<< ....
  • Must return the stream object (os) to allow chaining.
  • code and message must be private.

 

 

 

 

Loading...

Input

2 404 NotFound 200 OK

Expected Output

[Status: 404] NotFound [Status: 200] OK