By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

object to standard output and input ( cout / cin )

P: n/a
suppose i have a class:
class X
{
int i;
string out;
}

X::out = "object x";
how can i do this:
code:
X obj;
cout<<obj;

output:
objext x
and

code:
X obj;
cin>>obj

is this possible???

thanks. =)

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> cout<<obj;
cin>>obj

is this possible???

sure...operator overloading. there are tons of c++ textbook examples
on this.

Jul 22 '05 #2

P: n/a
gouki wrote:
suppose i have a class:
class X
{
int i;
string out;
}
You forgot the semicolon. Also, the class is unusable because it has no
friends and no public members. Assume you have this instead:

// Header file "X.h"

#include <iosfwd>
#include <string>

class X
{
int i;
std::string out;

// These will come in handy later.
friend std::ostream & operator << (std::ostream &, const X &);
friend std::istream & operator >> (std::istream &, X &);

public:
X (int i, const std::string & out) : i (i), out (out) { }
};
X::out = "object x";
(Another syntax error. As a non-static member of X, out can only be
accessed for a particular X object, using the . or -> operator.)
how can i do this:
code:
X obj;
cout<<obj;

output:
objext x
and

code:
X obj;
cin>>obj

is this possible???


Yep. Overload the << and >> operators. Here's your first draft. This is
not well-tested code, and it artificially constrains X (if an X object
is to be successfully streamed out and then back in again, the "out"
member cannot contain a newline), but you get the idea.

// Source file X_io.cpp

#include "X.h"
#include <ostream>
#include <istream>
#include <sstream>

std::ostream & operator << (std::ostream & stream, const X & x)
{
return stream << x.i << '\n' << x.out << '\n';

// That was easy!
}

std::istream & operator >> (std::istream & stream, X & x)
{
// For extractors we usually have to work a bit harder.

int i;
std::string out;

// Read 'i' and 'out' as single lines from 'stream'.

{
std::string t;
std::getline (stream, t);
std::istringstream sstream (t);
sstream >> i;
}
std::getline (stream, out); // Read "out".

// Commit results if no stream error.

if (stream)
{
x.i = i;
x.out.swap (out); // We can't risk a bad_alloc at this stage.
}
return stream;
}

// Source file "test.cpp"

#include "X.h"
#include <string>
#include <sstream>
#include <iostream>
#include <cstddef>

X x_from_string (const std::string & s)
{
X result (0, "");
std::istringstream sstream (s);
sstream >> result;
return result;
}

std::string x_to_string (const X & x)
{
std::ostringstream sstream;
sstream << x;
return sstream.str ();
}

int main (int argc, char * argv [])
{
std::string persistent;

{
X old (163, "262 537 412 640 768 744");
persistent = x_to_string (old);
}

X lazarus (x_from_string (persistent));

std::cout << lazarus;
return EXIT_SUCCESS;
}

--
Regards,
Buster.
Jul 22 '05 #3

P: n/a
On 5 Aug 2004 18:51:08 -0700 in comp.lang.c++, "gouki"
<fi*****@gmail.com> wrote,
how can i do this:
code:
X obj;
cout<<obj;


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[15.8] How can I provide printing for my class Fred? " It is always
good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.