#include #include using std::cout; using std::cin; using std::endl; using std::ostream; using std::istream; using std::setw; class PhoneNumber { friend ostream &operator<<(ostream&, const PhoneNumber&); friend istream &operator>>(istream&, PhoneNumber&); public: private: char areaCode[4]; char exchange[4]; char line[5]; }; ostream &operator<<(ostream& output, const PhoneNumber& num) { output << " (" << num.areaCode << ") " << num.exchange << "-" << num.line; return output; } istream &operator>>(istream& input, PhoneNumber& num) { input.ignore(2); // skip SPACE + ( input >> setw(4) >> num.areaCode; // read area code + NULL input.ignore(2); // skip ( + space input >> setw(4) >> num.exchange; // read exchange + NULL input.ignore(); // skip - input >> setw(5) >> num.line; // read line + NULL return input; // allows cascading >> } int main(int argc, char* argv[]) { PhoneNumber phone; cout << "Enter phone number '(123) 456-7890' format : "; cin >> phone; cout << "You entered " << phone << "! Isn't this neat!" << endl; return 0; }