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

  1. // "Derived Classes" - TURTLE.HPP
  2. // This program illustrates derived classes
  3.  
  4. #include <iostream.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. inline double sqr(double x)   // inline function used below
  10. {                             // in functions that measure
  11.   return x * x;               // distances
  12. }
  13.  
  14. class Turtle
  15. {
  16.   public:
  17.     Turtle(double fX = 0, double fY = 0);  // constructor,
  18.                                            // which also
  19.                                            // initializes data
  20.                                            // members with
  21.                                            // coordinates
  22.  
  23.     void moveBy(double fX, double fY);     // declares moveBy
  24.                                            // which takes
  25.                                            // two coordinates
  26.  
  27.     double getX()                  // returns the
  28.       { return m_fX; }             // horizontal coordinate
  29.                                    // data member (m_fX)
  30.  
  31.     double getY()                  // returns the
  32.       { return m_fY; }             // vertical coordinate
  33.                                    // data member (m_fY)
  34.  
  35.     void showXY();                 // displays current position
  36.  
  37.     double getDistanceFromStart()             // measures
  38.       { return sqrt(sqr(m_fX - m_fX0) +       // distance from
  39.                     sqr(m_fY - m_fY0)); }     // start
  40.  
  41.     double getDistanceFromOrigin()            // measures
  42.       { return sqrt(sqr(m_fX) + sqr(m_fY)); } // distance from
  43.                                               // origin
  44.   protected:
  45.     double m_fX;             // current horizontal coordinate
  46.     double m_fY;             // current vertical coordinate
  47.     double m_fX0;            // starting horizontal coordinate
  48.     double m_fY0;            // starting vertical coordinate
  49. };
  50.  
  51. class SuperTurtle : public Turtle     // derives SuperTurtle 
  52.                                       // from Turtle
  53. {
  54.   public:
  55.     SuperTurtle(double fX = 0, double fY = 0);  // declares
  56.                                                 // constructor
  57.                                                 // with default
  58.                                                 // values
  59.  
  60.     void moveBy(double fX, double fY);    // declares inherited
  61.                                           // function so it can
  62.                                           // be extended
  63.  
  64.     void moveToLast();           // declares new
  65.                                  // function defined
  66.                                  // below
  67.  
  68.     int getNumMoves()            // returns the new
  69.       { return m_nMoves; }       // data member (m_nMoves)
  70.  
  71.   protected:
  72.     double m_fLastX;          // previous horizontal position
  73.                               // used in moveToLast function
  74.                               // defined below
  75.  
  76.     double m_fLastY;          // previous vertical position
  77.                               // used in moveToLast function
  78.                               // defined below
  79.  
  80.     int m_nMoves;             // tracks number of moves
  81.         
  82. };                 // Note: SuperTurtle is a derived class of
  83.                    // Turtle so it also inherits TurtleÆs
  84.                    // four data members
  85.