< Previous | Contents | Next >
The Critter class is the blueprint for the object that represents the player’s critter. The class isn’t complicated, and most of it should look familiar, but it’s long enough that it makes sense to attack it in pieces.
After some initial comments and statements, I begin the Critter class.
//Critter Caretaker
//Simulates caring for a virtual pet #include <iostream>
using namespace std;
class Critter
{
public:
Critter(int hunger = 0, int boredom = 0); void Talk();
void Eat(int food = 4); void Play(int fun = 4);
private:
int m_Hunger; int m_Boredom;
int GetMood() const;
void PassTime(int time = 1);
};
m_Hunger is a private data member that represents the critter’s hunger level while m_Boredom is a private data member that represents its boredom level. I’ll go through each member function in its own section.
The constructor takes two arguments, hunger and boredom. The arguments each have a default value of zero, which I specified in the constructor prototype back
278 Chapter 8 n Classes: Critter Caretaker
in the class definition. I use hunger to initialize m_Hunger and boredom to initialize m_Boredom.
Critter::Critter(int hunger, int boredom): m_Hunger(hunger),
m_Boredom(boredom)
{}
Next, I define GetMood().
inline int Critter::GetMood() const
{
return (m_Hunger + m_Boredom);
}
The return value of this inlined member function represents a critter’s mood. As the sum of a critter’s hunger and boredom levels, a critter’s mood gets worse as the number increases. I made this member function private because it should only be invoked by another member function of the class. I made it constant since it won’t result in any changes to data members.
PassTime() is a private member function that increases a critter’s hunger and boredom levels. It’s invoked at the end of each member function where the critter does something (eats, plays, or talks) to simulate the passage of time. I made this member function private because it should only be invoked by another member function of the class.
void Critter::PassTime(int time)
{
m_Hunger += time; m_Boredom += time;
}
You can pass the member function the amount of time that has passed; otherwise, time gets the default argument value of 1, which I specify in the member function prototype in the Critter class definition.
Introducing the Critter Caretaker Game 279
The Talk() member function announces the critter’s mood, which can be happy, okay, frustrated, or mad. Talk() calls GetMood() and, based on the return value, displays the appropriate message to indicate the critter’s mood. Finally, Talk() calls PassTime() to simulate the passage of time.
void Critter::Talk()
{
cout << "I’m a critter and I feel ";
int mood = GetMood(); if (mood > 15)
{
cout << "mad.\n";
}
else if (mood > 10)
{
cout << "frustrated.\n";
}
else if (mood > 5)
{
}
else
{
}
cout << "okay.\n";
cout << "happy.\n";
PassTime();
}
Eat() reduces a critter’s hunger level by the amount passed to the parameter food. If no value is passed, food gets the default argument value of 4. The critter’s hunger level is kept in check and is not allowed to go below zero. Finally, PassTime() is called to simulate the passage of time.
void Critter::Eat(int food)
{
cout << "Brruppp.\n"; m_Hunger -= food;
280 Chapter 8 n Classes: Critter Caretaker
if (m_Hunger < 0)
{
m_Hunger = 0;
}
PassTime();
}
Play() reduces a critter’s boredom level by the amount passed to the parameter fun. If no value is passed, fun gets the default argument value of 4. The critter’s boredom level is kept in check and is not allowed to go below zero. Finally, PassTime() is called to simulate the passage of time.
void Critter::Play(int fun)
{
cout << "Wheee!\n";
m_Boredom -= fun; if (m_Boredom < 0)
{
m_Boredom = 0;
}
PassTime();
}