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

  1. // Figure 4 for "A Little CAD with C++"
  2. // Copyright 1988 Bruce Eckel
  3. // Permission required to distribute source
  4.  
  5. // file: cadshape.hpp
  6. /* A polymorphic base class for all shapes.
  7.    Almost all functions are virtual, so they
  8.    can be redefined in derived classes.  Then
  9.    we can make a list of shapes and draw()
  10.    each shape in the list without knowing
  11.    exactly what it is. */
  12. #ifndef CADSHAPE_HPP
  13. #define CADSHAPE_HPP
  14. class cadshape {  
  15.     unsigned x_center, y_center;
  16.   public:
  17.     // virtual functions must have SOME
  18.     // definition in the base class, even
  19.     // if it's just empty:
  20.     virtual ~cadshape() {}
  21.     virtual void draw() {}
  22.     virtual void erase() {}
  23.     void 
  24.     move(unsigned new_x, unsigned new_y ) {
  25.         x_center = new_x;
  26.         y_center = new_y;
  27.         draw(); // call proper virtual function
  28.     }
  29.     unsigned long 
  30.     range( unsigned xr, unsigned yr ) {
  31.         // a measure of distance between a 
  32.         // selected point and this object's center
  33.         unsigned long xx = 
  34.           xr > x_center ? 
  35.             xr - x_center : x_center - xr;
  36.             // (ternary if-then-else)
  37.         unsigned long yy = 
  38.           yr > y_center ? 
  39.             yr - y_center : y_center - yr;
  40.         xx *= xx;
  41.         yy *= yy;
  42.         // delta x squared + delta y squared:
  43.         return xx + yy; 
  44.     }
  45. };
  46. #endif CADSHAPE_HPP
  47.