home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / Rectangl.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  3.1 KB  |  109 lines

  1. // "Multiple Inheritance 1" -- RECTANGL.HPP
  2. /*
  3.  
  4.    C++ program that illustrates the following
  5.    small class hierarchy:
  6.  
  7.       Rectangle      Random
  8.          |            |
  9.          |            |
  10.          |            |
  11.          |            |
  12.          |            |
  13.          \------------/
  14.                 |
  15.                 |
  16.              RandRect
  17. */
  18.  
  19. #include <iostream.h>
  20. #include <math.h>
  21. #include <string.h>
  22.  
  23. // a variety of global constants used to generate
  24. // random numbers and specify the limits for the
  25. // random rectangle
  26.  
  27. const double PI = 4 * atan(1);
  28. const double INIT_SEED = 113;
  29. const double MIN_X = 0.0;
  30. const double MAX_X = 1000.0;
  31. const double MIN_Y = 0.0;
  32. const double MAX_Y = 1000.0;
  33.  
  34. double sqr(double x)
  35. {  return x * x; }
  36.  
  37. double cube(double x)
  38. { return x * x * x; }
  39.  
  40. double frac(double x)
  41. { return x - (long)x; }
  42.  
  43. class Random                      // parent class of RandRect
  44. {
  45.   public:
  46.     Random(double fSeed = INIT_SEED)   // constructor also
  47.       { m_fSeed = fSeed; }             // initializes
  48.  
  49.     double getRandom();
  50.  
  51.   protected:
  52.     double m_fSeed;                    // random number
  53. };
  54.  
  55. class Rectangle
  56. {
  57.   public:
  58.     // construct with upper left and lower right parameters
  59.     // and use 0 as the default value for each parameter
  60.     Rectangle(double fX1 = 0, double fY1 = 0,
  61.               double fX2 = 0, double fY2 = 0);
  62.  
  63.     // set new coordinate for upper left corner of rectangle
  64.     void setPoint1(double fX, double fY);
  65.  
  66.     // set new coordinate forlower right corner of rectangle
  67.     void setPoint2(double fX, double fY);
  68.  
  69.     double getWidth()             // calculate rectangle width
  70.       { return m_fX2 - m_fX1; }
  71.     double getLength()            // calculate rectangle length
  72.       { return m_fY2 - m_fY1; }
  73.  
  74.   protected:
  75.     double m_fX1;  // upper left corner horizontal coordinate
  76.     double m_fX2;  // lower right corner horizontal coordinate
  77.     double m_fY1;  // upper left corner vertical coordinate
  78.     double m_fY2;  // lower right corner vertical coordinate
  79. };
  80.  
  81. // the RandRect class multiply inherits from Random
  82. // and Rectangle so that random-sized rectangles can
  83. // be created in the main function below
  84.  
  85. class RandRect : public Random, public Rectangle    
  86. {                                                   
  87.   public:
  88.     // constructs with default values
  89.     RandRect(double fMinX = MIN_X, double fMaxX = MAX_X,
  90.              double fMinY = MIN_Y, double fMaxY = MAX_Y)
  91.       { setLimits(fMinX, fMaxX, fMinY, fMaxY); }
  92.  
  93.     // declares setLimits (defined below)
  94.     void setLimits(double fMinX, double fMaxX,
  95.                    double fMinY, double fMaxY);
  96.  
  97.     // declares makeRect (defined below)
  98.     void makeRect(double& fX1, double& fY1,
  99.                   double& fX2, double& fY2);
  100.  
  101.   protected:
  102.     double m_fMinX;        // minimum horizontal coordinate  
  103.     double m_fMaxX;        // maximum horizontal coordinate
  104.     double m_fMinY;        // minimum vertical coordinate
  105.     double m_fMaxY;        // maximum horizontal coordinate
  106. };
  107.  
  108.  
  109.