侯捷 C++ 课程学习笔记:C++ 面向对象开发的艺术
在侯捷老师的 C++ 系列课程中,《C++ 面向对象开发》这门课程让我对面向对象编程有了更深入的理解。面向对象编程(OOP)是现代软件开发中最重要的编程范式之一,而 C++ 作为支持 OOP 的语言,提供了强大的工具和特性。侯捷老师通过系统的讲解和实战案例,帮助我掌握了如何在 C++ 中高效地使用面向对象技术。以下是我对这门课程的学习笔记和心得体会。
一、课程核心内容:C++ 面向对象开发的关键特性

#include <iostream>
#include <stdexcept>
class BankAccount {
private:
double balance;
public:
BankAccount(double initialBalance) : balance(initialBalance) {
if (initialBalance < 0) {
throw std::invalid_argument("Initial balance cannot be negative.");
}
}
void deposit(double amount) {
if (amount <= 0) {
throw std::invalid_argument("Deposit amount must be positive.");
}
balance += amount;
}
void withdraw(double amount) {
if (amount <= 0) {
throw std::invalid_argument("Withdrawal amount must be positive.");
}
if (amount > balance) {
throw std::runtime_error("Insufficient funds.");
}
balance -= amount;
}
double getBalance() const {
return balance;
}
};
侯捷老师强调,封装的核心在于隐藏实现细节,只通过接口暴露必要的功能。这种方式不仅提高了代码的安全性,还使得类的内部实现可以灵活修改,而不会影响外部使用。
(二)继承:构建类的层次结构
继承是面向对象编程中用于实现代码复用的重要机制。侯捷老师通过一个简单的图形类库展示了继承的使用。以下是一个 Shape 基类和两个派生类 Circle 和 Rectangle:
#include <iostream>
#include <cmath>
class Shape {
public:
virtual double area() const = 0; // 纯虚函数
virtual ~Shape() = default; // 虚析构函数
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return M_PI * radius * radius;
}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override {
return width * height;
}
};
侯捷老师指出,继承不仅实现了代码的复用,还通过多态机制提供了强大的扩展能力。通过虚函数和纯虚函数,基类可以定义通用接口,而派生类则可以提供具体的实现。
(三)多态:实现动态绑定
多态是面向对象编程中最强大的特性之一,它允许通过基类指针或引用调用派生类的成员函数。侯捷老师通过以下代码展示了多态的实际应用:
#include <iostream>
#include <vector>
#include <memory>
void printArea(const Shape& shape) {
std::cout << "Area: " << shape.area() << std::endl;
}
int main() {
std::vector<std::shared_ptr<Shape>> shapes;
shapes.push_back(std::make_shared<Circle>(5.0));
shapes.push_back(std::make_shared<Rectangle>(4.0, 6.0));
for (const auto& shape : shapes) {
printArea(*shape);
}
return 0;
}
侯捷老师强调,多态的核心在于动态绑定,即在运行时根据对象的实际类型调用相应的函数。这种方式不仅提高了代码的灵活性,还使得系统可以轻松扩展新的类,而无需修改现有代码。
二、学习心得:面向对象编程的实践与思考
通过学习侯捷老师的《C++ 面向对象开发》课程,我对面向对象编程有了更深刻的理解。侯捷老师不仅讲解了面向对象的核心特性,还通过大量实战案例展示了如何在实际开发中应用这些特性。
(一)封装的重要性
封装是面向对象编程的基础,它通过隐藏实现细节,只暴露必要的接口,提高了代码的安全性和可维护性。侯捷老师通过实际案例展示了如何通过类封装数据和行为,让我深刻理解了封装的重要性。
(二)继承与多态的强大功能
继承和多态是面向对象编程的核心特性,它们不仅实现了代码的复用,还提供了强大的扩展能力。侯捷老师通过图形类库的案例,展示了如何通过继承和多态构建灵活的类层次结构。这种方式不仅提高了代码的可扩展性,还使得系统可以轻松应对需求的变化。
(三)面向对象的设计原则
侯捷老师在课程中还介绍了面向对象设计的一些基本原则,如单一职责原则、开闭原则、里氏替换原则等。这些原则为面向对象编程提供了指导,帮助开发者设计出更合理、更灵活的系统。
三、实际应用案例:面向对象开发在项目中的实践
在学习侯捷老师的课程后,我将所学知识应用到了实际项目中。我们团队负责开发一个简单的电子商务系统,需要管理用户、商品和订单。通过侯捷老师对面向对象开发的讲解,我决定使用面向对象技术来设计和实现这个系统。
(一)项目背景
电子商务系统需要管理用户、商品和订单。每个用户可以浏览商品、下单购买,并查看订单状态。系统需要支持多种商品类型和订单状态,同时还需要提供灵活的扩展能力。
(二)面向对象设计
我们通过面向对象技术设计了系统的类结构。以下是主要的类及其关系:
用户类(User)
#include <string>
#include <vector>
#include <memory>
class Order; // 前置声明
class User {
private:
std::string name;
std::vector<std::shared_ptr<Order>> orders;
public:
User(const std::string& name) : name(name) {}
void addOrder(const std::shared_ptr<Order>& order) {
orders.push_back(order);
}
void printOrders() const {
std::cout << "Orders for " << name << ":" << std::endl;
for (const auto& order : orders) {
order->print();
}
}
};
商品类(Product)
#include <string>
class Product {
private:
std::string name;
double price;
public:
Product(const std::string& name, double price) : name(name), price(price) {}
std::string getName() const {
return name;
}
double getPrice() const {
return price;
}
};
订单类(Order)
#include <iostream>
#include <vector>
#include <memory>
#include <string>
class Order {
private:
std::string status;
std::vector<std::shared_ptr<Product>> products;
public:
Order(const std::string& status) : status(status) {}
void addProduct(const std::shared_ptr<Product>& product) {
products.push_back(product);
}
void print() const {
std::cout << "Order Status: " << status << std::endl;
for (const auto& product : products) {
std::cout << "Product: " << product->getName() << ", Price: " << product->getPrice() << std::endl;
}
}
};
(三)系统实现
通过面向对象设计,我们能够灵活地管理用户、商品和订单。以下是系统的主函数:
#include <iostream>
#include <memory>
#include <vector>
int main() {
// 创建商品
std::shared_ptr<Product> book = std::make_shared<Product>("Book", 10.99);
std::shared_ptr<Product> laptop = std::make_shared<Product>("Laptop", 999.99);
// 创建订单
std::shared_ptr<Order> order1 = std::make_shared<Order>("Pending");
order1->addProduct(book);
order1->addProduct(laptop);
// 创建用户
User user("Alice");
user.addOrder(order1);
// 打印订单
user.printOrders();
return 0;
}
通过面向对象设计,我们能够清晰地管理用户、商品和订单的关系,并且系统具有很强的扩展能力。例如,我们可以轻松添加新的商品类型或订单状态,而无需修改现有代码。
四、总结与展望
通过学习侯捷老师的《C++ 面向对象开发》课程,我对面向对象编程有了更深入的理解