Source of telnum.cpp


  1: // Filename: TELNUM.CPP
  2: // Purpose:  Implementation file for a TelephoneNumber class.

  4: #include <iostream>
  5: #include "telnum.h"

  7: TelephoneNumber::TelephoneNumber()
  8: // Pre:  none
  9: // Post: The telephone number has been initialized to (902)555-1212.
 10: {
 11:     area = 902;
 12:     exchange = 555;
 13:     line = 1212;
 14: }


 17: TelephoneNumber::TelephoneNumber(/* in */ int areaCode,
 18:                                  /* in */ int number)
 19: // Pre:  "areaCode" and "number" have been initialized.
 20: // Post: The values of "areaCode" and "number" have been used
 21: //       to store the telephone number information.
 22: {
 23:     area = areaCode;
 24:     exchange = (number / 10000);
 25:     line = number % 10000;
 26: }


 29: void TelephoneNumber::Display() const
 30: // Pre:  "self" has been initialized.
 31: // Post: "self" is displayed in the form illustrated by:
 32: //       (902)425-5790
 33: {
 34:     cout << "(" << area << ")" << exchange << "-";
 35:     if (line < 1000)
 36:     {
 37:         cout << "0";
 38:         if (line < 100)
 39:         {
 40:             cout << "0";
 41:             if (line < 10)
 42:                 cout << "0";
 43:         }
 44:     }
 45:     cout << line;
 46: }


 49: int TelephoneNumber::AreaCode() const
 50: // Pre:  "self" has been initialized.
 51: // Post: Return-value is the three-digit value of the
 52: //       area code in "self"
 53: {
 54:     return area;
 55: }


 58: int TelephoneNumber::LocalNumber() const
 59: // Pre:  "self" has been initialized.
 60: // Post: Return-value is the seven-digit "local" telephone
 61: //       number part of "self", without the '-', as in
 62: //       4205790
 63: {
 64:     return exchange * 10000 + line;
 65: }