/* * Dog location Version 5 */ #include class Dog { private: short int x,y; short int speed; public: // Constructor Dog(short int x0, short int y0); // Access to private attributes short int GetX(void); short int GetY(void); // Set a new speed void ChangeSpeed(int s); // 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; speed = 1; Fence(); } /* * Methods */ // Set the private attribute speed // void Dog::ChangeSpeed(int s) { speed = s; } // 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 * speed); Fence(); } // Move a Dog in the Y direction // void Dog::MoveY(int amount) { y += (amount * speed); 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); spot.ChangeSpeed(3); spot.MoveX(1); cout << "Rover is at (" << rover.GetX() << "," << rover.GetY() << ")" << endl; cout << "Spot is at (" << spot.GetX() << "," << spot.GetY() << ")" << endl; }