Wu Yuxiong - born natural C ++ language study notes: C ++ interfaces (abstract)

Interface describes the behavior and function of the class, without the need to complete specific implementation class.
C ++ interfaces are implemented using an abstract class, an abstract class and do not confuse with the data abstraction, data abstraction is the concept of a separated implementation details associated with the data.
If there is at least a function class is declared as a pure virtual function, then this class is an abstract class. Pure virtual function is declared by using the " = 0 " is specified as follows:
 class Box
{
   public :
       // pure virtual function 
      Virtual  Double getVolume () = 0 ;
    Private :
       Double length;       // length 
      Double the breadth;      // width 
      Double height;       // height 
};
Designed to abstract class (commonly referred to as ABC), and to provide a suitable base class to other classes can be inherited. An abstract class can not be used to instantiate an object, it can only be used as an interface. If you try to instantiate an object of an abstract class, it will lead to a compiler 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 you do not override the pure virtual function in a derived class, try to instantiate an object of this class can cause compile errors.
Class can be used to instantiate an object is called a concrete class.
Consider the following example, the base class provides an interface to the getArea Shape (), at two derived classes Rectangle and Triangle were achieved getArea ():
#include <iostream>
 
using namespace std;
 
// 基类
class Shape 
{
public :
    // provides an interface framework pure virtual function 
   Virtual  int the getArea () = 0 ;
    void setWidth ( int W)
   {
      width = w;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int width;
   int height;
};
 
// derived class 
class of the Rectangle: public the 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 );
    // output target area 
   COUT << " the Total the Rectangle Area: " << Rect.getArea () << endl;
 
   Tri.setWidth(5);
   Tri.setHeight ( . 7 );
    // output target area 
   COUT << " the Total Area Triangle: " << Tri.getArea () << endl;
 
   return 0;
} 
From the above examples, we can see how an abstract class that defines an interface is the getArea (), two derived classes is how to achieve the same function is calculated by an algorithm different areas.
Design Strategy
Object-oriented system may use an abstract base class provides a suitable, generic, standardized interface for all external applications. Then, the derived classes inherit the abstract base class, similar operations are put all inherited.
External application provides functionality (i.e. the public function) in the form of a pure virtual function is present in the abstract base class. These pure virtual functions are implemented in the respective derived class.
This architecture also allows new applications can easily be added to the system, so even after the system can still be defined.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12149276.html