Knowledge point of virtual functions

concept

Binder:

( Link 1 ) ( Link 2 )

1. build a computer program itself refers to the process associated with each other, in the build process, it is necessary to call determination operation (program function call ) to perform the operation ( function mapping relationship between a) a code segment; according different stages of build conducted, and can be divided into state build dynamic binding;

2. Generally binder is combined together to form a module or a function process executable code, while the memory address allocated for each call or function module, and also assign the correct memory address for external access, which is a computer program process associated with each other. According to the different stages of the build carried out, it can be divided into two different methods of binder: static binding and dynamic binding.

Static binding and dynamic binding definition:

Static binding means will achieve the function and function calls at compile time associate, therefore, also known as static binding early binding, we must understand at compile time or modules to perform all the functions needed to detect information, its function selection is based on the type of pointer to the object (or reference), C language, all of the bindings are static binding, and any of compilers support the static binding.

Dynamic binding means that when the program was executed to achieve the functions and function calls related, and therefore binding or late binding, also known as run-time, dynamic binding function is not based on the choice of a pointer or reference to, but based on the object type, different object types will make different compilation results. C ++ coding in general, the second line is static binding, but when it comes to polymorphism and virtual functions it is necessary to use a dynamic binding. The following will introduce polymorphism.

Polymorphism of implementation:

Literal meaning of the various forms or morphology. C ++ polymorphism has two forms, dynamic polymorphism and static polymorphism; dynamic polymorphism refers generally polymorphic, by the implementation class inheritance and a virtual function polymorphic; Static Polymorphism by template to achieve, because it it is not the kind of multi-state operation, the so called static compile-time polymorphism.

Implementation process:

Static binding: The main function is achieved by the number of heavy-duty and heavy-duty operation

Dynamic binding: mainly by virtual function to achieve

 Code to understand and analyze :( links )

 1 #include <iostream> 
 2 using namespace std;
 3  
 4 class Shape {
 5    protected:
 6       int width, height;
 7    public:
 8       Shape( int a=0, int b=0)
 9       {
10          width = a;
11          height = b;
12       }
13       int area()
14       {
15          cout << "Parent class area :" <<endl;
16          return 0;
17       }
18 };
19 class Rectangle: public Shape{
20    public:
21       Rectangle( int a=0, int b=0):Shape(a, b) { }
22       int area ()
23       { 
24          cout << "Rectangle class area :" <<endl;
25          return (width * height); 
26       }
27 };
28 class Triangle: public Shape{
29    public:
30       Triangle( int a=0, int b=0):Shape(a, b) { }
31       int area ()
32       { 
33          cout << "Triangle class area :" <<endl;
34          return (width * height / 2); 
35       }
36 };
37 //The main function of the program 
38 is  int main ()
 39  {
 40     the Shape * Shape;
 41 is     the Rectangle REC ( 10 , . 7 );
 42 is     Triangle Tri ( 10 , . 5 );
 43 is   
44 is     // store address rectangle 
45     Shape = & REC;
 46 is     / / find function calls rectangular area area 
47     shape-> area ();
 48   
49     // store address triangle 
50     Shape = & Tri;
 51 is     // call the function triangle mensuration area 
52 is     shape->area();
53    
54    return 0;
55 }
View Code
输出:
Parent class area Parent class area

Code does not use the virtual keyword, which is static binding process because area () function during program compilation has been set up, refer to the following explanation better understanding.

Base class pointer to an object of a derived class , when you call a member function of the same name :( description link )

1) If the member function of the same name as the function in the base class virtual function, then calls the base class is a pointer to the derived class. virtual void display ();

     It can be understood: because the function is virtual, it will find that the real function, so call the B class virtual display in the derived class B.

2) If the base class virtual function in a non-member function, the member function calls the base class. void show ();

     Because the non-virtual base class, it has been fully realized, so there is no need to call the derived class, and calls the base class A class show ()

If instead the code below:

 1 class Shape {
 2    protected:
 3       int width, height;
 4    public:
 5       Shape( int a=0, int b=0)
 6       {
 7          width = a;
 8          height = b;
 9       }
10       virtual int area()
11       {
12          cout << "Parent class area :" <<endl;
13          return 0;
14       }
15 };
View Code
输出:
Rectangle class area Triangle class area

to sum up:

  • Plus virtual base class pointer keyword, all derived classes will default automatically add the virtual keyword.

  meaning:

  • If the class member functions are illustrated as a virtual function, to change the original function may be implemented differently in the derived class
  • When the member function using the pointer or reference to the identified object, the function call using dynamic binding members manner

 

Guess you like

Origin www.cnblogs.com/fanhua666/p/11532367.html