本章目录:
前言
多态(Polymorphism)是面向对象编程(OOP)中的一个重要概念,允许相同的接口以不同的方式执行操作。在 C++ 中,多态主要分为 编译时多态(静态多态) 和 运行时多态(动态多态)。
在本文中,我们将详细介绍 C++ 多态的各种实现方式,并提供示例代码帮助理解。
1. 编译时多态(静态多态)
编译时多态主要通过 函数重载(Function Overloading) 和 模板(Templates) 来实现。这种多态在编译阶段就已确定。
1.1 函数重载
函数重载是指在同一作用域内,具有相同名称但参数列表不同的多个函数。
#include <iostream>
class Math {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
};
int main() {
Math math;
std::cout << math.add(2, 3) << std::endl; // 输出 5
std::cout << math.add(2.5, 3.5) << std::endl; // 输出 6.0
std::cout << math.add(1, 2, 3) << std::endl; // 输出 6
return 0;
}
1.2 模板(泛型编程)
模板允许我们编写通用代码,避免为不同数据类型重复编写相似逻辑。
#include <iostream>
// 函数模板
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
std::cout << add(2, 3) << std::endl; // 输出 5
std::cout << add(2.5, 3.5) << std::endl; // 输出 6.0
return 0;
}
1.3 类模板
类模板可以用于创建适用于多种数据类型的类。
#include <iostream>
// 通用 Box 类
template <typename T>
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
void show() {
std::cout << "Value: " << value << std::endl;
}
};
int main() {
Box<int> intBox(10);
Box<double> doubleBox(10.5);
intBox.show(); // 输出:Value: 10
doubleBox.show(); // 输出:Value: 10.5
return 0;
}
2. 运行时多态(动态多态)
运行时多态主要通过 继承 和 虚函数 来实现。这种多态在运行阶段决定调用哪个方法。
2.1 继承与虚函数
#include <iostream>
class Animal {
public:
virtual void speak() {
std::cout << "Animal is making a sound" << std::endl;
}
virtual ~Animal() {}
};
class Dog : public Animal {
public:
void speak() override {
std::cout << "Dog barks" << std::endl;
}
};
class Cat : public Animal {
public:
void speak() override {
std::cout << "Cat meows" << std::endl;
}
};
int main() {
Animal* a1 = new Dog();
Animal* a2 = new Cat();
a1->speak(); // 输出:Dog barks
a2->speak(); // 输出:Cat meows
delete a1;
delete a2;
return 0;
}
2.2 纯虚函数与抽象类
当基类方法没有默认实现时,可以使用 纯虚函数 使其成为抽象类。
#include <iostream>
class Shape {
public:
virtual void draw() = 0; // 纯虚函数
virtual ~Shape() {}
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing Circle" << std::endl;
}
};
class Rectangle : public Shape {
public:
void draw() override {
std::cout << "Drawing Rectangle" << std::endl;
}
};
int main() {
Shape* s1 = new Circle();
Shape* s2 = new Rectangle();
s1->draw(); // 输出:Drawing Circle
s2->draw(); // 输出:Drawing Rectangle
delete s1;
delete s2;
return 0;
}
3. override
和 final
关键字
#include <iostream>
class Base {
public:
virtual void show() const {
std::cout << "Base class" << std::endl;
}
};
class Derived : public Base {
public:
void show() const override { // 使用 override 确保正确重写
std::cout << "Derived class" << std::endl;
}
};
int main() {
Base* b = new Derived();
b->show(); // 输出:Derived class
delete b;
return 0;
}
4. dynamic_cast
:运行时类型识别(RTTI)
#include <iostream>
class Base {
public:
virtual ~Base() {}
};
class Derived : public Base {
public:
void show() {
std::cout << "Derived class" << std::endl;
}
};
int main() {
Base* base = new Derived();
Derived* derived = dynamic_cast<Derived*>(base);
if (derived) {
derived->show(); // 输出:Derived class
}
delete base;
return 0;
}
总结
多态类型 | 实现方式 |
---|---|
编译时多态 | 函数重载、模板、类模板 |
运行时多态 | 继承+虚函数、纯虚函数(抽象类)、dynamic_cast |
C++11 之后 | override 、final 、std::function |
通过上述示例,你可以更清晰地理解 C++ 中的多态机制。如果有任何问题,欢迎交流!