Giao diện tối
15.x — Chapter 15 summary and quiz
🎯 Mục Tiêu Bài Học
- Nắm vững bản chất lý thuyết và cơ chế hoạt động của Chapter 15 summary and quiz trong tiêu chuẩn ISO C++.
- Hiểu rõ cách máy tính tổ chức bộ nhớ (RAM, CPU registers) khi thực thi mã nguồn liên quan.
- Nhận biết các cạm bẫy lỗi kinh điển (Undefined Behavior, Memory Leak, Bug logic) và cách phòng tránh.
- Áp dụng các quy tắc thực hành tốt nhất (Best Practices) chuẩn công nghiệp và cập nhật C++20/C++23 hiện đại.
15.x — Chapter 15 summary and quiz
Chapter Review
Inside every (non-static) member hàm, the keyword this is a const con trỏ that holds the address of the current implicit object. We can have các hàm return *this by tham chiếu in order to enable method chaining, where several member các hàm can be called on the same object in a single biểu thức.
Prefer to put your class definitions in a header file with the same name as the class. Trivial member các hàm (such as access các hàm, các hàm tạo (constructors) with empty bodies, etc…) can be defined inside the class definition.
Prefer to define non-trivial member các hàm in a source file with the same name as the class.
A type that is defined inside a class type is called a nested type (or member type). Type aliases can also be nested.
Member các hàm defined inside a class template definition can use the template tham số of the class template itself. Member các hàm defined outside the class template definition must resupply a template tham số declaration, and should be defined (in the same file) just below the class template definition.
Static member các biến are static duration members that are shared by all đối tượng of the class. Static members exist even if no đối tượng of the class have been instantiated. Prefer to access them using the class name, the phạm vi truy cập (scope) resolution toán tử, and the members name.
Making static members inline allows them to be được khởi tạo inside the class definition.
Static member các hàm are member các hàm that can be called with no object. They do not have a *this con trỏ, and cannot access non-static data members.
Inside the body of a class, a friend declaration (using the friend keyword) can be used to tell the trình biên dịch that some other class or hàm is now a friend. A friend is a class or hàm (member or non-member) that has been granted full access to the private and protected members of another class. A friend hàm is a hàm (member or non-member) that can access the private and protected members of a class as though it were a member of that class. A friend class is a class that can access the private and protected members of another class.
Hệ thống Câu hỏi & Quiz Tự Luyện
Câu hỏi #1
Let’s create a random monster generator. This one should be fun.
a) First, let’s create an scoped enumeration of monster types named MonsterType. Include the following monster types: Dragon, Goblin, Ogre, Orc, Skeleton, Troll, Vampire, and Zombie. Add an additional maxMonsterTypes enumerator so we can count how many enumerators there are.
Xem lời giải (Solution)
cpp
enum class MonsterType
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};b) Now, let’s create our Monster class. Our Monster will have 4 attributes (member các biến): a type (MonsterType), a name (std::string), a roar (std::string) and the number of hit points (int).
Xem lời giải (Solution)
cpp
#include <string>
enum class MonsterType
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
class Monster
{
private:
MonsterType m_type{};
std::string m_name{"???"};
std::string m_roar{"???"};
int m_hitPoints{};
};c) enum class MonsterType is specific to Monster, so make MonsterType a nested unscoped enum inside Monster and rename it to Type.
Xem lời giải (Solution)
cpp
#include <string>
class Monster
{
public:
enum Type
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
private:
Type m_type{};
std::string m_name{"???"};
std::string m_roar{"???"};
int m_hitPoints{};
};d) Create a hàm tạo (constructor) that allows you to khởi tạo all of the member các biến.
The following program should compile:
cpp
int main()
{
Monster skeleton{ Monster::skeleton, "Bones", "*rattle*", 4 };
return 0;
}Xem lời giải (Solution)
cpp
#include <string>
#include <string_view>
class Monster
{
public:
enum Type
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
private:
Type m_type{};
std::string m_name{"???"};
std::string m_roar{"???"};
int m_hitPoints{};
public:
Monster(Type type, std::string_view name, std::string_view roar, int hitPoints)
: m_type{ type }, m_name{ name }, m_roar{ roar }, m_hitPoints{ hitPoints }
{
}
};
int main()
{
Monster skeleton{ Monster::skeleton, "Bones", "*rattle*", 4 };
return 0;
}e) Now we want to be able to print our monster so we can validate it’s correct. Write two các hàm: One called getTypeString() that returns the monster’s type as a string, and one called print() that matches the output in the sample program below.
The following program should compile:
cpp
int main()
{
Monster skeleton{ Monster::skeleton, "Bones", "*rattle*", 4 };
skeleton.print();
Monster vampire{ Monster::vampire, "Nibblez", "*hiss*", 0 };
vampire.print();
return 0;
}and print:
cpp
Bones the skeleton has 4 hit points and says *rattle*.
Nibblez the vampire is dead.Xem lời giải (Solution)
cpp
#include <iostream>
#include <string>
#include <string_view>
class Monster
{
public:
enum Type
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
private:
Type m_type{};
std::string m_name{"???"};
std::string m_roar{"???"};
int m_hitPoints{};
public:
Monster(Type type, std::string_view name, std::string_view roar, int hitPoints)
: m_type{ type }, m_name{ name }, m_roar{ roar }, m_hitPoints{ hitPoints }
{
}
constexpr std::string_view getTypeString() const
{
switch (m_type)
{
case dragon: return "dragon";
case goblin: return "goblin";
case ogre: return "ogre";
case orc: return "orc";
case skeleton: return "skeleton";
case troll: return "troll";
case vampire: return "vampire";
case zombie: return "zombie";
default: return "???";
}
}
void print() const
{
std::cout << m_name << " the " << getTypeString();
if (m_hitPoints <= 0)
std::cout << " is dead.\n";
else
std::cout << " has " << m_hitPoints << " hit points and says " << m_roar << ".\n";
}
};
int main()
{
Monster skeleton{ Monster::skeleton, "Bones", "*rattle*", 4 };
skeleton.print();
Monster vampire{ Monster::vampire, "Nibblez", "*hiss*", 0 };
vampire.print();
return 0;
}f) Now we can create a random monster generator. Let’s consider how our MonsterGenerator will work. Ideally, we’ll ask it to give us a Monster, and it will create a random one for us. Because MonsterGenerator doesn’t have any state, this is a good candidate for a namespace.
Create a MonsterGenerator namespace. Create hàm within named generate(). This should return a Monster. For now, make it return Monster{ Monster::skeleton, "Bones", "*rattle*", 4};
The following program should compile:
cpp
int main()
{
Monster m{ MonsterGenerator::generate() };
m.print();
return 0;
}and print:
cpp
Bones the skeleton has 4 hit points and says *rattle*Xem lời giải (Solution)
cpp
#include <iostream>
#include <string>
#include <string_view>
class Monster
{
public:
enum Type
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
private:
Type m_type{};
std::string m_name{};
std::string m_roar{};
int m_hitPoints{};
public:
Monster(Type type, std::string_view name, const std::string& roar, int hitPoints)
: m_type{ type }, m_name{ name }, m_roar{ roar }, m_hitPoints{ hitPoints }
{
}
constexpr std::string_view getTypeString() const
{
switch (m_type)
{
case Type::dragon: return "dragon";
case Type::goblin: return "goblin";
case Type::ogre: return "ogre";
case Type::orc: return "orc";
case Type::skeleton: return "skeleton";
case Type::troll: return "troll";
case Type::vampire: return "vampire";
case Type::zombie: return "zombie";
default: return "???";
}
}
void print() const
{
if (m_hitPoints <= 0)
std::cout << m_name << " is dead.\n";
else
std::cout << m_name << " the " << getTypeString() << " has " << m_hitPoints << " hit points and says " << m_roar << ".\n";
}
};
namespace MonsterGenerator
{
Monster generate()
{
return Monster{ Monster::skeleton, "Bones", "*rattle*", 4 };
}
};
int main()
{
Monster m{ MonsterGenerator::generate() };
m.print();
return 0;
}g) Add two more các hàm to the MonsterGenerator namespace. getName(int) will take a number between 0 and 5 (inclusive) and return a name of your choice. getRoar(int) will also take a number between 0 and 5 (inclusive) and return a roar of your choice. Also update your generate() hàm to call getName(0) and getRoar(0).
The following program should compile:
cpp
int main()
{
Monster m{ MonsterGenerator::generate() };
m.print();
return 0;
}and print:
cpp
Blarg the skeleton has 4 hit points and says *ROAR*Your name and sound will vary based on what you chose.
Xem lời giải (Solution)
cpp
#include <iostream>
#include <string>
#include <string_view>
class Monster
{
public:
enum Type
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
private:
Type m_type{};
std::string m_name{"???"};
std::string m_roar{"???"};
int m_hitPoints{};
public:
Monster(Type type, std::string_view name, std::string_view roar, int hitPoints)
: m_type{ type }, m_name{ name }, m_roar{ roar }, m_hitPoints{ hitPoints }
{
}
constexpr std::string_view getTypeString() const
{
switch (m_type)
{
case dragon: return "dragon";
case goblin: return "goblin";
case ogre: return "ogre";
case orc: return "orc";
case skeleton: return "skeleton";
case troll: return "troll";
case vampire: return "vampire";
case zombie: return "zombie";
default: return "???";
}
}
void print() const
{
std::cout << m_name << " the " << getTypeString();
if (m_hitPoints <= 0)
std::cout << " is dead.\n";
else
std::cout << " has " << m_hitPoints << " hit points and says " << m_roar << ".\n";
}
};
namespace MonsterGenerator
{
std::string_view getName(int n)
{
switch (n)
{
case 0: return "Blarg";
case 1: return "Moog";
case 2: return "Pksh";
case 3: return "Tyrn";
case 4: return "Mort";
case 5: return "Hans";
default: return "???";
}
}
std::string_view getRoar(int n)
{
switch (n)
{
case 0: return "*ROAR*";
case 1: return "*peep*";
case 2: return "*squeal*";
case 3: return "*whine*";
case 4: return "*growl*";
case 5: return "*burp*";
default: return "???";
}
}
Monster generate()
{
return Monster{ Monster::skeleton, getName(0), getRoar(0), 4 };
}
};
int main()
{
Monster m{ MonsterGenerator::generate() };
m.print();
return 0;
}h) Now we’ll randomize our generated monster. Grab the “Random.h” code from 8.15 -- Global random numbers (Random.h) and save it as Random.h. Then use Random::get() to generate a random monster type, random name, random roar, and random hit points (between 1 and 100).
The following program should compile:
cpp
#include "Random.h"
int main()
{
Monster m{ MonsterGenerator::generate() };
m.print();
return 0;
}and print something like this:
cpp
Mort the zombie has 61 hit points and says *growl*Xem lời giải (Solution)
cpp
#include "Random.h"
#include <iostream>
#include <string>
#include <string_view>
class Monster
{
public:
enum Type
{
dragon,
goblin,
ogre,
orc,
skeleton,
troll,
vampire,
zombie,
maxMonsterTypes,
};
private:
Type m_type{};
std::string m_name{"???"};
std::string m_roar{"???"};
int m_hitPoints{};
public:
Monster(Type type, std::string_view name, std::string_view roar, int hitPoints)
: m_type{ type }, m_name{ name }, m_roar{ roar }, m_hitPoints{ hitPoints }
{
}
constexpr std::string_view getTypeString() const
{
switch (m_type)
{
case dragon: return "dragon";
case goblin: return "goblin";
case ogre: return "ogre";
case orc: return "orc";
case skeleton: return "skeleton";
case troll: return "troll";
case vampire: return "vampire";
case zombie: return "zombie";
default: return "???";
}
}
void print() const
{
std::cout << m_name << " the " << getTypeString();
if (m_hitPoints <= 0)
std::cout << " is dead.\n";
else
std::cout << " has " << m_hitPoints << " hit points and says " << m_roar << ".\n";
}
};
namespace MonsterGenerator
{
std::string_view getName(int n)
{
switch (n)
{
case 0: return "Blarg";
case 1: return "Moog";
case 2: return "Pksh";
case 3: return "Tyrn";
case 4: return "Mort";
case 5: return "Hans";
default: return "???";
}
}
std::string_view getRoar(int n)
{
switch (n)
{
case 0: return "*ROAR*";
case 1: return "*peep*";
case 2: return "*squeal*";
case 3: return "*whine*";
case 4: return "*growl*";
case 5: return "*burp*";
default: return "???";
}
}
Monster generate()
{
return Monster{
static_cast<Monster::Type>(Random::get(0, Monster::maxMonsterTypes-1)),
getName(Random::get(0,5)),
getRoar(Random::get(0,5)),
Random::get(1, 100)
};
}
};
int main()
{
Monster m{ MonsterGenerator::generate() };
m.print();
return 0;
}💡 Tóm Tắt Quy Tắc Vàng (Core Guidelines)
- Luôn hiểu rõ vòng đời và quyền sở hữu (Ownership) của dữ liệu trước khi viết code.
- Ưu tiên khởi tạo đồng nhất với dấu ngoặc nhọn
{}để ngăn chặn lỗi thu hẹp kiểu dữ liệu (Narrowing conversions). - Bật toàn bộ cờ cảnh báo
-Wall -Wextra -Wpedanticđể compiler giúp bạn phát hiện bug từ sớm!