/* * Animal, Dog and Cat */ #include class Animal { private: float x,y; float speed; public: // Constructor Animal(short int x0, short int y0); // Access to private attributes short int GetX(void); short int GetY(void); // Set a new speed, two versions void ChangeSpeed(int s); void ChangeSpeed(double s); // Movement methods void Home(void); void MoveX(int amount); void MoveY(int amount); void Fence(void); }; class Dog : public Animal { public: bool trained; // Constructor Dog(void); // Methods void Play(void); }; class Cat : public Animal { public: bool declawed; // Constructor Cat(void); //Methods void Play(void); }; /* * Animal methods */ // Constructor // Animal::Animal(short int x0, short int y0) { x = x0; y = y0; speed = 1.0; Fence(); } /* * Methods */ // Set the private attribute speed, // given an integer argument. // void Animal::ChangeSpeed(int s) { speed = (float) s; } // Set the private attribute speed, // given a double argument. // void Animal::ChangeSpeed(double s) { speed = s; } // Return the value of the private attribute x, // rounded to the nearest integer value. // short int Animal::GetX(void) { return((short int) (x + 0.5)); } // Return the value of the private attribute y // rounded to the nearest integer value. // short int Animal::GetY(void) { return((short int) (y + 0.5)); } // Send a Animal home (0,0) // void Animal::Home(void) { x = 0; y = 0; } // Move a Animal in the X direction // void Animal::MoveX(int amount) { x += (amount * speed); Fence(); } // Move a Animal in the Y direction // void Animal::MoveY(int amount) { y += (amount * speed); Fence(); } // Contain the Animal within a 30 x 20 playground // void Animal::Fence(void) { if(x < 0) x = 0; if(x > 29) x = 29; if(y < 0) y = 0; if(y > 19) y = 19; } /* * Dog Methods */ // Construct a Dog, at (0,0) // Dog::Dog(void) : Animal(0,0) { trained = false; } // Play with a Dog // void Dog::Play(void) { cout << "Let's play fetch!" << endl; } /* * Cat Methods */ // Construct a Cat, at (29,19) // Cat::Cat(void) : Animal(29,19) { declawed = false; } // Play with a Cat // void Cat::Play(void) { cout << "Let's play with a ball of yarn!" << endl; } /* * Main routine to test Animal, Dog and Cat classes */ void main() { Dog rover; Cat fluffy; cout << "Rover is at (" << rover.GetX() << "," << rover.GetY() << ")" << endl; cout << "Fluffy is at (" << fluffy.GetX() << "," << fluffy.GetY() << ")" << endl; cout << "Rover says: "; rover.Play(); cout << "Fluffy says: "; fluffy.Play(); }