home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_44.arc / MICROCAD.ARC / SHAPELST.HPP < prev    next >
C/C++ Source or Header  |  1988-08-08  |  1KB  |  54 lines

  1. // Figure 11 for "A Little CAD with C++"
  2. // Copyright 1988 Bruce Eckel
  3. // Permission required to distribute source
  4.  
  5. // file: shapelst.hpp
  6. /* Two classes to manage a list of cadshapes.
  7.    A shapelist contains a list of (what else)
  8.    shapelist_elements, which simply hold
  9.    a cadshape and a link to the next
  10.    element.  The usual linked list stuff:
  11.    you can insert, step through, etc. */
  12. #ifndef SHAPELST_HPP
  13. #define SHAPELST_HPP
  14. #include "cadshape.hpp"
  15.  
  16. // tell the compiler the class exists:
  17. class shapelist;
  18.  
  19. class shapelist_element {
  20.     cadshape * shp;
  21.     shapelist_element * next;
  22.   public:
  23.     // allow shapelist access to the
  24.     // private elements of this class:
  25.     friend shapelist;
  26.     shapelist_element(cadshape * s, 
  27.       shapelist_element * hd) {
  28.         shp = s;
  29.         next = hd;
  30.     }
  31. };
  32.  
  33. class shapelist {
  34.     shapelist_element * head, * current;
  35.   public:
  36.     shapelist() { current = head = 
  37.        new shapelist_element(
  38.            (cadshape *)0, (shapelist_element *)0);
  39.     } // end of list marked by null pointers
  40.     void insert(cadshape * s) {
  41.         shapelist_element * p = 
  42.             new shapelist_element(s, head);
  43.         head = p;
  44.     }
  45.     void reset() { current = head; }
  46.     cadshape * next();
  47.     void remove(cadshape * s);
  48.     // hunt through the list and find the 
  49.     // nearest element to x,y:
  50.     cadshape * nearest(unsigned x, unsigned y);
  51. };
  52.  
  53. #endif SHAPELST_HPP
  54.