10.5. Overloading the Binary Stream Insertion and Stream Extraction Operators

You can input and output fundamental-type data using the stream extraction operator >> and the stream insertion operator <<. The C++ class libraries overload these binary operators for each fundamental type, including pointers and char * strings. You can also overload these operators to perform input and output for your own types. The program of Figs. 10.310.5 overloads these operators to input and output PhoneNumber objects in the format “(000) 000-0000.” The program assumes telephone numbers are input correctly.


 1   // Fig. 10.3: PhoneNumber.h
 2   // PhoneNumber class definition
 3   #ifndef PHONENUMBER_H
 4   #define PHONENUMBER_H
 5
 6   #include <iostream>
 7   #include <string>
 8
 9   class PhoneNumber
10   {
11      friend std::ostream &operator<<( std::ostream &, const PhoneNumber & );
12      friend std::istream &operator>>( std::istream &, PhoneNumber & );      
13   private:
14      std::string areaCode; // 3-digit area code
15      std::string exchange; // 3-digit exchange
16      std::string line; // 4-digit line
17   }; // end class PhoneNumber
18
19   #endif


Fig. 10.3. PhoneNumber class with overloaded stream insertion and stream extraction operators as friend functions.


 1   // Fig. 10.4: PhoneNumber.cpp
 2   // Overloaded stream insertion and stream extraction operators
 3   // for class PhoneNumber.
 4   #include <iomanip>
 5   #include "PhoneNumber.h"
 6   using namespace std;
 7
 8   // overloaded stream insertion operator; cannot be               
 9   // a member function if we would like to invoke it with          
10   // cout << somePhoneNumber;                                      
11   ostream &operator<<( ostream &output, const PhoneNumber &number )
12   {                                                                
13      output << "(" << number.areaCode << ") "                      
14         << number.exchange << "-" << number.line;                  
15      return output; // enables cout << a << b << c;                
16   } // end function operator<<                                     
17
18   // overloaded stream extraction operator; cannot be         
19   // a member function if we would like to invoke it with     
20   // cin >> somePhoneNumber;                                  
21   istream &operator>>( istream &input, PhoneNumber &number )  
22   {                                                           
23      input.ignore(); // skip (                                
24      input >> setw( 3 ) >> number.areaCode; // input area code
25      input.ignore( 2 ); // skip ) and space                   
26      input >> setw( 3 ) >> number.exchange; // input exchange 
27      input.ignore(); // skip dash (-)                         
28      input >> setw( 4 ) >> number.line; // input line         
29      return input; // enables cin >> a >> b >> c;             
30   } // end function operator>>                                


Fig. 10.4. Overloaded stream insertion and stream extraction operators for class PhoneNumber.


 1   // Fig. 10.5: fig10_05.cpp
 2   // Demonstrating class PhoneNumber's overloaded stream insertion
 3   // and stream extraction operators.
 4   #include <iostream>
 5   #include "PhoneNumber.h"
 6   using namespace std;
 7
 8   int main()
 9   {
10      PhoneNumber phone; // create object phone
11
12      cout << "Enter phone number in the form (123) 456-7890:" << endl;
13
14      // cin >> phone invokes operator>> by implicitly issuing
15      // the non-member function call operator>>( cin, phone )
16      cin >> phone;                                           
17
18      cout << "The phone number entered was: ";
19
20      // cout << phone invokes operator<< by implicitly issuing
21      // the non-member function call operator<<( cout, phone )
22      cout << phone << endl;                                   
23   } // end main


Enter phone number in the form (123) 456-7890:
(800) 555-1212
The phone number entered was: (800) 555-1212


Fig. 10.5. Overloaded stream insertion and stream extraction operators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.2.78