Solved

cout/cin overload

Posted on 2001-08-08
8
1,986 Views
Last Modified: 2010-08-05
Is it possible to overload cout & cin ?
if so, how? if not, why ?
0
Comment
Question by:ls1429
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Expert Comment

by:IainHere
ID: 6363392
Yes, they're overloadable - that's pretty much the reason they were developed.  Don't forget that cout is defined as

extern ostream cout;

say you have your own class:

class WriteMe{
public:
    double m_writethis;
};

you would overload the << operator to work with cout like this:

ostream& operator<< (ostream&s, const WriteMe& write)
{
   return s << "your formatting here " << write.m_writethis;
}

[Ignoring the publicness of the member variable and the uselessness of the class]
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6363469
Oh, and the cin being istream can be overloaded in the same fashion (of course, the class you're overloading for cannot be const in this case, and you must remove any formatting you define in the ostream - I've ignored this removal because it would complicate matters, and I'd probably get it wrong anyway :)

istream& operator>> (istream& s, WriteMe& write)
{
   s >> write.m_writethis;
   return s;
}

Of course in a real case, you'd have to put all sorts of error checking in there too.

you would use this exactly as you would a built in type:

WriteMe   bob;
bob.m_writethis = 77;
cout << bob;

...

WriteMe   bill;
cin >> bill;  //ignoring the formatting again.
0
 

Author Comment

by:ls1429
ID: 6363611
The above code ( tested for cout )did not got compiled.
But when I changed the overload function to friend function it got magically compiled.
I dont know why I got problems and by friend function how it solved/ redirected the purpose.

Can you explain ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:glebspy
ID: 6363627
I just wanted to make a technical point.

cin and cout are objects. It's not possible to overload them, and the phrase doesn't even mean anything. It's only possible to overload functions (of which operators like << are a special case).

when you employ Iainhere's solution (he correctly guessed what you meant), you are using a different(overloaded) operator<<, but the same cin and cout.

It's possible to over*ride* cin and cout by saying something silly like

float cin;

or

string cout;

after which operator<< doesn't work any more..
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6363930
>>But when I changed the overload function to friend function it got magically compiled.

Could you post the code - I don't understand.  In the meantime, here's the code for a functioning example (I just tested it).

#include <iostream>
using namespace std;

class WriteMe{
public:
   double m_writethis;
};

ostream& operator<< (ostream&s, const WriteMe& write)
{
  return s << "your formatting here " << write.m_writethis;
}

int main(int, char**)

{
     WriteMe jeff;
     jeff.m_writethis = 33;
     cout << jeff;    
     return 0;
}
0
 
LVL 1

Accepted Solution

by:
glebspy earned 25 total points
ID: 6364075
>The above code ( tested for cout )did not got compiled.
>But when I changed the overload function to friend function it got magically compiled.
>I dont know why I got problems and by friend function how it solved/ redirected the purpose.

>Can you explain ?


I've  encountered exactly this situation many times, and the right thing to do is to make the overloaded operator a friend function.

The reason is that the overloaded >> and << are not members of Iainhere's class Writeme! They are global. You can't make them member functions of Writeme. Imagine you had

class Writeme{
 double m_writethis;
 ostream&operator<<(ostream&a){return a<<m_writethis}
}bill;
that would work but you'd have to say
bill<<cout;
rather than cout<<bill; which is what I'd consider desirable.

So if you try to access

istream& operator>> (istream& s, WriteMe& write)
           {
             s >> write.m_writethis;
             return s;
           }

*operator>> has to have access to the member m_writethis*
so either m_writethis has to be declared public: (remember
members of a class are private by default) or the global operator>> has to be declared a friend. I personally use the friend way of doing things since I might want to write out all sorts of members of the class in the cout<< call, some of which I might think were more private than others.

I usually call writing functions which only have access to public members `display' or `show' rather than using operator<<, and then these are not friends and so they can only write out the public members.
0
 
LVL 11

Expert Comment

by:griessh
ID: 6851036
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "glebspy, IanHere" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6874840
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange

IainHere: points for you at http://experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20278011
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question