473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overload << and >>

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::operator<<
//do something
return out;
}

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

Thanks.
Jul 22 '05 #1
7 4304
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::operator<<
//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.

Jul 22 '05 #2
friend ostream & operator<<(ostream &, const Persona &);
friend istream & operator>>(istream &, Persona &);

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

Jul 22 '05 #3

"Piotre Ugrumov" <au************@tin.it> wrote in message
news:kV*********************@news4.tin.it...
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
Jul 22 '05 #4

"Kirti" <mk****@nospam.yahoo.com> wrote in message
news:c5******************************@localhost.ta 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
Jul 22 '05 #5

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:bu************@ID-221354.news.uni-berlin.de...

"Kirti" <mk****@nospam.yahoo.com> wrote in message
news:c5******************************@localhost.ta 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.
Jul 22 '05 #6
Sharad Kala wrote:

"Kirti" <mk****@nospam.yahoo.com> wrote in message

news:c5******************************@localhost.ta 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.

Jul 22 '05 #7

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


I agree :-)
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
5
by: Suman | last post by:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++ archives and Stroustrup's FAQs (particularly the following: <url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>) I am...
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.