186. Stream Operator Overloading

Back To All Submissions
Previous Submission
Next Submission
#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

 

 

 

 

Was this helpful?
Upvote
Downvote