home *** CD-ROM | disk | FTP | other *** search
- // "Derived Classes" - TURTLE.HPP
- // This program illustrates derived classes
-
- #include <iostream.h>
- #include <math.h>
- #include <stdlib.h>
- #include <time.h>
-
- inline double sqr(double x) // inline function used below
- { // in functions that measure
- return x * x; // distances
- }
-
- class Turtle
- {
- public:
- Turtle(double fX = 0, double fY = 0); // constructor,
- // which also
- // initializes data
- // members with
- // coordinates
-
- void moveBy(double fX, double fY); // declares moveBy
- // which takes
- // two coordinates
-
- double getX() // returns the
- { return m_fX; } // horizontal coordinate
- // data member (m_fX)
-
- double getY() // returns the
- { return m_fY; } // vertical coordinate
- // data member (m_fY)
-
- void showXY(); // displays current position
-
- double getDistanceFromStart() // measures
- { return sqrt(sqr(m_fX - m_fX0) + // distance from
- sqr(m_fY - m_fY0)); } // start
-
- double getDistanceFromOrigin() // measures
- { return sqrt(sqr(m_fX) + sqr(m_fY)); } // distance from
- // origin
- protected:
- double m_fX; // current horizontal coordinate
- double m_fY; // current vertical coordinate
- double m_fX0; // starting horizontal coordinate
- double m_fY0; // starting vertical coordinate
- };
-
- class SuperTurtle : public Turtle // derives SuperTurtle
- // from Turtle
- {
- public:
- SuperTurtle(double fX = 0, double fY = 0); // declares
- // constructor
- // with default
- // values
-
- void moveBy(double fX, double fY); // declares inherited
- // function so it can
- // be extended
-
- void moveToLast(); // declares new
- // function defined
- // below
-
- int getNumMoves() // returns the new
- { return m_nMoves; } // data member (m_nMoves)
-
- protected:
- double m_fLastX; // previous horizontal position
- // used in moveToLast function
- // defined below
-
- double m_fLastY; // previous vertical position
- // used in moveToLast function
- // defined below
-
- int m_nMoves; // tracks number of moves
-
- }; // Note: SuperTurtle is a derived class of
- // Turtle so it also inherits TurtleÆs
- // four data members
-