学懂C++(五):C++ 多态(Polymorphism) 详解

在 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++ 中一个强大的特性,使得程序能够灵活而高效地处理不同类型的对象。在编写可扩展的代码时,多态提供了很多便利,允许程序员在不修改现有代码的情况下扩展功能。

  • 编译时多态通过函数重载和运算符重载实现,提供了在编译时选择合适的函数。
  • 运行时多态通过虚函数和继承实现,允许在运行时选择合适的函数。

通过合理使用多态,开发者可以创建出更具灵活性和可维护性的代码结构。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿享天开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值