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

  1. // \EXAMPLES\EX10012.H
  2. //--------------------------------------------------------------
  3. // definition of the class PhoneNum, List, and ListIter
  4. //--------------------------------------------------------------
  5.  
  6. //  files in this example:
  7. // %F,15,EX10011.H%EX10011.H       definition of the class String
  8. // EX10012.H       this file
  9. // %F,15,EX10013.H%EX10013.H       definition of the class PhoneBk
  10. // %F,15,EX10011.CPP%EX10011.CPP   member functions of the class String
  11. // %F,15,EX10012.CPP%EX10012.CPP   member functions of the classes
  12. //                                 PhoneNum, List, and ListIter
  13. // %F,15,EX10013.CPP%EX10013.CPP   member functions of the class PhoneBk
  14. // %F,15,EX1001.CPP%EX1001.CPP      main() to exercise this class
  15.  
  16.  
  17. #ifndef PHLIST_H
  18. #define PHLIST_H
  19.  
  20. #include <IOSTREAM.H>
  21. #include <STDDEF.H>
  22. #include "EX10011.H"
  23.  
  24. //--------------------------------------------------------------
  25. // definition of the class PhoneNum - A node in a linked list
  26. //--------------------------------------------------------------
  27.  
  28. class PhoneNum {
  29.   String name;
  30.   long tel;
  31.   PhoneNum* link;
  32.   friend class ListIter;
  33.   friend class List;
  34. public:
  35.   PhoneNum( char* n="", long tn=0 )
  36.       : name(n), tel(tn), link(NULL) {};
  37.   ~PhoneNum() {};
  38.   void change ( long tn) { tel = tn; };
  39.   long gettel() { return tel; };
  40.   int compare (char* who) { return name.compare (who); };
  41.   ostream& print(ostream& os);
  42. };
  43.  
  44.  
  45. //--------------------------------------------------------------
  46. // definition of the class List - A linked list of PhoneNum
  47. //--------------------------------------------------------------
  48.  
  49. class List {
  50.   PhoneNum* first;                   // list of PhoneNum objects
  51.   friend class ListIter;             // iterator class
  52. public:
  53.   List(): first(NULL) {};            // constructor
  54.   ~List ();                          // destructor
  55.   PhoneNum* insert(char* who, long telnum, ListIter next );
  56.   void del(PhoneNum* entry, ListIter next);    // delete an entry
  57. };
  58.  
  59.  
  60. //--------------------------------------------------------------
  61. // Definition of a class ListIter, iterator for class List
  62. //--------------------------------------------------------------
  63.  
  64. class ListIter {
  65.   List* list;                       // associated List
  66.   PhoneNum* current;                // index current entry
  67.   PhoneNum* prior;                  // index previous entry
  68. public:
  69.   ListIter(List& l): current(NULL), prior(NULL), list( &l) {};
  70.   ~ListIter() {};                   // destructor
  71.   PhoneNum* operator()();           // iterator - overload ()
  72.   PhoneNum* prev(PhoneNum* entry);  // get pointer to prior
  73. };
  74.  
  75. #endif
  76.