Latest web development tutorials

C ++ interface (abstract class)

It describes the behavior of the interface and functional class, without the need to complete a particular implementation class.

C ++ interface is implemented using anabstract class, abstract class and do not be confused with data abstraction, data abstraction is a concept of the implementation details associated with data separated.

If at least one function class is declared as a pure virtual function, then this class is an abstract class. Pure virtual function by using the "= 0" in the statement specified, as follows:

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

Designan abstract class (commonly referred to as ABC) The purpose is to give the other a class can inherit the appropriate base class.An abstract class can not be used to instantiate an object, it can only be used asan interface.If you try to instantiate an object of an abstract class, it will result in a compilation error.

Therefore, if the ABC a subclass is instantiated, you must implement each virtual function, it also means that C ++ supports the use of ABC statement interface. If there is no overloading pure virtual function in a derived class, try to instantiate an object of this class will cause a compiler error.

Class can be used to instantiate an object is called aconcrete class.

Instance of an abstract class

Consider the following examples, the base class Shape provides an interfacegetArea (), two derived classes Rectangle and Triangle were realized in the 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 produces the following results:

Total Rectangle area: 35
Total Triangle area: 17

From the above examples, we can see how an abstract class is to define an interface getArea (), two derived classes is how to calculate the area of ​​a different algorithm to achieve the same function.

Design Strategy

Object-oriented systems may use an abstract base class provides an appropriate, generic, standardized interface for all external applications. Then, the derived class by inheriting from an abstract base class, put all such operations are inherited.

Function (ie public function) external applications provided in the form of an abstract base class pure virtual functions exist. These pure virtual functions are implemented in the corresponding derived class.

This architecture also enables new applications can easily be added to the system, even after the system is still can be so defined.