C++ interface (abstract class)


The interface describes the behavior and functionality of the class without completing a specific implementation of the class.

C++ interface is implemented using abstract classes. Abstract classes and data abstraction are not confused with each other. Data abstraction is a concept that separates implementation details from related data.

If at least one function in a class is declared as a pure virtual function, the class is an abstract class. Pure virtual functions are specified by using "= 0" in the declaration, as shown below:

class Box
{
   public:
      // 纯虚函数
      virtual double getVolume() = 0;
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};

DesignAbstract class (commonly called ABC) is to provide other Classes provide an appropriate base class from which to inherit. An abstract class cannot be used to instantiate objects, it can only be used as an interface. If you try to instantiate an object of an abstract class, it will cause a compilation error.

Therefore, if a subclass of ABC needs to be instantiated, each virtual function must be implemented, which also means that C++ supports the use of ABC to declare interfaces. If a pure virtual function is not overloaded in a derived class, trying to instantiate an object of this class will result in a compilation error.

A class that can be used to instantiate objects is called a concrete class.

Instances of abstract classes

Please see the following examples. The base class Shape provides an interface getArea(), which is implemented in the two derived classes Rectangle and Triangle respectively. getArea():

#include <iostream>
 
using namespace std;
 
// 基类
class Shape 
{
public:
   // 提供接口框架的纯虚函数
   virtual int getArea() = 0;
   void setWidth(int w)
   {
      width = w;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int width;
   int height;
};
 
// 派生类
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);
   // 输出对象的面积
   cout << "Total Rectangle area: " << Rect.getArea() << endl;

   Tri.setWidth(5);
   Tri.setHeight(7);
   // 输出对象的面积
   cout << "Total Triangle area: " << Tri.getArea() << endl; 

   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Total Rectangle area: 35
Total Triangle area: 17

From the above example, we can see See how an abstract class defines an interface getArea(), and how two derived classes implement the same function through different algorithms for calculating area.

Design Strategy

Object-oriented systems may use an abstract base class to provide an appropriate, common, standardized interface for all external applications. Then, the derived class inherits all similar operations by inheriting the abstract base class.

The functions provided by external applications (that is, public functions) exist in the form of pure virtual functions in the abstract base class. These pure virtual functions are implemented in the corresponding derived classes.

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