object to standard output and input ( cout / cin )

Discussion in 'C++' started by gouki, Aug 6, 2004.

  1. gouki

    gouki Guest

    suppose i have a class:


    class X
    {
    int i;
    string out;
    }

    X::eek:ut = "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. =)
     
    gouki, Aug 6, 2004
    #1
    1. Advertising

  2. gouki

    Wiseguy Guest

    > cout<<obj;

    > cin>>obj
    >
    > is this possible???



    sure...operator overloading. there are tons of c++ textbook examples
    on this.
     
    Wiseguy, Aug 6, 2004
    #2
    1. Advertising

  3. gouki

    Buster Guest

    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::eek:stream & operator << (std::eek:stream &, const X &);
    friend std::istream & operator >> (std::istream &, X &);

    public:
    X (int i, const std::string & out) : i (i), out (out) { }
    };

    > X::eek:ut = "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::eek:stream & operator << (std::eek:stream & 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::eek:stringstream 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.
     
    Buster, Aug 6, 2004
    #3
  4. gouki

    David Harmon Guest

    On 5 Aug 2004 18:51:08 -0700 in comp.lang.c++, "gouki"
    <> 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/
     
    David Harmon, Aug 6, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,114
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,203
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,746
    Alex Vinokur
    Mar 6, 2005
  4. Xoomer
    Replies:
    1
    Views:
    306
    Ian Collins
    Aug 23, 2007
  5. Fernando
    Replies:
    4
    Views:
    1,746
    Fernando
    Nov 16, 2011
Loading...

Share This Page