C++ 面向对象&泛型&STL


C++ 面向对象编程(OOP)是C++语言的核心特性之一,它基于以下三个基本原则:

  1. 封装(Encapsulation)
  2. 继承(Inheritance)
  3. 多态(Polymorphism)

1. 封装(Encapsulation)

封装是将数据(属性)和对数据的操作(方法)组合在一起,形成一个“类”,并对外界隐藏实现细节,只暴露必要的接口。这可以帮助程序员简化对复杂系统的管理,同时确保数据的安全性。

封装的优点:

  • 信息隐藏:外部只能通过公开的接口访问对象的状态,避免直接访问,增强安全性。
  • 代码维护性:修改类的实现时,不会影响到类外的使用者。

封装的实现:

在C++中,我们通过类来实现封装。类定义包含两个部分:数据成员(属性)和成员函数(方法)。

  • 私有成员:类的内部属性只能被类的成员函数访问。
  • 公共成员:类的接口,允许类外部访问。
C++ - 39 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

class Person {
private:
string name; // 私有属性
int age;

public:
// 公共方法,提供对私有成员的访问
void setName(string name) {
this->name = name;
}

string getName() {
return name;
}

void setAge(int age) {
if (age > 0 && age < 150) {
this->age = age;
} else {
cout << "Invalid age!" << endl;
}
}

int getAge() {
return age;
}
};

int main() {
Person p;
p.setName("Alice");
p.setAge(30);

cout << "Name: " << p.getName() << ", Age: " << p.getAge() << endl;
return 0;
}

2. 继承(Inheritance)

继承是一种机制,它允许我们创建一个新的类,这个类继承自一个现有的类,从而复用其已有的代码并扩展新功能。

继承的类型:

  • 公共继承(public inheritance):最常见的继承方式,子类可以访问基类的公有成员。
  • 保护继承(protected inheritance):子类可以访问基类的公有和保护成员,但基类的成员在外部不可见。
  • 私有继承(private inheritance):子类只能访问基类的私有成员,外部不可访问。

继承的优点:

  • 代码复用:继承允许子类重用父类的代码,避免重复编写相同的代码。
  • 扩展性:子类可以通过继承添加或重写父类的方法,增加新功能。

示例代码:

C++ - 23 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class Animal {
public:
void eat() {
cout << "Animal is eating" << endl;
}
};

class Dog : public Animal { // Dog 类继承自 Animal 类
public:
void bark() {
cout << "Dog is barking" << endl;
}
};

int main() {
Dog dog;
dog.eat(); // 从 Animal 类继承来的方法
dog.bark(); // Dog 类自己的方法
return 0;
}

3. 多态(Polymorphism)

多态指的是同一个操作作用于不同的对象时,表现出不同的行为。它主要通过函数重载虚函数来实现。

多态的类型:

  1. 静态多态(Compile-time Polymorphism):通过函数重载和运算符重载实现。
  2. 动态多态(Run-time Polymorphism):通过虚函数和基类指针/引用实现。

静态多态:

静态多态通常通过函数重载或运算符重载来实现,编译时就能确定调用哪个函数。

C++ - 20 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

class Print {
public:
void show(int i) {
cout << "Integer: " << i << endl;
}

void show(double d) {
cout << "Double: " << d << endl;
}
};

int main() {
Print p;
p.show(10); // 调用 show(int)
p.show(3.14); // 调用 show(double)
return 0;
}

动态多态:

动态多态通过虚函数实现。虚函数允许在基类中声明一个函数,在派生类中重新定义该函数,从而根据对象的实际类型调用相应的函数。

C++ - 37 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

class Shape {
public:
virtual void draw() {
cout << "Drawing Shape" << endl;
}
};

class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

class Square : public Shape {
public:
void draw() override {
cout << "Drawing Square" << endl;
}
};

int main() {
Shape* shape;
Circle c;
Square s;

shape = &c;
shape->draw(); // 输出:Drawing Circle

shape = &s;
shape->draw(); // 输出:Drawing Square

return 0;
}

虚函数的作用

  • 基类指针或引用可以指向派生类对象。
  • 在调用函数时,C++会根据实际对象的类型来决定调用哪个版本的函数,这就是多态。

4. 构造函数与析构函数

构造函数:

构造函数是一个特殊的成员函数,它在对象创建时自动调用,用于初始化对象的成员数据。

  • 构造函数没有返回类型。
  • 构造函数的名称与类名相同。

析构函数:

析构函数也是一个特殊的成员函数,它在对象销毁时自动调用,用于释放资源。

  • 析构函数的名称与类名相同,前面加上波浪符号 ~
  • 析构函数没有返回类型。
C++ - 19 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

class Person {
public:
Person() { // 构造函数
cout << "Person created!" << endl;
}

~Person() { // 析构函数
cout << "Person destroyed!" << endl;
}
};

int main() {
Person p; // 创建对象时自动调用构造函数
// 程序结束时,p对象被销毁,自动调用析构函数
return 0;
}
0 comments
Anonymous
Error: Not Found.
Markdown is supported

Be the first person to leave a comment!