Stream Operator Overloading

#include <iostream>
#include <string>

class SystemStatus {
private:
    int code;
    std::string message;

    // TODO: Declare the stream operator as a friend
    friend std::ostream& operator << (std::ostream& os, const SystemStatus& s);

public:
    SystemStatus(int c, std::string m) : code(c), message(m) {}
};

// TODO: Implement the overloaded operator<<
// It must take 'std::ostream& os' and 'const SystemStatus& s'
// It must print "[Status: " << s.code << "] " << s.message
// It must return 'os'
std::ostream& operator<<(std::ostream& os, const SystemStatus& s) {
    // Placeholder logic
    os << "[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);
        
        // This line works because of the operator overloading
        std::cout << status << std::endl;
    }
    return 0;
}
Upvote
Downvote
Loading...

Input

2 404 NotFound 200 OK

Expected Output

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