第10章 对象和类

欢迎来到面向对象编程的世界!学习如何将现实事物抽象成代码,像搭建乐高一样构建复杂程序。

环节 1:类是“蓝图”,对象是“实体”

想象一下,**类 (Class)** 是一张制造汽车的**“设计蓝图”**,它规定了汽车有什么属性(颜色、品牌)和功能(跑、停)。而**对象 (Object)** 则是根据这张蓝图制造出来的**一辆辆具体的汽车**。

// "汽车"这张蓝图 (类) class Car { private: // 内部零件, 外界不能直接碰 std::string brand; std::string color; public: // 公开的功能按钮 Car(const std::string& b, const std::string& c) { brand = b; color = c; } void show() const { std::cout << "一辆 " << color << " 的 " << brand << " 汽车" << std::endl; } }; // main函数中根据蓝图造车 Car myCar("奔驰", "红色"); // 创建一个对象 myCar.show(); // 调用对象的功能

汽车制造工厂 🏭

使用 `Car` 蓝图,输入品牌和颜色,制造你自己的汽车对象!

车库

空空如也...

环节 2:数据隐藏 ——“银行保险箱”

类里面的数据(成员变量)默认是 `private`(私有的),就像**保险箱里的钱**,不能直接拿。你必须通过 `public`(公有的)方法,比如去**“银行柜台”**办理业务,才能安全地存取。

我的银行账户 🏦

🔒

账户余额 (私有数据)

$1500.50

你无法直接修改这个数值

class BankAccount { private: double balance; // 🔒 保险箱里的钱 public: BankAccount(double b) { balance = b; } // 🔑 存款柜台 void deposit(double cash) { if (cash > 0) balance += cash; } // 🔑 取款柜台 bool withdraw(double cash) { if (cash > 0 && balance >= cash) { balance -= cash; return true; } return false; } };

环节 3:对象的“出生”与“消亡”

**构造函数 (Constructor)** 是对象**出生时**自动运行的函数,负责初始化。**析构函数 (Destructor)** 是对象**消亡时**自动运行的函数,负责清理工作。

#include <iostream> class LifeCycle { public: LifeCycle() { // 构造函数 std::cout << "👶 对象出生了!" << std::endl; } ~LifeCycle() { // 析构函数 std::cout << "👻 对象消亡了..." << std::endl; } }; void some_function() { std::cout << "--- 进入函数 ---" << std::endl; LifeCycle obj; // 在这里创建对象 std::cout << "--- 准备离开函数 ---" << std::endl; } // obj 在这里超出作用域,自动消亡

生命周期观察器 🔬

点击按钮,模拟调用一次 `some_function()`,观察对象何时出生,何时消亡。

> 日志输出区...

环节 4:this 指针 —— 我是谁?

在成员函数内部,`this` 是一个特殊的指针,它**永远指向调用该函数的那个对象**。这解决了“方法被调用时,它如何知道是哪个对象在调用它”的问题。

对象身份识别器 🆔

下面有两个不同的 `Player` 对象。点击它们各自的按钮,让它们自己说出“我是谁”。

玩家 P1

Kate

玩家 P2

Mike

> 函数内部日志:

class Player { private: std::string name; public: Player(std::string n) : name(n) {} void who_am_i() { // this 指向调用本函数的对象 std::cout << "我是 " << this->name << std::endl; } }; int main() { Player p1("Kate"); Player p2("Mike"); p1.who_am_i(); // 此处 this 指向 p1 p2.who_am_i(); // 此处 this 指向 p2 }

终点站:编程挑战

练习 1:基本账户类

任务:

定义一个 `BankAccount` 类,包含姓名、账号和存款。提供构造函数、`deposit`(存款)、`withdraw`(取款)和 `show` 方法。

预期知识点:

类声明、私有数据、公有方法、构造函数、const成员函数。

点击查看参考答案

File 1: bank.h (头文件)

#include <string> class BankAccount { private: std::string client_name; std::string acct_num; double balance; public: BankAccount(const std::string& n, const std::string& a, double b = 0.0); void deposit(double cash); bool withdraw(double cash); void show() const; };

File 2: bank.cpp (实现文件)

#include "bank.h" #include <iostream> BankAccount::BankAccount(const std::string& n, const std::string& a, double b) { client_name = n; acct_num = a; balance = b; } void BankAccount::deposit(double cash) { if (cash > 0) balance += cash; } bool BankAccount::withdraw(double cash) { if (cash > 0 && balance >= cash) { balance -= cash; return true; } else { return false; } } void BankAccount::show() const { std::cout << "客户: " << client_name << std::endl << "账号: " << acct_num << std::endl << "余额: $" << balance << std::endl; }
练习 2:使用账户类

任务:

编写一个 `main` 函数来使用 `BankAccount` 类。创建一个对象,并调用其方法模拟存钱和取钱操作,最后显示账户信息。

预期知识点:

对象创建、方法调用、构造函数使用。

点击查看参考答案

File 3: use_bank.cpp (主程序)

#include "bank.h" #include <iostream> int main() { BankAccount my_account("张三", "1001-2002", 1500.50); my_account.show(); my_account.deposit(500.00); std::cout << "\n--- 存入500后 ---" << std::endl; my_account.show(); my_account.withdraw(200.00); std::cout << "\n--- 取出200后 ---" << std::endl; my_account.show(); return 0; }