虚函数和纯虚函数的区别

在 C++ 中,虚函数和纯虚函数的概念都与多态和接口实现有关。它们的主要区别在于它们的定义和用途。

虚函数(Virtual Function)

虚函数是在基类中使用 virtual 关键字声明的函数,它的目的是允许派生类(子类)重写(override)该函数的行为。当你通过基类指针或引用调用虚函数时,C++ 运行时会使用动态绑定(late binding)来确定应当调用哪个类的函数实现。虚函数允许基类提供默认的实现,而不需要强制派生类去重写它。
基类 Base 有一个虚函数 Display()。Derived1 和 Derived2 继承自 Base 并覆盖了 Display() 函数。在 main 函数中,我们使用 Base 类型的指针来指向派生类 Derived1 和 Derived2 的对象,并且调用 Display() 函数。通过动态绑定,程序能在运行时决定调用 Derived1 或 Derived2 的 Display() 实现,而不是 Base 类中的实现。
#include <iostream>
#include <memory>

// 基类
class Base {
public:
    virtual void Display() {
        std::cout << "Display Base class" << std::endl;
    }

    virtual ~Base() {}  // 虚析构函数,确保适当调用派生类的析构函数
};

// 派生类1
class Derived1 : public Base {
public:
    void Display() override {  // 使用 override 关键字明确表示覆盖
        std::cout << "Display Derived1 class" << std::endl;
    }
};

// 派生类2
class Derived2 : public Base {
public:
    void Display() override {
        std::cout << "Display Derived2 class" << std::endl;
    }
};

int main() {
#if 1
    std::unique_ptr<Base> basePtr;
    // 指向 Derived1 的对象
    basePtr = std::make_unique<Derived1>();
    basePtr->Display();  // 输出: Display Derived1 class

    // 指向 Derived2 的对象
    basePtr = std::make_unique<Derived2>();
    basePtr->Display();  // 输出: Display Derived2 class
#else
	Base* b; // 基类指针
    Derived1 d1; // 第一个派生类对象
    Derived2 d2; // 第二个派生类对象
    
    // 指向派生类Derived1
    b = &d1;
    b->Display(); // 输出: Derived1 Class Display()
    
    // 指向派生类Derived2
    b = &d2;
    b->Display(); // 输出: Derived2 Class Display()
#endif
    return 0;
}
注意,一个好的实践是在基类中提供虚析构函数。如果你的基类有虚函数,那么它很可能会被用作多态基类,这意味着人们可能会用基类指针指向派生类的对象,并期望通过基类指针来删除这些对象。在这种情况下,如果基类的析构函数不是虚的,那么派生类的析构函数就不会被调用,可能导致资源泄漏或其他问题。

纯虚函数(Pure Virtual Function)

纯虚函数是在基类中声明的一种特殊类型的虚函数,它没有定义(或者说是定义为0),用于创建抽象类。如果类中至少有一个纯虚函数,则该类被认为是抽象类,它不能被实例化。派生类必须重写(override)所有的纯虚函数,才能实例化对象。纯虚函数用于定义接口,在基类中指定派生类必须实现的函数。
#include <iostream>
using namespace std;

// 抽象基类
class Shape {
public:
    virtual void draw() = 0; // 纯虚函数
    virtual ~Shape() {} // 虚析构函数
};

// 派生类 Circle,实现了基类的纯虚函数
class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing a circle." << endl;
    }
    ~Circle() {}
};

// 派生类 Rectangle,实现了基类的纯虚函数
class Rectangle : public Shape {
public:
    void draw() override {
        cout << "Drawing a rectangle." << endl;
    }
    ~Rectangle() {}
};

int main() {
    // Shape shape; // 错误: 抽象类不能被实例化
    Shape *shape; // 可以创建指向Shape类型的指针
    
    Circle circle;
    Rectangle rectangle;
    
    shape = &circle;
    shape->draw(); // 输出: Drawing a circle.
    
    shape = &rectangle;
    shape->draw(); // 输出: Drawing a rectangle.
    
    return 0;
}
当你声明一个纯虚函数时,你告诉使用这个类的开发者:这个函数没有默认实现,必须由派生类提供具体实现。
注意
1、纯虚函数不提供任何实现: 在基类中,纯虚函数不需要(也不能)写出具体的函数体。
2、抽象类不能被实例化: 既然Shape类包含了纯虚函数,你无法创建Shape类的对象。
3、派生类必须实现所有纯虚函数: 任何从抽象类派生的类必须实现基类中所有的纯虚函数,除非它们本身也想成为抽象类。
4、可以有虚析构函数: 即使是抽象类,也可以(实际上经常)含有虚析构函数。如果没有虚析构函数,那么通过基类指针删除派生类对象时可能不会调用派生类的析构函数,导致派生类中的资源泄露。
5、带纯虚函数的类可以有成员函数实现: 一个常见的误解是抽象类只能含有纯虚函数,但实际上抽象类也可以包含正常的成员函数和成员变量,包含纯虚函数意思是该类至少包含一项功能必须由派生类提供具体实现。
6、纯虚析构函数必须提供实现: 尽管纯虚析构函数会使类变为抽象类,但你仍然必须为它提供实现。因为当派生类的对象被销毁时,析构函数的调用链会确保基类的析构函数也被调用。

或者说

#include <iostream>
#include <vector>

// 抽象基类 Shape
class Shape {
public:
    // 纯虚函数
    virtual void draw() const = 0;
    virtual ~Shape() {} // 虚析构函数,避免内存泄漏
};

// Circle 类继承自 Shape
class Circle : public Shape {
public:
    void draw() const override {
        std::cout << "Circle drawn." << std::endl;
    }
};

// Rectangle 类继承自 Shape
class Rectangle : public Shape {
public:
    void draw() const override {
        std::cout << "Rectangle drawn." << std::endl;
    }
};

// 绘制所有图形
void drawShapes(const std::vector<Shape*>& shapes) {
    for (const auto& shape : shapes) {
        shape->draw();
    }
}

int main() {
    Circle circle;
    Rectangle rectangle;

    // 不能创建抽象类 Shape 的实例
    // Shape s; // Error! 不能实例化抽象类

    // 但是可以创建指向派生类对象的基类指针
    std::vector<Shape*> shapes = {&circle, &rectangle};
    drawShapes(shapes); // 输出: Circle drawn.
                        //       Rectangle drawn.
    return 0;
}

总结

虚函数是基类中使用 virtual 关键字声明的成员函数,可以在派生类中被重写,也可以不被重写,如果不被重写,则使用基类的实现。
纯虚函数是一个没有实现的虚函数,它在其声明末尾使用 = 0 表明它是纯虚函数。声明纯虚函数的类是抽象类,不能被实例化,任何继承此类的派生类都必须提供纯虚函数的实现才能实例化。
在派生类中,可以只重写部分虚函数,而其他的不重写;但是纯虚函数必须全部被重写。
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

淘气の小狼人¹º²⁴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值