迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

C++ 中虚函数和纯虚函数的区别

作者:迹忆客 最近更新:2023/03/24 浏览次数:

本文将介绍 C++ 中虚函数和纯虚函数的区别。


C++ 中的虚函数及其特性

虚函数与多态性的概念密切相关。在 C++ 中,我们可以在链接层次结构中组织不同的类,其中它们可能共享一些数据成员并具有作为接口公开的相同成员函数。

通常,从其他类继承部分代码的类称为派生类,而继承自其他类的类称为基类。请注意,有时这些术语可以与父子或超类-子类名称互换使用。可以在派生类中覆盖的函数称为虚函数,它们用关键字 virtual 声明。虚函数在给定的类层次结构中具有相同的名称,每个派生类都可以实现自己的函数定义。

如果函数未被覆盖,则派生类对象调用基类中定义的函数。下面的示例程序通过在 EngineerEmployee 类中定义 print 函数来演示虚函数的基本用法。然后我们可以实现一些任意函数 Func,它接受对 Employee 的引用,并在体内调用 print 函数。

现在,Func 实现可能会多次更改,但它始终会根据作为参数传递的对象调用相应的 print 函数。我们可以在 Employee 类层次结构中添加多个派生类。它们中的每一个都可能/可能不实现 print 函数,但 Func 函数将接受它们的实例并调用正确的虚函数。

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::string; using std::cin;

class Employee {
public:
    Employee(string fn, string ln) : first_name(std::move(fn)),
                                    last_name(std::move(ln)){}
    virtual void print() {
        cout << "name: " << first_name << "\n"
             << "last name: " << last_name << "\n";
    };

protected:
    string first_name, last_name;
};

class Engineer : public Employee {
public:
    Engineer(string fn, string ln, string sp) : Employee(std::move(fn), std::move(ln)),
            specialization(std::move(sp)) {}

    void print() override {
        Employee::print();
        cout << "specialization: " << specialization << "\n";
    }

private:
    string specialization;
};

void Func(Employee &em) {
    em.print();
}

int main() {

    Employee em1("Jim", "Jiao");
    Engineer eng1("Jin", "Baker", "Aerospace Engineering");

    Func(em1);
    cout << "\n";
    Func(eng1);

    return EXIT_SUCCESS;
}

输出:

name: Jim
last name: Jiao

name: Jin
last name: Baker
specialization: Aerospace Engineering

C++ 中的纯虚函数和抽象类型

另一方面,我们有纯虚函数的概念,它的声明类似于常规虚函数,并包含符号 = 0; 在声明的末尾。这些函数本质上在基类中没有定义,它们首先在基类中声明。因此,它们很可能在派生类中定义。

包含纯虚函数的类称为抽象类,它们通常用于指定派生类的接口。注意抽象类不能直接实例化。

下一个代码片段使用抽象基类 Shape 实现 TriangleRectangle 类。在这种情况下,我们在两个派生类中定义了 printArea 纯虚函数。有时,派生类可能不会定义继承的纯虚函数,使其成为给定类层次结构中的另一个抽象类。派生类可以继承多个纯虚函数。如果它甚至没有定义其中之一,则将抽象分类应用于该类。

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::endl;
using std::string; using std::vector;

class Shape {
public:
    virtual void printArea() = 0;
};

class Triangle : public Shape {
public:
    Triangle(double b, double h) : base(b), height(h) {}

    void printArea() override {
        cout << (base * height) / 2.0;
    }

private:
    double base;
    double height;
};

class Rectangle : public Shape {
public:
    Rectangle(double i1, double i2) : edges({i1, i2}) {}

    void printArea() override {
        cout << edges[0] * edges[1];
    }

private:
    vector<double> edges;
};

int main() {

    Triangle t1(3, 5);
    t1.printArea();
    cout << "\n";

    Rectangle r1(3, 5);
    r1.printArea();
    cout << "\n";

    return EXIT_SUCCESS;
}

输出:

7.5
15

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便