Polymorphism:
- Static-Union polymorphism (compile-time polymorphism) implemented by overloads of functions, operators (the system compiles polymorphism based on the number of parameters)
- Dynamic Union polymorphism (runtime polymorphism) through inheritance, virtual functions (when the runtime can implement objects and functions)C + +: Dynamic linking is implemented with the support of virtual function
Condition: A base class pointer to a derived class object or reference to a base class reference to a derived class object (calling a virtual function)
Dynamic linking:
1#include <iostream>2 using namespacestd; 3 classshape{4 Public: 5 void VirtualDraw () {cout<<"I am Shape"<<endl;}//This sets the draw to be a virtual function.6 voidFun () {Draw ();} 7 }; 8 classCircle Publicshape{9 Public: Ten voidDraw () {cout<<"I am Circle"<<endl;}//Although it is not stated that draw in the Circle class is a virtual function, circle actually inherits the virtual nature One }; A voidMain () { - Circle Oneshape; - Oneshape.fun (); the}
Without virtual, the program will output I am circle, because the virtual function is bound, if not, the output is I am shape; to get to the list, you have to learn the data structure and the Assembly,
In fact, the essence of virtual function is to make a data structure. that is, the virtual function table
Initial understanding of static and dynamic linking.