Wu Yuxiong - born natural C ++ language study notes: C ++ polymorphism

Polymorphism literally means that a variety of forms. When there is a hierarchy between classes, and inheritance between classes through the association will be used polymorphism. 
C when ++ member function calls polymorphic means that will perform different functions depending on the type of the object function is called.
In the following examples, the base class is derived Shape into two classes, as follows: 
#include <the iostream> 
 the using  namespace STD; 
 
class Shape {
    protected :
       int width, height;
    public : 
      Shape ( int A = 0 , int B = 0 ) 
      { 
         width = A; 
         height = B; 
      } 
      int Area () 
      { 
         COUT << " the Parent class Area: " << endl;
          return 0;
      }
};
class Rectangle: public Shape{
   public:
      Rectangle( int a=0, int b=0):Shape(a, b) { }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class Triangle: public Shape{
   public:
      Triangle( int a=0 , int B = 0 ): the Shape (A, B) {}
       int Area () 
      { 
         COUT << " Triangle class Area: " << endl;
          return (width * height / 2 ); 
      } 
}; 
// program the main function 
int main () 
{ 
   the Shape * Shape; 
   the rectangle REC ( 10 , . 7 ); 
   Triangle Tri ( 10 , . 5 ); 
 
   // address storing rectangular 
   Shape = & REC;
    //Rectangular area function call request Area 
   shape-> Area (); 
 
   // address storing triangle 
   Shape = & Tri;
    // call the function triangular mensuration Area 
   shape-> Area (); 
   
   return  0 ; 
} 
causes error output yes, call the function area () is set compiler for the base class's version, which is called the static polymorphism , or statically linked  - a function call before the program is ready to perform. This is also sometimes referred to as early binding , because the area () function during program compilation has been set up.
Minor modifications to the program, in the Shape class, the keyword is placed before the declaration area () of Virtual , as follows:
 class Shape {
    protected :
       int width, height;
    public : 
      Shape ( int A = 0 , int B = 0 ) 
      { 
         width = A; 
         height = B; 
      } 
      Virtual  int Area () 
      { 
         COUT << " the Parent class Area: " << endl;
          return  0 ;  
      }
};
At this time, the compiler is a pointer to see the contents, rather than its type. Accordingly, since the address stored in the object class tri and rec * Shape in, it will invoke the respective area () function. 
As can be seen, each sub-class has a function area () is implemented independently. This is the general use of polymorphism. With polymorphism, you can have several different classes, all with the same name but the parameter function, the function has different implementations may even be the same.
Virtual functions using the keyword in the base class virtual function declarations. When redefine the base class virtual function defined in a derived class, it tells the compiler not statically linked to the function. 
Want is any point in the program can be selected according to the type of object function calls invoked, this operation is called dynamic linking, or late binding.
Pure virtual function 
in the base class can not give meaningful implementation of the virtual function, this time it will use a pure virtual function. 
The area can be virtual function in the base class () can be rewritten as follows: 
class the Shape {
    protected :
       int width, height;
    public : 
      the Shape ( int A = 0 , int B = 0 ) 
      { 
         width = A; 
         height = B; 
      } 
      // virtual function pure 
      virtual  int Area () = 0 ; 
};
 = 0 tells the compiler, the body does not function, the above pure virtual function is a virtual function.

 

Guess you like

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