C++ 中虚函数和纯虚函数的区别
 
本文将介绍 C++ 中虚函数和纯虚函数的区别。
C++ 中的虚函数及其特性
虚函数与多态性的概念密切相关。在 C++ 中,我们可以在链接层次结构中组织不同的类,其中它们可能共享一些数据成员并具有作为接口公开的相同成员函数。
通常,从其他类继承部分代码的类称为派生类,而继承自其他类的类称为基类。请注意,有时这些术语可以与父子或超类-子类名称互换使用。可以在派生类中覆盖的函数称为虚函数,它们用关键字 virtual 声明。虚函数在给定的类层次结构中具有相同的名称,每个派生类都可以实现自己的函数定义。
如果函数未被覆盖,则派生类对象调用基类中定义的函数。下面的示例程序通过在 Engineer 和 Employee 类中定义 print 函数来演示虚函数的基本用法。然后我们可以实现一些任意函数 Func,它接受对 Employee 的引用,并在体内调用 print 函数。
现在,Func 实现可能会多次更改,但它始终会根据作为参数传递的对象调用相应的 print 函数。我们可以在 Employee 类层次结构中添加多个派生类。它们中的每一个都可能/可能不实现 print 函数,但 Func 函数将接受它们的实例并调用正确的虚函数。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
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 实现 Triangle 和 Rectangle 类。在这种情况下,我们在两个派生类中定义了 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
