Stream Operator Overloading

#include <iostream>
#include <string>

class SystemStatus {
private:
    int code;
    std::string message;
    friend std::ostream& operator<<(std::ostream& , const SystemStatus& );
public:
    SystemStatus(int c, std::string m) : code(c), message(m) {}
};

std::ostream& operator<<(std::ostream& os, const SystemStatus& s){
    std::cout << "[Status: " << s.code << "] " << s.message;
    return os;
}


int main() {
    int N;
    if (!(std::cin >> N)) return 0;

    for (int i = 0; i < N; ++i) {
        int code;
        std::string msg;
        std::cin >> code >> msg;

        SystemStatus status(code, msg);
        
        
        std::cout << status << std::endl;
    }
    return 0;
}

Solving Approach

 

 

 

 

Upvote
Downvote
Loading...

Input

2 404 NotFound 200 OK

Expected Output

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