《C++游戏编程入门》第8章 类:Critter Caretaker
- 8.1 定义新类型
- 08.simple_critter.cpp
- 8.2 使用构造函数
- 08.constructor_critter.cpp
- 8.3 设置成员访问级别
- 08.private_critter.cpp
- 8.4 使用静态数据成员与静态成员函数
- 08.static_critter.cpp
- 8.5 Critter Caretaker游戏
- 08.critter_caretaker.cpp
8.1 定义新类型
08.simple_critter.cpp
#include <iostream>
using namespace std;class Critter // 类定义,定义新类型Critter
{
public:int m_Hunger; // 声明数据成员void Greet(); // 声明成员函数
};void Critter::Greet() // 成员函数定义
{cout << "Hi. I'm a critter. My hunger level is " << m_Hunger << ".\n";
}int main()
{Critter crit1; // 对象实例化Critter crit2;crit1.m_Hunger = 9; // 对象操作cout << "crit1's hunger level is " << crit1.m_Hunger << ".\n";crit2.m_Hunger = 3;cout << "crit2's hunger level is " << crit2.m_Hunger << ".\n\n";crit1.Greet(); // 成员函数调用crit2.Greet();return 0;
}
8.2 使用构造函数
构造函数,实例化新对象时自动调用,通常初始化工作,给数据成员赋值。
08.constructor_critter.cpp
#include <iostream>
using namespace std;class Critter
{
public:int m_Hunger;Critter(int hunger = 0); // 声明构造函数,提供默认参数void Greet();
};Critter::Critter(int hunger) // 定义构造函数
{cout << "A new critter has been born!" << endl;m_Hunger = hunger;
}void Critter::Greet()
{cout << "Hi. I'm a critter. My hunger level is " << m_Hunger << ".\n\n";
}int main()
{Critter crit(7);crit.Greet();return 0;
}
8.3 设置成员访问级别
避免直接修改或访问对象的数据成员,让成员函数维护。
08.private_critter.cpp
#include <iostream>
using namespace std;class Critter
{
public: // begin public sectionCritter(int hunger = 0);int GetHunger() const;void SetHunger(int hunger);private: // begin private sectionint m_Hunger;
};Critter::Critter(int hunger): m_Hunger(hunger)
{cout << "A new critter has been born!" << endl;
}int Critter::GetHunger() const // const不能修改非静态数据成员值,不能调用非常量成员函数
{return m_Hunger;
}void Critter::SetHunger(int hunger)
{if (hunger < 0){cout << "You can't set a critter's hunger to a negative number.\n\n";}else{m_Hunger = hunger;}
}int main()
{Critter crit(5);// cout << crit.m_Hunger; -- illegal, m_Hunger is private!cout << "Calling GetHunger(): " << crit.GetHunger() << "\n\n";cout << "Calling SetHunger() with -1.\n";crit.SetHunger(-1);cout << "Calling SetHunger() with 9.\n";crit.SetHunger(9);cout << "Calling GetHunger(): " << crit.GetHunger() << "\n\n";return 0;
}
8.4 使用静态数据成员与静态成员函数
08.static_critter.cpp
#include <iostream>
using namespace std;class Critter
{
public:static int s_Total; // 静态成员变量声明// total number of Critter objects in existenceCritter(int hunger = 0);static int GetTotal(); // 静态成员函数声明private:int m_Hunger;
};int Critter::s_Total = 0; // 静态成员变量定义并初始化Critter::Critter(int hunger) : m_Hunger(hunger)
{cout << "A critter has been born!" << endl;++s_Total;
}int Critter::GetTotal() // 静态成员函数定义,不能访问非静态数据成员
{return s_Total;
}int main()
{cout << "The total number of critters is: ";cout << Critter::s_Total << "\n\n"; // 访问静态常用变量Critter crit1, crit2, crit3;cout << "\nThe total number of critters is: ";cout << Critter::GetTotal() << "\n"; // 调用静态成员函数return 0;
}
8.5 Critter Caretaker游戏
08.critter_caretaker.cpp
#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);
};Critter::Critter(int hunger, int boredom): m_Hunger(hunger), m_Boredom(boredom)
{
}inline int Critter::GetMood() const
{return (m_Hunger + m_Boredom);
}void Critter::PassTime(int time)
{m_Hunger += time;m_Boredom += 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)cout << "okay.\n";elsecout << "happy.\n";PassTime();
}void Critter::Eat(int food)
{cout << "Brruppp.\n";m_Hunger -= food;if (m_Hunger < 0)m_Hunger = 0;PassTime();
}void Critter::Play(int fun)
{cout << "Wheee!\n";m_Boredom -= fun;if (m_Boredom < 0)m_Boredom = 0;PassTime();
}int main()
{Critter crit;crit.Talk();int choice;do{cout << "\nCritter Caretaker\n\n";cout << "0 - Quit\n";cout << "1 - Listen to your critter\n";cout << "2 - Feed your critter\n";cout << "3 - Play with your critter\n\n";cout << "Choice: ";cin >> choice;switch (choice){case 0:cout << "Good-bye.\n";break;case 1:crit.Talk();break;case 2:crit.Eat();break;case 3:crit.Play();break;default:cout << "\nSorry, but " << choice << " isn't a valid choice.\n";}} while (choice != 0);return 0;
}