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

  1. //  \EXAMPLES\EX08064.CPP
  2. //  member functions of the class PhoneBk
  3. //--------------------------------------------------------------
  4.  
  5. //  files in this example:
  6. // %F,15,EX08061.H%EX08061.H       definition of class String
  7. // %F,15,EX08062.H%EX08062.H       definition of class PhoneNum
  8. // %F,15,EX08063.H%EX08063.H       definition of List and ListIter
  9. // %F,15,EX08064.H%EX08064.H       definition of class PhoneBk
  10. // %F,15,EX08061.CPP%EX08061.CPP     member functions of the String class
  11. // %F,15,EX08062.CPP%EX08062.CPP     member functions of the class PhoneNum
  12. // %F,15,EX08063.CPP%EX08063.CPP     member functions List and ListIter
  13. // EX08064.CPP     this file
  14. // %F,15,EX0806.CPP%EX0806.CPP   main to exercise PhoneBk class
  15. //--------------------------------------------------------------
  16. #include "EX08064.H"
  17. #include "EX08063.H"
  18.  
  19. //--------------------------------------------------------------
  20. // Constructor
  21. //--------------------------------------------------------------
  22. PhoneBk::PhoneBk() : numEntry (0), maxsize (100), list (100)
  23. { };
  24.  
  25. //--------------------------------------------------------------
  26. // The function insert() adds or changes an entry in a phonebook
  27. // PARAMETERS:  char* who     the name of the new entry- if
  28. //                            already in the book, change number
  29. //              long  telnum  then new telephone number
  30. // RETURNS:     int   1       success
  31. //              int   0       failure,  phonebook full
  32. //--------------------------------------------------------------
  33. int PhoneBk::insert( char* who, long telnum)
  34. // look at each name entry in the book to see if its matches who
  35. { ListIter place;
  36.   for( int i = 0; i < numEntry; i++)
  37.     { PhoneNum* entry = place.next(list);
  38.       if( entry->compare(who ) )
  39.         continue;
  40. // If name matched, set the associated telephone number = telnum
  41.     entry->change(telnum);
  42.     return ( 1 );
  43.     }
  44. // If the book is full, new name and number can not be added
  45.   if( numEntry == 100 )
  46.     return ( 0 );
  47. // Add new entry
  48.   PhoneNum newentry( who, telnum);
  49.   list.add(newentry);
  50.   numEntry++;
  51.   return ( 1 );
  52. };
  53.  
  54. //--------------------------------------------------------------
  55. // The function remove() deletes an entry from a PhoneBk object
  56. // PARAMETERS:  char* who    the name of the entry to delete
  57. // RETURNS:     int   1      success
  58. //              int   0      failure,  name not in phonebook
  59. //--------------------------------------------------------------
  60. int PhoneBk::remove( char* who)
  61. // look at each name entry in the book to see if its matches who
  62. { ListIter place;
  63.   for( int i = 0; i < numEntry; i++)
  64.   { PhoneNum* entry = place.next(list);
  65.     if( entry->compare( who ) )
  66.       continue;
  67. // entry found, delete it - do not leave gaps between entries
  68. // is this the only entry?
  69.    list.del(entry);
  70.     --numEntry;
  71.     return ( 1 );
  72.   };
  73. // if name not found in book, return failure code
  74.   return ( 0 );
  75. };
  76.  
  77. //--------------------------------------------------------------
  78. // The function lookup() finds a name in a PhoneBk object
  79. //                       and prints associated telephone number
  80. // PARAMETERS:  char* who     the name of the entry
  81. // RETURNS:     long  !=0     success, the telephone number
  82. //              long  0       failure,  name not in phonebook
  83. //--------------------------------------------------------------
  84. long PhoneBk::lookup( char* who)
  85. // look at each name entry in the book to see if its matches who
  86. { ListIter place;
  87.   for( int i = 0; i < numEntry;  i++)
  88.   { PhoneNum* entry = place.next(list);
  89.     if( entry->compare( who ) == 0 )
  90. // return telephone number associated with matching name
  91.        return ( entry->gettel());
  92.   }
  93. // if name not found in book, return failure code
  94.   return ( 0 );
  95. };
  96.  
  97. //--------------------------------------------------------------
  98. // The function print() lists every name and telephone number
  99. // PARAMETERS:  ostream& os      the output stream
  100. // RETURNS:     ostream& os      the output stream
  101. //--------------------------------------------------------------
  102. ostream& PhoneBk::print (ostream& os)
  103. // consider every entry in turn
  104. { ListIter place;
  105.   for( int i = 0; i < numEntry;  i++)
  106.   { PhoneNum* entry = place.next(list);
  107. // display name, tab for formatting, telephone number, newline
  108.     entry->print(os);
  109.    }
  110.    os << "EOF" << endl;
  111.    return(os);
  112. };
  113.  
  114. //--------------------------------------------------------------
  115.