home *** CD-ROM | disk | FTP | other *** search
- // "Multiple Inheritance 1" -- RECTANGL.HPP
- /*
-
- C++ program that illustrates the following
- small class hierarchy:
-
- Rectangle Random
- | |
- | |
- | |
- | |
- | |
- \------------/
- |
- |
- RandRect
- */
-
- #include <iostream.h>
- #include <math.h>
- #include <string.h>
-
- // a variety of global constants used to generate
- // random numbers and specify the limits for the
- // random rectangle
-
- const double PI = 4 * atan(1);
- const double INIT_SEED = 113;
- const double MIN_X = 0.0;
- const double MAX_X = 1000.0;
- const double MIN_Y = 0.0;
- const double MAX_Y = 1000.0;
-
- double sqr(double x)
- { return x * x; }
-
- double cube(double x)
- { return x * x * x; }
-
- double frac(double x)
- { return x - (long)x; }
-
- class Random // parent class of RandRect
- {
- public:
- Random(double fSeed = INIT_SEED) // constructor also
- { m_fSeed = fSeed; } // initializes
-
- double getRandom();
-
- protected:
- double m_fSeed; // random number
- };
-
- class Rectangle
- {
- public:
- // construct with upper left and lower right parameters
- // and use 0 as the default value for each parameter
- Rectangle(double fX1 = 0, double fY1 = 0,
- double fX2 = 0, double fY2 = 0);
-
- // set new coordinate for upper left corner of rectangle
- void setPoint1(double fX, double fY);
-
- // set new coordinate forlower right corner of rectangle
- void setPoint2(double fX, double fY);
-
- double getWidth() // calculate rectangle width
- { return m_fX2 - m_fX1; }
- double getLength() // calculate rectangle length
- { return m_fY2 - m_fY1; }
-
- protected:
- double m_fX1; // upper left corner horizontal coordinate
- double m_fX2; // lower right corner horizontal coordinate
- double m_fY1; // upper left corner vertical coordinate
- double m_fY2; // lower right corner vertical coordinate
- };
-
- // the RandRect class multiply inherits from Random
- // and Rectangle so that random-sized rectangles can
- // be created in the main function below
-
- class RandRect : public Random, public Rectangle
- {
- public:
- // constructs with default values
- RandRect(double fMinX = MIN_X, double fMaxX = MAX_X,
- double fMinY = MIN_Y, double fMaxY = MAX_Y)
- { setLimits(fMinX, fMaxX, fMinY, fMaxY); }
-
- // declares setLimits (defined below)
- void setLimits(double fMinX, double fMaxX,
- double fMinY, double fMaxY);
-
- // declares makeRect (defined below)
- void makeRect(double& fX1, double& fY1,
- double& fX2, double& fY2);
-
- protected:
- double m_fMinX; // minimum horizontal coordinate
- double m_fMaxX; // maximum horizontal coordinate
- double m_fMinY; // minimum vertical coordinate
- double m_fMaxY; // maximum horizontal coordinate
- };
-
-
-