HackerRank C++ Overloading Ostream Operator




The task is to overload the << operator for Person class in such a way that for p being an instance of class Person the result of:

std::cout << p << " " << << std::endl;



  1. #include <iostream>
  2. using namespace std;
  3. class Person {
  4. public:
  5. Person(const string& first_name, const string& last_name) : first_name_(first_name), last_name_(last_name) {}
  6. const string& get_first_name() const {
  7. return first_name_;
  8. }
  9. const string& get_last_name() const {
  10. return last_name_;
  11. }
  12. private:
  13. string first_name_;
  14. string last_name_;
  15. };
  16. ostream& operator<<(ostream& os, const Person& pr)
  17. {
  18. os <<"first_name=" <> first_name >> last_name >> event;
  19. auto p = Person(first_name, last_name);
  20. cout << p << " " << event << endl;
  21. return 0;
  22. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube