Stop inherits from Position
authorIra W. Snyder <devel@irasnyder.com>
Wed, 3 Oct 2007 02:26:46 +0000 (19:26 -0700)
committerIra W. Snyder <devel@irasnyder.com>
Wed, 3 Oct 2007 02:26:46 +0000 (19:26 -0700)
This change makes Stop inherit from Position, since all a Stop is is a
Position with directional information tacked on.

Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
elevator.cpp
position.cpp
position.hpp
stop.cpp
stop.hpp
test.cpp

index b65cef5..b85fee9 100644 (file)
@@ -46,11 +46,13 @@ enum direction Elevator::find_best_direction ()
 
        for (above=0, below=0, it = _stops.begin(); it != _stops.end(); it++)
        {
+#if 0
                if (_pos.lowerThan (*it))
                        above++;
 
                if (_pos.higherThan (*it))
                        below++;
+#endif
        }
 
        std::cout << "above=" << above << "  below=" << below << std::endl;
index 86dc08a..545eade 100644 (file)
@@ -43,4 +43,30 @@ Position& Position::operator-= (const float rhs)
        return *this;
 }
 
+bool Position::operator< (const Position& rhs)
+{
+       if (_major < rhs._major)
+               return true;
+
+       if (_major == rhs._major)
+               if (_minor < rhs._minor)
+                       return true;
+
+       return false;
+}
+#include <iostream>
+
+bool Position::operator> (const Position& rhs)
+{
+       if (_major > rhs._major)
+               return true;
+
+       if (_major == rhs._major)
+               if (_minor > rhs._minor)
+                       return true;
+
+       return false;
+}
+
+
 /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */
index 76fa6b6..ef94846 100644 (file)
@@ -66,10 +66,14 @@ class Position
                 */
                Position& operator-= (const float rhs);
 
+               bool operator< (const Position& rhs);
+               bool operator> (const Position& rhs);
+
        protected:
-       private:
                int _major;
                int _minor;
+
+       private:
 };
 
 #endif /* POSITION_HPP */
index 1629074..6595d29 100644 (file)
--- a/stop.cpp
+++ b/stop.cpp
@@ -1,7 +1,7 @@
 #include "stop.hpp"
 
 Stop::Stop (int floor, enum direction mydirection)
-       : _floor(floor)
+       : Position(floor)
        , _direction(mydirection)
 {
        // Intentionally Left Empty
@@ -9,24 +9,7 @@ Stop::Stop (int floor, enum direction mydirection)
 
 bool Stop::operator== (Stop& rhs)
 {
-       return (_floor == rhs._floor) && (_direction == rhs._direction);
+       return (Position::operator==(rhs)) && (_direction == rhs._direction);
 }
 
-bool Stop::lowerThan (Position& rhs)
-{
-       return (_floor < rhs._floor);
-}
-
-bool Stop::higherThan (Position& rhs)
-{
-       return (_floor > rhs._floor);
-}
-
-#if 0
-bool Stop::operator< (Stop& rhs)
-{
-       return _floor < rhs._floor;
-}
-#endif
-
 /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */
index 21e9ac3..57839a2 100644 (file)
--- a/stop.hpp
+++ b/stop.hpp
@@ -10,7 +10,7 @@
 #include "position.hpp"
 #include "direction.hpp"
 
-class Stop
+class Stop : public Position
 {
        public:
                /* PURPOSE: Construct a new Stop object, and set the floor and direction
@@ -31,8 +31,6 @@ class Stop
                bool operator== (Stop& rhs);
 
        private:
-               /* Storage for the floor */
-               Position _floor;
 
                /* Storage for the direction */
                enum direction _direction;
index 9a0bf7e..e8dda09 100644 (file)
--- a/test.cpp
+++ b/test.cpp
@@ -1,23 +1,22 @@
 #include <iostream>
 using namespace std;
 
-#include "elevator.hpp"
+//#include "elevator.hpp"
+#include "stop.hpp"
 
 int main (int argc, char *argv[])
 {
-       Elevator e;
-
-       e.stop_at (2, UP);
-       e.stop_at (3, UP);
-       e.stop_at (4, UP);
-       e.stop_at (5, DOWN);
-       e.stop_at (6, DOWN);
-
-       e.move ();
-
-       Elevator e2;
-
-       e.stop_at (3, DOWN);
+       Stop s1 (1, DOWN);
+       Stop s2 (2, DOWN);
+
+       if (s1 > s2)
+               cout << "s1 > s2" << endl;
+       else if (s1 == s2)
+               cout << "s1 == s2" << endl;
+       else if (s1 < s2)
+               cout << "s1 < s2" << endl;
+       else
+               cout << "BAD BAD BAD" << endl;
 
        return 0;
 }