/* * Dog location Version 4 */ #include class Dog { private: short int x,y; public: // Constructor Dog(short int x0, short int y0); // Access to private attributes short int GetX(void); short int GetY(void); // Movement methods void Home(void); void MoveX(int amount); void MoveY(int amount); void Fence(void); }; // Constructor // Dog::Dog(short int x0, short int y0) { x = x0; y = y0; Fence(); } /* * Methods */ // Return the value of the private attribute x // short int Dog::GetX(void) { return(x); } // Return the value of the private attribute y // short int Dog::GetY(void) { return(y); } // 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.GetX() << "," << rover.GetY() << ")" << endl; cout << "Spot is at (" << spot.GetX() << "," << spot.GetY() << ")" << endl; rover.MoveX(1); cout << "Rover is at (" << rover.GetX() << "," << rover.GetY() << ")" << endl; spot.MoveY(-9); cout << "Spot is at (" << spot.GetX() << "," << spot.GetY() << ")" << endl; spot.Home(); cout << "Spot is at (" << spot.GetX() << "," << spot.GetY() << ")" << endl; }