Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overload << and >>

Reply
Thread Tools

Overload << and >>

 
 
Piotre Ugrumov
Guest
Posts: n/a
 
      01-21-2004
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:perator<<
//do something
return out;
}

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

Thanks.


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-21-2004
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: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.

 
Reply With Quote
 
 
 
 
Kirti
Guest
Posts: n/a
 
      01-22-2004
friend ostream & operator<<(ostream &, const Persona &);
friend istream & operator>>(istream &, Persona &);

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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-22-2004
Sharad Kala wrote:

>
> "Kirti" <(E-Mail Removed)> wrote in message
>

news:(E-Mail Removed) lkaboutprogramming.com...
>> 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.

 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      01-22-2004

"Piotre Ugrumov" <(E-Mail Removed)> wrote in message
news:kVBPb.163699$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      01-22-2004

"Kirti" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) lkaboutprogramming.com...
> 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


 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      01-22-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message
news:buoaqf$cbr4h$(E-Mail Removed)-berlin.de...
>
> "Kirti" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) lkaboutprogramming.com...
> > 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.


 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      01-22-2004

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


I agree


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NAT overload with some static NAT's and a block public IP's Ronald de Leeuw Cisco 2 07-03-2005 05:45 PM
A question (and answer) for the Ruby FAQ: override and overload Stephan Kämper Ruby 2 01-24-2005 01:05 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Problem with JTable and event overload Akey Java 3 01-17-2004 04:12 PM



Advertisments