IDI Software Component Library

Telephone Number (C++ Class Definitions)
© 2005 Information Disciplines, Inc., Chicago


Conrad Weisert, Information Disciplines, Inc., Chicago


To standardize the representation of telephone numbers in databases and to facilitate their manipulation. (This class does not support dialing operations or other technical telephony functions.)

This version supports North American telephone numbers, and also provides a base class from which you can derive telephone number classes for other countries.


This class was inspired by a 1998-1999 software development project done with Integrated Design Strategies, Inc. (Oak Brook, Illinois) for GTE Wireless.


Two classes are defined:

  1. Abstract base class PhoneNumberBase assumes four components with pure virtual accessor functions to retrieve them:
          unsigned short  countryCode()  
          unsigned short  areaCode()
          unsigned short  exchange()
          unsigned long   digits()
    where short is assumed to be 16 bits and long at least 32 bits. (The actual member data in the derived classes may be shorter.) Extensions are not considered part of the telephone number.

    The six relational operators == != < <= > >= use those accessors, and therefore need not be implemented separately in the derived classes.

    The virtual predicate method bool isLegal() tests for an illegal value; if none exists for the derived class it returns true.

    The output stream insertion operator << invokes the pure virtual function ostream& print(ostream& s) to produce a suitable external representation for the appropriate country.

  2. Derived class NA_PhoneNumber is the concrete representation of U.S. and Canadian phone numbers.


     NA_TelephoneNumber(const short area,
                        const short exch, const long digits) 
     NA_TelephoneNumber(const NA_TelephoneNumber&)    //  Copy constructor
     NA_TelephoneNumber()    //  Default constructor yields "illegal" value.

    Editing rules

    A legal value is one where:

      200  <  area  ≤ 999
      200  <  exch  ≤ 999
      area modulo 100 ≠ 11
      area modulo 100 ≠ 0, (800 is OK) 
      exch modulo 100 ≠ 11
      exch modulo 100 ≠ 0
      0000 ≤ digits ≤ 9999 
    The 3-parameter constructor enforces these rules by setting the internal value of an illegal phone number to zero. No exception is thrown. The isLegal() method tests for a non-zero area code. You can modify the rules by editing the source code for the constructor.

    External representation

    The output stream insertion operator yields the common 14-character format with parenthesized area code, e.g.:
    (773) 736-9661. You can change it by editing the source code for the print function.

    Alternative accessor names

    For consistency with some North American telephony applications, the non-virtual functions NPA() and NXX() can be used instead of areaCode() and exchange() respectively.


If the application uses only North American telephone numbers, you can improve both the execution speed of the virtual functions and the size of each telephone-number object by removing the abstract base class TelephoneNumber and moving its functionality into NA_PhoneNumber. Each object will then consume 6 bytes in most implementations.


Some users reserve the unused 555 exchange conventionally to represent an "unknown" value.

Observations about the chaotic area-code situation in North America can be found in Area Code Hell,

Return to Freeware directory
Return to IDI home page

Last modified November 16, 2005