Thread: Overloaded stream insertion study

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Overloaded stream insertion study

    I'm trying to convert this simple phone number program into a demonstration of using the overloaded stream-insertion operator as a member function, i.e. I'm trying to use
    Code:
     phone << cout
    instead of the typical
    Code:
     cout << phone
    Can someone point me in the right direction?

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class PhoneNumber {
       ostream &operator<<( ostream& ); // defined as a member function
       friend istream &operator>>( istream&, PhoneNumber& );
    
    private:
       char areaCode[ 4 ]; // 3-digit area code and null
       char exchange[ 4 ]; // 3-digit exchange and null
       char line[ 5 ];  // 4-digit line and null
    
    ostream &operator<<( ostream &output ) // defined as a member function
    {
       output << "(" << num.areaCode << ")" << num. exchange << "-" << num.line;
    
       return output; // enables cout << a << b << c;
    } // end function operator<<
    
    
    
    }; // end class PhoneNumber
    
    istream &operator>>( istream &input, PhoneNumber &num )
    {
       input.ignore();
       input >> setw( 4 ) >> num.areaCode;
       input.ignore( 2 ); // skip ( and space
       input >> setw( 4 ) >> num.exchange;
       input.ignore();
       input >> setw( 5 ) >> num.line;  // input line
    
       return input;
    }
    
    int main()
    {
       PhoneNumber phone; // create object phone
    
       cout << "Enter phone number in the form (123) 456-7890:\n";
    
       cin >> phone; // operator>>(cin, phone)
    
       cout << "The phone number entered was: ";
    
      //cout << phone << endl; // operator<<( cout, phone );
    
        phone << cout << endl;
        phone.operator<<( cout ); // must be defined within the function operator<<
    
       return 0;
    } // end main
    THE redheaded stepchild.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Simply change the order of the parameters in the overloaded operator.
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    class myClass {
       friend ostream &operator<<(const myClass&, ostream&);
       int data;
       public:
         myClass(int d) : data(d) {}
    };
    
    ostream &operator<<(const myClass& obj, ostream& file) {
        return file << obj.data;
    }
    
    int main()
    {
       myClass obj1(5);
       
       obj1 << cout;
       cin.get();
       return 0;
    }
    Now keep in mind, there is no way to prevent the right shift operator from reading left to right, so chaining becomes impossible without a reference to the ostream, for instance
    Code:
    obj1 << obj1 << cout; // Doesn't work
    You also can't, as you seem to be trying to do, make it a member function. It's simply not allowed in the language to change the number of arguements.
    Last edited by SlyMaelstrom; 05-03-2006 at 12:13 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Overloaded >>
    By alphaoide in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2004, 12:27 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM