/* * Dog location Version 3 */ #include class Dog { public: short int x,y; // Constructor Dog(short int x0, short int y0); // Movement methods void Home(void); void MoveX(int amount); void MoveY(int amount); void Fence(); }; // Constructor // Dog::Dog(short int x0, short int y0) { x = x0; y = y0; Fence(); } /** Methods **/ // Send a Dog home (0,0) void Dog::Home(void) { x = 0; y = 0; } // Move a Dog in the X direction void Dog::MoveX(int amount) { x += amount; Fence(); } // Move a Dog in the Y direction void Dog::MoveY(int amount) { y += amount; Fence(); } // Contain the Dog within a 30 x 20 playground // void Dog::Fence(void) { if(x < 0) x = 0; if(x > 29) x = 29; if(y < 0) y = 0; if(y > 19) y = 19; } void main() { Dog rover(5,6), spot(10,3); cout << "Rover is at (" << rover.x << "," << rover.y << ")" << endl; cout << "Spot is at (" << spot.x << "," << spot.y << ")" << endl; rover.MoveX(1); cout << "Rover is at (" << rover.x << "," << rover.y << ")" << endl; spot.MoveY(-9); cout << "Spot is at (" << spot.x << "," << spot.y << ")" << endl; spot.Home(); cout << "Spot is at (" << spot.x << "," << spot.y << ")" << endl; }