PhoneNumber extractor : Extractor « Overload « C++






PhoneNumber extractor

PhoneNumber extractor
#include <iostream>
#include <cstring>
using namespace std;
class PhoneNumber {
  char name[80];
  int areaCode;
  int prefix;
  int num;
public:
  PhoneNumber() { };
  PhoneNumber(char *n, int a, int p, int nm)
  {
    strcpy(name, n);
    areaCode = a;
    prefix = p;
    num = nm;
  }
  friend ostream &operator<<(ostream &stream, PhoneNumber o);
  friend istream &operator>>(istream &stream, PhoneNumber &o);
};

ostream &operator<<(ostream &stream, PhoneNumber o)
{
  stream << o.name << " ";
  stream << "(" << o.areaCode << ") ";
  stream << o.prefix << "-" << o.num << "\n";
  return stream; // must return stream
}
istream &operator>>(istream &stream, PhoneNumber &o)
{
  cout << "Enter name: ";
  stream >> o.name;
  cout << "Enter area code: ";
  stream >> o.areaCode;
  cout << "Enter prefix: ";
  stream >> o.prefix;
  cout << "Enter number: ";
  stream >> o.num;
  cout << "\n";
  return stream;
}
int main()
{
  PhoneNumber a;
  cin >> a;
  cout << a;
  return 0;
}


           
       








Related examples in the same category

1.Add a friend extractor for objects of type MyClass.Add a friend extractor for objects of type MyClass.
2.Overload << and >>Overload << and >>