home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX06021.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  4.8 KB  |  128 lines

  1. // \EXAMPLES\EX06021.CPP
  2. // the prototype implementation of a phone book class
  3. //  member function definitions for PhoneBk class
  4.  
  5. //--------------------------------------------------------------
  6. //  files in this example:
  7. // %F,15,EX0602.H%EX0602.H      definition of the class PhoneBk
  8. // EX06021.CPP   this file -- member functions of PhoneBk
  9. // %F,15,EX06022.CPP%EX06022.CPP   main() to exercise this class
  10. //--------------------------------------------------------------
  11.  
  12. #include "EX0602.H"
  13.  
  14. //--------------------------------------------------------------
  15. // the copy constructor for the PhoneBk class
  16. // PARAMETER:  const          const - do not change object
  17. //                PhoneBk&    & - reference to a PhoneBk object
  18. //                  oldbook   the PhoneBk object to copy
  19. //--------------------------------------------------------------
  20.  
  21. PhoneBk::PhoneBk( const PhoneBk& oldbook)
  22. // new PhoneBk has same number of entries as oldbook
  23.   { numEntry = oldbook.numEntry;
  24. // copy entries from oldbook, one by one
  25.       for ( int i = 0; i < numEntry; i++)
  26. // use the standard function strcopy to duplicate each name
  27.       { strcpy( (char*) &names[i], (char*) &oldbook.names[i] );
  28. // duplicate the telephone number for an entry
  29.         tel[i] = oldbook.tel[i];
  30.       };
  31. };
  32.  
  33. //--------------------------------------------------------------
  34. // The function insert() adds or changes an entry in a phonebook
  35. // PARAMETERS:  char* who     the name of the new entry- if
  36. //                            already in the book, change number
  37. //              long  telnum  then new telephone number
  38. // RETURNS:     int   1       success
  39. //              int   0       failure,  phonebook full
  40. //--------------------------------------------------------------
  41.  
  42. int PhoneBk::insert( char* who, long telnum)
  43. // look at each name entry in the book to see if its matches who
  44.   { for( int i = 0; i < numEntry; i++)
  45.     { if( strcmp( (char*) &names [i], who ) )
  46.         continue;
  47. // If name matched, set the associated telephone number = telnum
  48.     tel[i] = telnum;
  49.     return ( 1 );
  50.     }
  51. // If the book is full, new name and number can not be added
  52.   if( numEntry == 100 )
  53.     return ( 0 );
  54. // Add new netry
  55.   tel[numEntry] = telnum;
  56.   strcpy( (char*) &names [numEntry++], who );
  57.   return ( 1 );
  58. };
  59.  
  60. //--------------------------------------------------------------
  61. // The function remove() deletes an entry from a PhoneBk object
  62. // PARAMETERS:  char* who    the name of the entry to delete
  63. // RETURNS:     int   1      success
  64. //              int   0      failure,  name not in phonebook
  65. //--------------------------------------------------------------
  66.  
  67. int PhoneBk::remove( char* who)
  68. // look at each name entry in the book to see if its matches who
  69. {  for( int i = 0; i < numEntry; i++)
  70.    { if( strcmp( (char*) &names [i], who ) )
  71.        continue;
  72. // entry found, delete it - do not leave gaps between entries
  73. // is this the only entry?
  74.      if( numEntry > 1 )
  75. // if there are more than one entry, overwrite with last entry
  76.      { tel[i] = tel[--numEntry];
  77.        strcpy( (char*) &names [i], (char*) &names[numEntry] );
  78.        return ( 1 );
  79.      }
  80. // if the entry to delete is the only entry, just reset counter
  81.      else
  82.      { --numEntry;
  83.        return ( 1 );
  84.      }
  85.    }
  86. // if name not found in book, return failure code
  87.    return ( 0 );
  88. };
  89.  
  90. //--------------------------------------------------------------
  91. // The function lookup() finds a name in a PhoneBk object
  92. //                       and prints associated telephone number
  93. // PARAMETERS:  char* who     the name of the entry
  94. // RETURNS:     long  !=0     success, the telephone number
  95. //              long  0       failure,  name not in phonebook
  96. //--------------------------------------------------------------
  97.  
  98. long PhoneBk::lookup( char* who)
  99. // look at each name entry in the book to see if its matches who
  100. { for( int i = 0; i < numEntry;  i++)
  101.      if( strcmp( (char*) &names [i], who ) == 0 )
  102. // return telephone number associated with matching name
  103.        return ( tel [i] );
  104. // if name not found in book, return failure code
  105.    return ( 0 );
  106. };
  107.  
  108. //--------------------------------------------------------------
  109. // The function print() lists every name and telephone number
  110. // PARAMETERS:  ostream& os      the output stream
  111. // RETURNS:     ostream& os      the output stream
  112. //--------------------------------------------------------------
  113.  
  114. ostream& PhoneBk::print (ostream& os)
  115. // consider every entry in turn
  116. { for( int i = 0; i < numEntry;  i++)
  117. // display name, tab for formatting, telephone number, newline
  118.    os << (char*) &names [i]
  119.       << "\t"
  120.       << tel[i]
  121.       << endl;
  122. // add an end of file message
  123.    os << "EOF"
  124.       << endl;
  125.    return ( os );
  126. };
  127. //--------------------------------------------------------------
  128.