Let's try.
Language = C++
Prompt:
A.
Write a class named Pet.
Pet should have the following private member variables:
name, age, isHungry, isBored, and energy (an integer. Any function that changes energy should keep it between 0 and 100).
And the following public member functions:
Setters and getters.
A constructor with name and age parameters which sets the bool variables to true and energy to 100. Use a constructor initializer list to initialize all member variables.
eat: If isHungry is true, print “petName eats a meal!” and set isHungry to false. Otherwise, print “petName is not hungry!”
move: print “petName runs across the room!”
rest: increase energy by 25.
isTired: returns true if energy is less than 20.
play: decrease energy by 25.
playWith: takes another Pet as a reference and prints "name plays with otherName!" Both Pets should have isBored set to false. Both pets should lose 10 energy.
Write another class named Dog, which inherits from Pet.
Dog should have the following member functions:
walk:
If the dog is tired, print "petName is tired!"
Otherwise, if the dog is not bored, print "petName wants to stay home right now!" and set isBored to true.
Otherwise, print “petName goes for a walk!” and set isBored to false. Decrease energy by 50.
speak: print “petName barks!”.
Write another class named Cat, which inherits from Pet.
Cat should have the following member functions:
climbTree: if isBored is true and the cat is not tired, print “petName climbs a tree!”, set isBored to false, and decrease energy by 25. Otherwise, print “petName would rather take a long nap!”
purr: print “petName purrs!”
Both derived classes should have constructors that call the base class constructor.
In your main function, demonstrate each of these classes and call each of their functions.
Language = C++
Prompt:
A.
Write a class named Pet.
Pet should have the following private member variables:
name, age, isHungry, isBored, and energy (an integer. Any function that changes energy should keep it between 0 and 100).
And the following public member functions:
Setters and getters.
A constructor with name and age parameters which sets the bool variables to true and energy to 100. Use a constructor initializer list to initialize all member variables.
eat: If isHungry is true, print “petName eats a meal!” and set isHungry to false. Otherwise, print “petName is not hungry!”
move: print “petName runs across the room!”
rest: increase energy by 25.
isTired: returns true if energy is less than 20.
play: decrease energy by 25.
playWith: takes another Pet as a reference and prints "name plays with otherName!" Both Pets should have isBored set to false. Both pets should lose 10 energy.
Write another class named Dog, which inherits from Pet.
Dog should have the following member functions:
walk:
If the dog is tired, print "petName is tired!"
Otherwise, if the dog is not bored, print "petName wants to stay home right now!" and set isBored to true.
Otherwise, print “petName goes for a walk!” and set isBored to false. Decrease energy by 50.
speak: print “petName barks!”.
Write another class named Cat, which inherits from Pet.
Cat should have the following member functions:
climbTree: if isBored is true and the cat is not tired, print “petName climbs a tree!”, set isBored to false, and decrease energy by 25. Otherwise, print “petName would rather take a long nap!”
purr: print “petName purrs!”
Both derived classes should have constructors that call the base class constructor.
In your main function, demonstrate each of these classes and call each of their functions.