The C ++ object-oriented programming the abstract base class


 A pure virtual function:

  And virtual functions as a pure virtual function No definition. We i.e. j declaration statement before the semicolon at the position of the body plus the function = 0 on a virtual function can be declared as a pure virtual function, where = 0, only appear in the virtual function declaration inside the class at the statement .

As follows: net_price is a pure virtual function:

1 class Disc_quote : public Quote {
2 public:
3    Disc_quote() = default;
4    double net_price(std::size_t) const = 0;
5 protected:
6   std::size_t quantity = 0;
7   double discount = 0.0;
8 
9 }

   It is worth noting: We can also provide defined as a pure virtual function, but the body of the function must be defined outside of the class , which means that we can not provide the function body inside the class as a function = 0.


 Second, the abstract base class:

  Class containing pure virtual functions is an abstract base class (without covering or directly inherited), an abstract base class is responsible for defining the interface and the subsequent other classes can override this interface, we can not create objects of an abstract base class directly, because it internal pure virtual function . But we can define an abstract base class derived class, provided that the derived class virtual function to these covers.

Guess you like

Origin www.cnblogs.com/ccpang/p/11368837.html