Header Ad

HackerRank Overloading Ostream Operator solution in c++ programming

In this HackerRank Overloading Ostream Operator problem in c++ programming language, you need to overload the << operator for Person class in such a way that for p being as an instance of class Person produce the output.


HackerRank Overloading Ostream Operator solution in c++ programming


HackerRank Overloading Ostream Operator problem solution in c++ programming.

#include <iostream>

using namespace std;

class Person {
public:
    Person(const string& first_name, const string& last_name) : first_name_(first_name), last_name_(last_name) {}
    const string& get_first_name() const {
      return first_name_;
    }
    const string& get_last_name() const {
      return last_name_;
    }
private:
    string first_name_;
    string last_name_;
};
// Enter your code here.
ostream& operator<<(ostream& os, const Person& pr)  
{  
    os <<"first_name=" <<pr.get_first_name() << ",last_name=" << pr.get_last_name();  
    return os;  
} 


int main() {
    string first_name, last_name, event;
    cin >> first_name >> last_name >> event;
    auto p = Person(first_name, last_name);
    cout << p << " " << event << endl;
    return 0;
}


Post a Comment

0 Comments