From: Ira W. Snyder Date: Wed, 3 Oct 2007 02:26:46 +0000 (-0700) Subject: Stop inherits from Position X-Git-Url: https://irasnyder.com/gitweb/?a=commitdiff_plain;h=fb458316875aebf0ab70211d6aac8b3d68556b52;p=cs356-p1-elevator.git Stop inherits from Position 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 --- diff --git a/elevator.cpp b/elevator.cpp index b65cef5..b85fee9 100644 --- a/elevator.cpp +++ b/elevator.cpp @@ -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; diff --git a/position.cpp b/position.cpp index 86dc08a..545eade 100644 --- a/position.cpp +++ b/position.cpp @@ -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 + +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: */ diff --git a/position.hpp b/position.hpp index 76fa6b6..ef94846 100644 --- a/position.hpp +++ b/position.hpp @@ -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 */ diff --git a/stop.cpp b/stop.cpp index 1629074..6595d29 100644 --- 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: */ diff --git a/stop.hpp b/stop.hpp index 21e9ac3..57839a2 100644 --- 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; diff --git a/test.cpp b/test.cpp index 9a0bf7e..e8dda09 100644 --- a/test.cpp +++ b/test.cpp @@ -1,23 +1,22 @@ #include 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; }