/********************************************************************************** *** FILE: mov_obj.cpp *** DATE: Jan 14, 2004 *** AUTHOR: Daniel Dementiev *** GOAL: This is a short example illustrating class definition syntax in C++. *** Designed for IST482 class. **********************************************************************************/ #include using namespace std; class MovingObject { public: enum Direction {North, West, South, East}; MovingObject(); int getX(); int getY(); void step(); void speed_up(); void slow_down(); void turn(Direction new_dir); private: int x, y; // current coordinates of the object Direction dir; // direction of the object int speed; // speed of the object }; inline MovingObject::MovingObject() { x = y = 0; speed = 1; dir = South; } inline int MovingObject::getX() { return x; } inline int MovingObject::getY() { return y; } inline void MovingObject::step() { switch( dir ){ case North: y += speed; break; case West: x -= speed; break; case South: y -= speed; break; case East: x += speed; break; } } inline void MovingObject::speed_up() { speed++; } inline void MovingObject::slow_down() { if( speed ) speed--; } inline void MovingObject::turn(Direction new_dir) { dir = new_dir; } /* ------------------------------------------------------------------------------ */ void main() { MovingObject tank; for(int i=0;i<100;i++){ if( i%4 == 0 ) // change direction switch( rand()%4 ){ case 0: tank.turn(MovingObject::North); break; case 1: tank.turn(MovingObject::South); break; case 2: tank.turn(MovingObject::East); break; case 3: tank.turn(MovingObject::West); break; } if( i%7 == 0 ) // change speed if( i%7>3 ) tank.slow_down(); else tank.speed_up(); tank.step(); cout<<"Tank is now at "<