Overload << and >>

P

Piotre Ugrumov

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.
 
R

Rolf Magnus

Piotre said:
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.
 
K

Kirti

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

why do we need to make these 2 functions as friend ?
 
R

Rolf Magnus

Sharad said:
news:c514d833e82f4a9fcc8a9ccad370de3f@localhost.talkaboutprogramming.com...

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.
 
S

Sharad Kala

Piotre Ugrumov said:
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
 
S

Sharad Kala

Kirti said:
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
 
S

Sharad Kala

Sharad Kala said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top