home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX07025.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  1.5 KB  |  68 lines

  1. // ex07025.cpp
  2. // static member functions
  3. #include <iostream.h>
  4. #include <string.h>
  5.  
  6. class ListEntry {
  7.     static ListEntry *lastentry; // a static list head pointer
  8.     char *listvalue;
  9.     ListEntry *nextentry;
  10. public:
  11.     ListEntry();
  12.     ListEntry(char *);
  13.     ~ListEntry() { if (listvalue != NULL) delete listvalue; }
  14.     ListEntry *NextEntry() { return nextentry; };
  15.     void display() { cout << '\n' << listvalue; }
  16.     // ------- a static member function
  17.     static void showlast();
  18. };
  19.  
  20. ListEntry *ListEntry::lastentry;
  21.  
  22. ListEntry::ListEntry()
  23. {
  24.     listvalue = NULL;
  25.     nextentry = NULL;
  26.     lastentry = this;
  27. }
  28. ListEntry::ListEntry(char *s)
  29. {
  30.     lastentry->nextentry = this;
  31.     lastentry = this;
  32.     listvalue = new char[strlen(s)+1];
  33.     strcpy(listvalue, s);
  34.     nextentry = NULL;
  35. }
  36.  
  37. // ---------- a static member function
  38. void ListEntry::showlast()
  39. {
  40.     lastentry->display();
  41.     cout << " is the last entry in the list";
  42. }
  43.  
  44. main()
  45. {
  46.     ListEntry listhead;    // ---- this is the list head
  47.     // ---------- read in some names
  48.     while (1)    {
  49.         cout << "\nEnter a name: ";
  50.         char name[25];
  51.         cin >> name;
  52.         if (strncmp(name, "end", 3) == 0)
  53.             break;
  54.         // -------- make a list entry of the name
  55.         new ListEntry(name);
  56.         // ----- call the static member function
  57.         listhead.showlast();
  58.     }
  59.     // ------- delete the entries
  60.     ListEntry *next = listhead.NextEntry();
  61.     while (next != NULL)    {
  62.         ListEntry *hold = next;
  63.         next = next->NextEntry();
  64.         // -------- delete the ListEntry
  65.         delete hold;
  66.     }
  67. }
  68.