Overloading Ostream Operator

Sort by

recency

|

67 Discussions

|

  • + 1 comment
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    class Person
    {
        private: 
        string name, surname;
        
        public:
        Person(string n, string s)
        {
            this->name = n;
            this->surname = s;
        }
        friend ostream& operator<<(ostream& out,Person const& p1);
        friend istream& operator>>(istream& ins, Person p1);
        
    };
    istream& operator >> (istream& ins, Person p1)
    {
       ins >> p1.name;
       ins >> p1.surname; 
       return ins;
    };
    ostream & operator << (ostream &out, const Person& p1)
    {
        out << "first_name=" << p1.name << ","<<"last_name=" << p1.surname;
        return out;
    };
    
    
    int main() {
        
        string name,surname,message;
        
        
        cin >> name >> surname >> message;
        
        Person p(name,surname);
        
        
        cout << p << " " << message << endl;
        
        
           
        return 0;
    }
    
  • + 1 comment

    Is this a 'medium difficulty' problem?

  • + 0 comments
    ostream& operator<<(ostream& os, const Person& p) {
        os << "first_name=" << p.get_first_name() << ",last_name=" << p.get_last_name();
        return os;
    }
    
  • + 0 comments

    Shouldn't the overloaded << operator function be declared as a friend of class Person, so that it can access the private data within a Person object?

  • + 0 comments
    std::ostream &operator<<(std::ostream &out, const Person &p){
        out << "first_name=" + p.get_first_name() << "," << "last_name=" + p.get_last_name();
        
        return out;
    }