Overloading the Stream Extraction (>>) Operator

The stream extraction operator function operator>> (Fig. 10.4, lines 21–30) takes the istream reference input and the PhoneNumber reference number as arguments and returns an istream reference. Operator function operator>> inputs phone numbers of the form

(800) 555-1212

into objects of class PhoneNumber. When the compiler sees the expression

cin >> phone

in line 16 of Fig. 10.5, the compiler generates the non-member function call

operator>>( cin, phone );

When this call executes, reference parameter input (Fig. 10.4, line 21) becomes an alias for cin and reference parameter number becomes an alias for phone. The operator function reads as strings the three parts of the telephone number into ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.