In the program of Figs. 11.3–11.5, Fig. 11.4 contains the comment "overloaded stream insertion operator; cannot be a member function if we would like to invoke it with cout << somePhoneNumber;." Actually, the stream insertion operator could be a PhoneNumber class member function if we were willing to invoke it either as somePhoneNumber.operator<<( cout ); or as somePhoneNumber << cout;. Rewrite the program of Fig. 11.5 with the overloaded stream insertion operator<< as a member function and try the two preceding statements in the program to demonstrate that they work.
Here is what I have so far: we have to create phonnumber class
Header File:
#ifndef PHONENUMBER_H#define PHONENUMBER_H #include <iostream>using std::ostream;using std::istream; #include <string>using std::string; class PhoneNumber{friend ostream &operator<<( ostream &, const PhoneNumber & );friend istream &operator>>( istream &, PhoneNumber & ); private:string areaCode; // 3-digit area codestring exchange; // 3-digit exchangestring line; // 4-digit line}; // end class PhoneNumber #endif
Phonenumber .cpp file
// for class PhoneNumber.#include <iomanip>using std::setw; #include "PhoneNumber.h" // overloaded stream insertion operator; cannot be // a member function if we would like to invoke it with // cout << somePhoneNumber; ostream &operator<<( ostream &output, const PhoneNumber &number ){ output << "(" << number.areaCode << ") " << number.exchange << "-" << number.line; return output; // enables cout << a << b << c; } // end function operator<< // overloaded stream extraction operator; cannot be // a member function if we would like to invoke it with // cin >> somePhoneNumber; istream &operator>>( istream &input, PhoneNumber &number ) { input.ignore(); // skip ( input >> setw( 3 ) >> number.areaCode; // input area code input.ignore( 2 ); // skip ) and space input >> setw( 3 ) >> number.exchange; // input exchange input.ignore(); // skip dash (-) input >> setw( 4 ) >> number.line; // input line return input; // enables cin >> a >> b >> c;
}
MAIN .CPP file
#include <iostream>using std::cout;using std::cin;using std::endl; #include "PhoneNumber.h" int main(){PhoneNumber phone; // create object phonecout << "Enter phone number in the form (123) 456-7890:" << endl; // cin >> phone invokes operator>> by implicitly issuing// the global function call operator>>( cin, phone ) cin >> phone; cout << "The phone number entered was: "; // cout << phone invokes operator<< by implicitly issuing// the global function call operator<<( cout, phone ) cout << phone << endl; system (“pause”);return 0;} // end main
Please Help me any type of help I would appreciate it!!!!