《C++游戏编程入门》第8章 类:Critter Caretaker

《C++游戏编程入门》第8章 类:Critter Caretaker

8.1 定义新类型

08.simple_critter.cpp
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
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
cpp 复制代码
#include <iostream>
using namespace std;

class Critter
{
public: // begin public section
    Critter(int hunger = 0);
    int GetHunger() const;
    void SetHunger(int hunger);

private: // begin private section
    int 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
cpp 复制代码
#include <iostream>
using namespace std;

class Critter
{
public:
    static int s_Total; // 静态成员变量声明
                        // total number of Critter objects in existence

    Critter(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
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";
    else
        cout << "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;
}
相关推荐
小俊俊的博客27 分钟前
海康RGBD相机使用C++和Opencv采集图像记录
c++·opencv·海康·rgbd相机
_WndProc42 分钟前
C++ 日志输出
开发语言·c++·算法
薄荷故人_44 分钟前
从零开始的C++之旅——红黑树及其实现
数据结构·c++
m0_7482400244 分钟前
Chromium 中chrome.webRequest扩展接口定义c++
网络·c++·chrome
windwind200044 分钟前
游戏关卡设计方法的杂感
游戏·关卡设计
qq_433554541 小时前
C++ 面向对象编程:+号运算符重载,左移运算符重载
开发语言·c++
努力学习编程的伍大侠1 小时前
基础排序算法
数据结构·c++·算法
yuyanjingtao2 小时前
CCF-GESP 等级考试 2023年9月认证C++四级真题解析
c++·青少年编程·gesp·csp-j/s·编程等级考试
闻缺陷则喜何志丹2 小时前
【C++动态规划 图论】3243. 新增道路查询后的最短距离 I|1567
c++·算法·动态规划·力扣·图论·最短路·路径
charlie1145141912 小时前
C++ STL CookBook
开发语言·c++·stl·c++20