Advertisement
Guest User

Untitled

a guest
Jul 9th, 2020
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class PhoneNumber {
  6.  
  7. public:
  8. friend ostream& operator<<(ostream&, const PhoneNumber&);
  9. friend istream& operator>>(istream&, PhoneNumber&);
  10.  
  11. private:
  12. char first[4]; // 3 + '\0'
  13. char second[4]; // 3 + '\0'
  14. char third[3]; // 2 + '\0'
  15. char fourth[3]; // 2 + '\0'
  16.  
  17. };
  18.  
  19. ostream& operator<<(ostream& output, const PhoneNumber& num) {
  20.  
  21. output << num.first << "-" << num.second << "-" << num.third << '-' << num.fourth;
  22.  
  23. return output; //разрешить конкатенацию
  24. }
  25.  
  26. istream& operator>>(istream& input, PhoneNumber& num) {
  27.  
  28. input.getline(num.first, 4);
  29. cout << "num.first = " << num.first << endl;
  30. input.ignore();
  31.  
  32. input.getline(num.second, 4);
  33. cout << "num.second = " << num.second << endl;
  34. input.ignore();
  35.  
  36. input.getline(num.third, 3);
  37. cout << "num.third = " << num.third << endl;
  38. input.ignore();
  39.  
  40. input.getline(num.fourth, 3);
  41. cout << "num.fourth = " << num.fourth << endl;
  42.  
  43. return input; //разрешить конкатенацию
  44. }
  45.  
  46.  
  47. int main() {
  48.  
  49. PhoneNumber phone;
  50.  
  51. std::cout << "Enter a phone number in the form \n 123-456-78-90:\n";
  52.  
  53. cin >> phone;
  54.  
  55. cout << "The entered number was: " << phone << std::endl;
  56.  
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement