Item 34: Differentiate between inheritance of interface and inheritance of implementation(Effective ...

it2022-05-08  8

Inheritance of interface is different from inheritance of implementation. Under public inheritance, derived classes always inherit base class interfaces. Pure virtual functions specify inheritance of interface only. Simple (impure) virtual functions specify inheritance of interface plus inheritance of a default implementation. Non-virtual functions specify inheritance of interface plus inheritance of a mandatory implementation.

 1  #include  < iostream >  2  #include  < string >  3  using   namespace  std;  4   5  class  Shape  6  {  7  public :  8       virtual   void  draw()  const   =   0 ;  9       virtual   void  error( const   string &  msg) 10      { 11          cout  <<  msg  <<  endl; 12      } 13       14  15  }; 16  17  void  Shape::draw()  const 18  { 19      cout  <<   " draw in Shape "   <<  endl; 20  } 21  22  class  Rectangle:  public  Shape 23  { 24  public : 25       virtual   void  draw()  const 26      { 27          cout  <<   " draw in Rectangle "   <<  endl; 28      } 29  }; 30  31  int  main() 32  { 33      Rectangle  * =   new  Rectangle(); 34      r -> draw(); 35      r -> Shape::draw(); 36      cin. get (); 37       return   0 ; 38  }

转载于:https://www.cnblogs.com/zhtf2014/archive/2011/04/02/2003717.html


最新回复(0)