fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class phone_number {
  7. string phone;
  8. public:
  9. phone_number(const string& p) {
  10. // validate p...
  11. if (p.size() != 10) {
  12. // Do something violent :)
  13. cerr << "The phone number is incorrect." << endl;
  14. }
  15. phone = p;
  16. }
  17. friend ostream& operator<<(ostream &os, const phone_number& p);
  18. };
  19.  
  20. ostream& operator<<(ostream &os, const phone_number& pn) {
  21. const string &p(pn.phone);
  22. os << "(" << p.substr(0, 3) << ")" << p.substr(3, 3) << "-" << p.substr(6);
  23. return os;
  24. }
  25.  
  26.  
  27. int main() {
  28. phone_number p = phone_number("6152784567");
  29. cout << p << endl;
  30. return 0;
  31. }
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
(615)278-4567