Еще один пример, если можно:
Код:
![Выделить весь код](images/misc/selectcode.png)
//MAIN.CPP
//Перегрузка операций поместить в поток и взять из потока
#include <iostream>
#if !defined(__USING_STD_NAMES__) && defined(__cplusplus)
using namespace std;
#endif
class PhoneNumber
{
friend ostream &operator << (ostream &, const PhoneNumber &);
friend istream &operator >> (istream &, PhoneNumber &);
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();
input.getline(num.areaCode, 4);
input.ignore(2);
input.getline(num.exchange, 4);
input.ignore();
input.getline(num.line, 5);
return input;
}
int main()
{
setlocale(LC_ALL,".1251");
PhoneNumber phone;
cout << "Введите номер телефона в "
<< "виде (123) 456-7890:" << endl;
cin >> phone;
cout << "Был введен номер телефона:" << endl << phone << endl;
system("pause");
return 0;
}
Пример компилируется, но не правильно работает. Не могу разобраться в чем дело. Помогите.