Overload << and >>

Discussion in 'C++' started by Piotre Ugrumov, Jan 21, 2004.

  1. I have tried to implement the overload of these 2 operators.
    ostream & operator<<(ostream &out, Person &p){
    out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl;
    return out;
    }
    This overload work but I have a curiosty
    If I try to approch to the name or to the surname or to the dateofbirth in
    this way i receive error:
    ostream & operator<<(ostream &out, Person &p){
    out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
    return out;
    }

    I don't know how implement the overload of >>, I have tried in this way but
    this system don't work:

    istream & operator>>(istream &in, Person &p){
    char tmp1[30];
    char tmp2[30];
    char tmp3[30];
    in>>tmp1;
    in>>tmp2;
    in>>tmp3;
    Person::setName(tmp1);
    Persona::setSurname(tmp2);
    Persona::setDateOfBirth(tmp3);
    return in;
    }

    How can I implement the overload o the operator >>?


    I have defined these overload in this class:
    #pragma once
    #include <iostream>
    using namespace std;

    class Person
    {
    friend ostream & operator<<(ostream &, const Persona &);
    friend istream & operator>>(istream &, Persona &);
    public:
    Persona(char *n, char *c, char *dn);
    void setName(char *n);
    void setSurname(char *c);
    void setDataOBirth(char *dn);
    char* getName();
    char* getSurname();
    char* getDateOfBirth();
    ~Persona(void);
    protected:
    char *name, *surname, *dateofbirth;
    };

    If in a class son of Person I define the overload of << and >>, can I do
    this thing?
    ostream & operator<<(ostream &out, Person &p){
    Person::eek:perator<<
    //do something
    return out;
    }

    istream & operator>>(istream &in, Person &p){
    Person::eek:perator>>
    //do something
    return in;
    }

    Thanks.
    Piotre Ugrumov, Jan 21, 2004
    #1
    1. Advertising

  2. Piotre Ugrumov

    Rolf Magnus Guest

    Piotre Ugrumov wrote:

    > I have tried to implement the overload of these 2 operators.
    > ostream & operator<<(ostream &out, Person &p){
    > out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl;
    > return out;
    > }
    > This overload work but I have a curiosty
    > If I try to approch to the name or to the surname or to the
    > dateofbirth in this way i receive error:


    What error?

    > ostream & operator<<(ostream &out, Person &p){
    > out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
    > return out;
    > }


    Why would you want that instead of the first version? This version
    accesses protected members directly and therefore needs to be a friend,
    the first doesn't. Anyway, maybe this version doesn't work, because
    it's not a friend of your class (the friend operator<<'s signature is
    different).

    > I don't know how implement the overload of >>, I have tried in this
    > way but this system don't work:


    What do you mean by "don't work"?

    > istream & operator>>(istream &in, Person &p){
    > char tmp1[30];
    > char tmp2[30];
    > char tmp3[30];


    You should really use strings instead of arrays of char.

    > in>>tmp1;
    > in>>tmp2;
    > in>>tmp3;
    > Person::setName(tmp1);
    > Persona::setSurname(tmp2);
    > Persona::setDateOfBirth(tmp3);


    You have to call those non-static member functions the same way as in
    the operator>>, for an object, i.e.:

    p.setName(tmp1);
    ....

    > return in;
    > }
    >
    > How can I implement the overload o the operator >>?
    >
    >
    > I have defined these overload in this class:
    > #pragma once
    > #include <iostream>
    > using namespace std;
    >
    > class Person
    > {
    > friend ostream & operator<<(ostream &, const Persona &);
    > friend istream & operator>>(istream &, Persona &);
    > public:
    > Persona(char *n, char *c, char *dn);
    > void setName(char *n);
    > void setSurname(char *c);
    > void setDataOBirth(char *dn);
    > char* getName();


    Make that:

    const char* getName() const;

    Or even better:

    std::string getName() const;

    Same for the next two members.

    > char* getSurname();
    > char* getDateOfBirth();
    > ~Persona(void);
    > protected:
    > char *name, *surname, *dateofbirth;


    Why are those member variables protected and not private?

    > };
    >
    > If in a class son of Person


    You mean a class that is derived from it?

    > I define the overload of << and >>, can I
    > do this thing?
    > ostream & operator<<(ostream &out, Person &p){

    ^^^^^^
    Don't you mean that to be another class?

    > Person::eek:perator<<
    > //do something
    > return out;
    > }


    You can do something like this:

    ostream& operator<<(ostream& out, const Manager& p)
    {
    out << static_cast<Person&>(p);
    //do the rest
    return out;
    }

    But note that this won't work correctly with polymorphism, since
    non-member-functions cannot work polymorphic. If you need polymorphism,
    you need to add a member function to your class, like:

    ostream& Person::write(ostream& out) const
    {
    return out << name << " " << surname << ", " << dateofbirth;
    }

    declare it virtual in the Person class and override it in your derived
    classes. Then write your operator as:

    ostream& operator<<(ostream& out, const Person& p)
    {
    return p.write(out);
    }

    Then you only need that single one operator<<, and the virtual member
    functions will take care of calling the right write() function.
    Rolf Magnus, Jan 21, 2004
    #2
    1. Advertising

  3. Piotre Ugrumov

    Kirti Guest

    friend ostream & operator<<(ostream &, const Persona &);
    friend istream & operator>>(istream &, Persona &);

    why do we need to make these 2 functions as friend ?
    Kirti, Jan 22, 2004
    #3
  4. Piotre Ugrumov

    Rolf Magnus Guest

    Sharad Kala wrote:

    >
    > "Kirti" <> wrote in message
    >

    news:...
    >> friend ostream & operator<<(ostream &, const Persona &);
    >> friend istream & operator>>(istream &, Persona &);
    >>
    >> why do we need to make these 2 functions as friend ?

    >
    > If non-member functions need to access the private members of a class
    > they need to be declared private..right :)?
    > That's what OP was trying to attempt in his code.


    Yes, but without a reason. He can implement the operators by using only
    the public interface, and so he should.
    Rolf Magnus, Jan 22, 2004
    #4
  5. Piotre Ugrumov

    Sharad Kala Guest

    "Piotre Ugrumov" <> wrote in message
    news:kVBPb.163699$...
    > I have tried to implement the overload of these 2 operators.
    > ostream & operator<<(ostream &out, Person &p){
    > out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl;
    > return out;
    > }
    > This overload work but I have a curiosty
    > If I try to approch to the name or to the surname or to the dateofbirth in
    > this way i receive error:
    > ostream & operator<<(ostream &out, Person &p){
    > out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
    > return out;
    > }
    >

    Change your friend declaration in class definition to -
    friend ostream & operator<<(ostream &, const Person &);
    ^^^^^

    Best wishes,
    Sharad
    Sharad Kala, Jan 22, 2004
    #5
  6. Piotre Ugrumov

    Sharad Kala Guest

    "Kirti" <> wrote in message
    news:...
    > friend ostream & operator<<(ostream &, const Persona &);
    > friend istream & operator>>(istream &, Persona &);
    >
    > why do we need to make these 2 functions as friend ?


    If non-member functions need to access the private members of a class they need
    to be declared private..right :)?
    That's what OP was trying to attempt in his code.

    Best wishes,
    Sharad
    Sharad Kala, Jan 22, 2004
    #6
  7. Piotre Ugrumov

    Sharad Kala Guest

    "Sharad Kala" <> wrote in message
    news:buoaqf$cbr4h$-berlin.de...
    >
    > "Kirti" <> wrote in message
    > news:...
    > > friend ostream & operator<<(ostream &, const Persona &);
    > > friend istream & operator>>(istream &, Persona &);
    > >
    > > why do we need to make these 2 functions as friend ?

    >
    > If non-member functions need to access the private members of a class they

    need
    > to be declared private..right :)?

    ^^^^^
    oops..I meant friend here.
    Sharad Kala, Jan 22, 2004
    #7
  8. Piotre Ugrumov

    Sharad Kala Guest


    >
    > Yes, but without a reason. He can implement the operators by using only
    > the public interface, and so he should.


    I agree :)
    Sharad Kala, Jan 22, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Akey
    Replies:
    3
    Views:
    538
    Tor Iver Wilhelmsen
    Jan 17, 2004
  2. Chris E. Yoon
    Replies:
    2
    Views:
    378
    Michiel Salters
    Jul 23, 2003
  3. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  4. Stephan Kämper
    Replies:
    2
    Views:
    180
    Stephan Kämper
    Jan 24, 2005
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page