Cplusplus 简明教程

Interfaces in C++ (Abstract Classes)

接口描述 C++ 类的行为或功能,而不承诺该类的特定实现。

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.

C++ 接口使用 abstract classes 实现,且这些抽象类不应与数据抽象混淆,数据抽象是一个将实现细节与关联数据分开的概念。

The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.

通过将至少一个函数声明为 pure virtual 函数,可以使一个类变为抽象类。纯虚函数通过在其声明中放置 “= 0” 来指定,如下所示 −

A class is made abstract by declaring at least one of its functions as pure virtual function. A pure virtual function is specified by placing "= 0" in its declaration as follows −

class Box {
   public:
      // pure virtual function
      virtual double getVolume() = 0;

   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};

abstract class (通常称为 ABC)的目的是提供一个合适的基类,其他类可以从中继承。抽象类不能用于实例化对象,只用作 interface 。尝试实例化抽象类的对象会引发编译错误。

The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.

因此,如果需要实例化 ABC 的子类,则它必须实现每个虚函数,这意味着它支持由 ABC 声明的接口。如果在派生类中未重写纯虚函数,然后尝试实例化该类的对象,则会发生编译错误。

Thus, if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.

可以用来实例化对象的类称为 concrete classes

Classes that can be used to instantiate objects are called concrete classes.

Abstract Class Example

考虑以下示例,其中父类为基类提供一个接口来实现一个称为 getArea() 的函数 −

Consider the following example where parent class provides an interface to the base class to implement a function called getArea()

#include <iostream>

using namespace std;

// Base class
class Shape {
   public:
      // pure virtual function providing interface framework.
      virtual int getArea() = 0;
      void setWidth(int w) {
         width = w;
      }

      void setHeight(int h) {
         height = h;
      }

   protected:
      int width;
      int height;
};

// Derived classes
class Rectangle: public Shape {
   public:
      int getArea() {
         return (width * height);
      }
};

class Triangle: public Shape {
   public:
      int getArea() {
         return (width * height)/2;
      }
};

int main(void) {
   Rectangle Rect;
   Triangle  Tri;

   Rect.setWidth(5);
   Rect.setHeight(7);

   // Print the area of the object.
   cout << "Total Rectangle area: " << Rect.getArea() << endl;

   Tri.setWidth(5);
   Tri.setHeight(7);

   // Print the area of the object.
   cout << "Total Triangle area: " << Tri.getArea() << endl;

   return 0;
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Total Rectangle area: 35
Total Triangle area: 17

你可以看到抽象类如何以 getArea() 的形式定义接口,以及其他两个类使用不同的算法来计算特定形状的区域,但它们实现的是同一个函数。

You can see how an abstract class defined an interface in terms of getArea() and two other classes implemented same function but with different algorithm to calculate the area specific to the shape.

Designing Strategy

面向对象系统可能使用抽象基类来为所有外部应用程序提供一个通用和标准化的接口。然后,通过从该抽象基类继承,将形成以类似方式工作的派生类。

An object-oriented system might use an abstract base class to provide a common and standardized interface appropriate for all the external applications. Then, through inheritance from that abstract base class, derived classes are formed that operate similarly.

外部应用程序提供的功能(即,公有函数)作为抽象基类中的纯虚函数。这些纯虚函数的实现由对应于应用程序特定类型的派生类提供。

The capabilities (i.e., the public functions) offered by the external applications are provided as pure virtual functions in the abstract base class. The implementations of these pure virtual functions are provided in the derived classes that correspond to the specific types of the application.

此架构还允许在新系统定义后轻松地向其中添加新的应用程序。

This architecture also allows new applications to be added to a system easily, even after the system has been defined.