在 C++ 中,多态(Polymorphism)是面向对象编程的重要特性之一,允许程序在运行时表现出不同的行为。多态性主要通过三个机制来实现:函数重载、运算符重载和虚函数。下面我们将详细讲解多态及其相关概念。
1. 多态的定义
多态是指同一操作作用于不同的对象时,能够表现出不同的行为。它使得程序在运行时能够选择合适的操作方式,以适应不同类型的对象。
1.1 多态的分类
-
编译时多态(静态多态):
- 主要通过函数重载和运算符重载来实现。
- 在编译时决定调用哪个函数。
-
运行时多态(动态多态):
- 主要通过虚函数和继承来实现。
- 在运行时根据对象的实际类型决定调用哪个函数。
2. 编译时多态
2.1 函数重载
函数重载允许在同一个作用域中定义多个同名但参数列表不同的函数。编译器根据函数调用时提供的参数类型和数量决定调用哪个函数。
#include <iostream>
using namespace std;
class Overload {
public:
void func(int i) {
cout << "Function with int: " << i << endl;
}
void func(double d) {
cout << "Function with double: " << d << endl;
}
void func(int i, double d) {
cout << "Function with int and double: " << i << ", " << d << endl;
}
};
int main() {
Overload obj;
obj.func(10); // 调用 func(int)
obj.func(3.14); // 调用 func(double)
obj.func(10, 3.14); // 调用 func(int, double)
return 0;
}
2.2 运算符重载
运算符重载允许用户自定义运算符的行为,以便能够在特定类型的对象上使用运算符。
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
// 运算符重载
Complex operator + (const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(1.2, 3.4);
Complex c2(5.6, 7.8);
Complex c3 = c1 + c2; // 调用重载的 + 运算符
c3.display(); // 输出: 6.8 + 11.2i
return 0;
}
3. 运行时多态
运行时多态通过虚函数和继承实现,它允许程序在运行时根据对象的实际类型决定调用哪个函数。
3.1 虚函数(Virtual Functions)
虚函数是通过在基类中声明为 virtual 的成员函数。虚函数使得 C++ 能够在基类指针或引用调用时,决定调用派生类中的重写函数。
3.1.1 基本语法
class Base {
public:
virtual void show() { // 基类中的虚函数
cout << "Base class show function." << endl;
}
};
class Derived : public Base {
public:
void show() override { // 重写基类的虚函数
cout << "Derived class show function." << endl;
}
};
3.1.2 使用虚函数的示例
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base class show function." << endl;
}
virtual ~Base() {} // 虚析构函数
};
class Derived : public Base {
public:
void show() override {
cout << "Derived class show function." << endl;
}
};
int main() {
Base* b; // 基类指针
Derived d; // 派生类对象
b = &d; // 基类指针指向派生类对象
// 使用基类指针调用虚函数
b->show(); // 输出: "Derived class show function."
return 0;
}
3.2 虚析构函数
在基类中定义虚析构函数非常重要,尤其是在使用基类指针删除派生类对象时,可以确保派生类的析构函数被正确调用。
class Base {
public:
virtual ~Base() { // 虚析构函数
cout << "Base destructor called!" << endl;
}
};
class Derived : public Base {
public:
~Derived() {
cout << "Derived destructor called!" << endl;
}
};
int main() {
Base* b = new Derived(); // 基类指针指向派生类对象
delete b; // 调用 Derived 的析构函数,然后调用 Base 的析构函数
return 0;
}
4. 总结
多态是 C++ 中一个强大的特性,使得程序能够灵活而高效地处理不同类型的对象。在编写可扩展的代码时,多态提供了很多便利,允许程序员在不修改现有代码的情况下扩展功能。
- 编译时多态通过函数重载和运算符重载实现,提供了在编译时选择合适的函数。
- 运行时多态通过虚函数和继承实现,允许在运行时选择合适的函数。
通过合理使用多态,开发者可以创建出更具灵活性和可维护性的代码结构。

被折叠的 条评论
为什么被折叠?



