< Previous | Contents | Next >

The Critter Class

The Critter class is the blueprint for the object that represents the players critter. The class isnt complicated, and most of it should look familiar, but its long enough that it makes sense to attack it in pieces.


The Class Definition

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 critters hunger level while m_Boredom is a private data member that represents its boredom level. Ill go through each member function in its own section.


The Class Constructor

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)

{}


The GetMood() Member Function

Next, I define GetMood().

inline int Critter::GetMood() const

{

return (m_Hunger + m_Boredom);

}

The return value of this inlined member function represents a critters mood. As the sum of a critters hunger and boredom levels, a critters 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 wont result in any changes to data members.


The PassTime() Member Function

PassTime() is a private member function that increases a critters hunger and boredom levels. Its 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

The Talk() member function announces the critters 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 critters 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();

}


The Eat() Member Function

Eat() reduces a critters hunger level by the amount passed to the parameter food. If no value is passed, food gets the default argument value of 4. The critters 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();

}


The Play() Member Function

Play() reduces a critters boredom level by the amount passed to the parameter fun. If no value is passed, fun gets the default argument value of 4. The critters 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();

}