Logo
Section 1 / 13

类、封装与访问控制

学习如何使用 struct 和 class 定义自定义类型,并应用访问修饰符来封装数据,以确保安全性和清晰性。

导入

类、封装与访问控制

学习如何使用 struct 和 class 定义自定义类型,并应用访问修饰符来封装数据,以确保安全性和清晰性。

1#include <iostream> 2#include <string> 3 4struct BankAccount { 5 std::string owner; 6 double balance; 7}; // 分号是必需的! 8 9int main() { 10 // 1. 创建结构体的实例(对象) 11 BankAccount account; 12 13 // 2. 我们可以直接访问成员,因为它们默认是公共的 14 account.owner = "Alice"; 15 account.balance = 100.0; 16 17 std::cout << account.owner << " 拥有 $" << account.balance << "\n"; 18 19 // 问题:缺乏安全性。我们很容易破坏逻辑 20 account.balance = -5000.0; // 无效状态! 21 22 std::cout << "损坏的余额: " << account.balance << "\n"; 23 24 return 0; 25}
1#include <iostream> 2#include <string> 3 4class BankAccount { 5 std::string owner; // 默认私有 6 double balance; // 默认私有 7}; 8 9int main() { 10 BankAccount account; 11 12 account.balance = 100.0; // 编译错误:'balance' 是私有的 13 return 0; 14}
1#include <iostream> 2#include <string> 3 4class BankAccount { 5private: 6 // 数据被隐藏。只有这个类可以访问它 7 std::string owner; 8 double balance = 0.0; // 类内初始化(良好实践) 9 10public: 11 // 初始化函数(我们将在下一课用构造函数替换它) 12 void init(std::string name, double startAmount) { 13 owner = name; 14 if (startAmount > 0) balance = startAmount; 15 } 16 17 // 公共接口:受控访问 18 void deposit(double amount) { 19 if (amount > 0) { 20 balance += amount; 21 std::cout << "存入 $" << amount << "\n"; 22 } 23 } 24 25 // 获取器:只读访问余额 26 double getBalance() { 27 return balance; 28 } 29}; 30 31int main() { 32 BankAccount account; 33 account.init("Alice", 100.0); // 使用公共函数进行初始化 34 35 account.deposit(50.0); // 使用公共函数修改 36 // account.balance = -500; // 错误:仍然为私有! 37 38 std::cout << "当前余额: $" << account.getBalance() << "\n"; 39 40 return 0; 41}
1#include <iostream> 2#include <string> 3 4class BankAccount { 5private: 6 std::string owner; 7 double balance = 0.0; 8 9public: 10 void init(std::string name, double startAmount) { 11 owner = name; 12 if (startAmount > 0) balance = startAmount; 13 } 14 15 void deposit(double amount) { 16 if (amount > 0) { 17 balance += amount; 18 } 19 } 20 21 // 逻辑:强制执行“余额不能为负”的规则 22 void withdraw(double amount) { 23 if (amount > balance) { 24 std::cout << "错误:取款 $" << amount << " 时资金不足\n"; 25 } else if (amount <= 0) { 26 std::cout << "错误:无效的取款金额。\n"; 27 } else { 28 balance -= amount; 29 std::cout << "成功:取款 $" << amount << "\n"; 30 } 31 } 32 33 double getBalance() { return balance; } 34}; 35 36int main() { 37 BankAccount account; 38 account.init("Alice", 100.0); 39 40 account.withdraw(40.0); // 正常:余额变为 60.0 41 account.withdraw(500.0); // 被阻止:类自身保护自己 42 43 std::cout << "最终余额: $" << account.getBalance() << "\n"; 44 return 0; 45}
1#include <iostream> 2#include <string> 3#include <vector> 4 5class BankAccount { 6private: 7 std::string owner; 8 double balance = 0.0; 9 std::vector<std::string> history; // 我们也隐藏了历史记录日志 10 11 // 内部辅助函数:用户不需要直接调用 12 void logTransaction(std::string msg) { 13 history.push_back(msg); 14 std::cout << "[LOG]: " << msg << "\n"; 15 } 16 17public: 18 void init(std::string name, double startAmount) { 19 owner = name; 20 balance = startAmount; 21 logTransaction("为 " + name + " 创建账户"); 22 } 23 24 void deposit(double amount) { 25 if (amount > 0) { 26 balance += amount; 27 // 在这里使用私有辅助函数 28 logTransaction("存入 " + std::to_string(amount)); 29 } 30 } 31 32 void withdraw(double amount) { 33 if (amount <= balance && amount > 0) { 34 balance -= amount; 35 // 在这里使用私有辅助函数 36 logTransaction("取款 " + std::to_string(amount)); 37 } 38 } 39 40 double getBalance() { return balance; } 41}; 42 43int main() { 44 BankAccount account; 45 account.init("Alice", 100.0); // 自动记录创建日志 46 account.deposit(50.0); // 自动记录存款日志 47 48 // account.logTransaction("伪造日志"); // 错误:私有函数! 49 50 return 0; 51}

C++ 入门第一步

构造函数和析构函数