The concept of C++ interface (abstract class)

Article directory

Overview

An interface describes the behavior and functionality of a class without requiring a specific implementation of the class.

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

A class is abstract if at least one of its functions is declared as pure virtual. Pure virtual functions are specified by using "= 0" in the declaration, as follows:

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

The purpose of designing abstract classes (often called ABCs) is to provide other classes with an appropriate base class from which they can inherit. An abstract class cannot be used to instantiate objects, it can only be used as an interface. Attempting to instantiate an object of an abstract class will result in a compile error.

Therefore, if a subclass of an ABC needs to be instantiated, it must implement every virtual function, which also means that C++ supports declaring interfaces with ABCs. Attempting to instantiate an object of a derived class without overriding a pure virtual function in that class will result in a compile error.

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

An instance of an abstract class
Please see the following example, the base class Shape provides an interface getArea(), and the two derived classes Rectangle and Triangle implement getArea() respectively:

#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 produces the following results:

Total Rectangle area: 35
Total Triangle area: 17

From the above example, we can 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 from 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 makes it easy for new applications to be added to the system, even after the system is defined.

Guess you like

Origin blog.csdn.net/weixin_43838785/article/details/122920751