前言

这里只是简单列出C++中面向对象编程的知识点


一、概述

面向对象编程(OOP)是C++的核心范式,通过封装继承多态三大特性构建程序结构。C++在C语言基础上添加了类等面向对象特性,既支持过程化编程也支持面向对象编程,具有高度的灵活性和性能。


二、核心特性详解

2.1 类与对象(封装)

2.1.1 基本类定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Person {
private: // 访问修饰符
string name; // 成员变量
int age;

public:
// 构造函数
Person(string n, int a) : name(n), age(a) {}

// 成员函数
void introduce() {
cout << "I'm " << name << ", " << age << " years old." << endl;
}

// setter/getter
void setAge(int a) { age = a; }
int getAge() const { return age; }
};

2.1.2 特殊成员函数

1
2
3
4
5
6
7
8
9
class Example {
public:
Example(); // 默认构造函数
Example(const Example&); // 拷贝构造函数
Example& operator=(const Example&); // 拷贝赋值运算符
~Example(); // 析构函数
Example(Example&&); // 移动构造函数(C++11)
Example& operator=(Example&&); // 移动赋值运算符(C++11)
};

2.2 继承与派生

2.2.1 继承基础

1
2
3
4
5
6
7
8
9
10
11
12
class Student : public Person {  // 公有继承
private:
int grade;
public:
Student(string n, int a, int g) : Person(n, a), grade(g) {}

// 方法重写
void introduce() {
Person::introduce(); // 调用基类方法
cout << "Grade: " << grade << endl;
}
};

2.2.2 继承类型

继承方式 基类public成员 基类protected成员 基类private成员
public public protected 不可访问
protected protected protected 不可访问
private private private 不可访问

2.3 多态与虚函数

2.3.1 虚函数机制

1
2
3
4
5
6
7
8
9
10
11
12
13
class Shape {
public:
virtual double area() const = 0; // 纯虚函数(抽象类)
virtual ~Shape() {} // 虚析构函数
};

class Circle : public Shape {
double radius;
public:
double area() const override { // 重写虚函数
return 3.14 * radius * radius;
}
};

2.3.2 多态应用

1
2
3
4
5
6
7
void printArea(const Shape& shape) {
cout << "Area: " << shape.area() << endl;
}

// 使用
Circle c(5.0);
printArea(c); // 动态绑定调用Circle::area()

2.4 运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
class Vector {
double x, y;
public:
Vector operator+(const Vector& other) const {
return Vector(x + other.x, y + other.y);
}

// 输出运算符重载
friend ostream& operator<<(ostream& os, const Vector& v) {
return os << "(" << v.x << ", " << v.y << ")";
}
};

2.5 友元与静态成员

1
2
3
4
5
6
7
8
9
10
11
12
13
class BankAccount {
private:
double balance;
static int totalAccounts; // 静态成员

public:
friend class BankManager; // 友元类

static int getTotalAccounts() { // 静态方法
return totalAccounts;
}
};
int BankAccount::totalAccounts = 0; // 静态成员初始化

三、高级特性

3.1 多重继承

1
2
3
4
class TA : public Teacher, public Student {
// 可能产生菱形继承问题
// 需要使用虚继承解决
};

3.2 RTTI(运行时类型识别)

1
2
3
4
Base* ptr = new Derived();
if (Derived* d = dynamic_cast<Derived*>(ptr)) {
// 转换成功
}

3.3 现代C++特性(C++11/14/17)

3.3.1 override/final

1
2
3
4
5
6
7
8
9
class Base {
public:
virtual void foo() final; // 禁止重写
};

class Derived : public Base {
public:
void foo() override; // 显式声明重写
};

3.3.2 移动语义

1
2
3
4
5
6
7
8
class ResourceHolder {
int* data;
public:
ResourceHolder(ResourceHolder&& other) noexcept // 移动构造函数
: data(other.data) {
other.data = nullptr;
}
};

四、总结

4.1 面向对象优势

  1. 封装:隐藏实现细节,提供清晰接口
  2. 继承:代码复用和层次化设计
  3. 多态:统一接口,不同实现

4.2 最佳实践建议

  1. 优先使用组合而非继承
  2. 遵循单一职责原则
  3. 为多态基类声明虚析构函数
  4. 避免过度使用友元
  5. 合理使用const成员函数
  6. 注意对象生命周期管理
  7. 使用智能指针管理资源