12-inheritance and polymorphism (lower)
I. Rewrite and reload
Class Parent {public: virtual void func () {cout <"void func ()" <
Function overload:
(1) it must be in the same category, that is, in the same scope.
(2) The subclass cannot reload the function of the parent class, and the function with the same name of the parent class is overwritten.
For example, c. func (); // The error code is not compiled.
(3) The overload function is called Based on the parameter type and quantity during compilation.
Function Rewriting:
(1) it must occur between the parent class and the subclass.
(2) The parent class and the function in the subclass must have the same prototype.
(3) Use virtual declarations to generate Polymorphism
(4) polymorphism is used to call a function according to the specific object type during running.
Comparison: one is determined during compilation and the other is determined during runtime. Therefore, the efficiency of heavy load is higher than the write efficiency.
Ii. Understanding of virtual functions
Implementation principle of polymorphism in C ++
(1) When a virtual function is declared in the class, the compiler will generate a virtual function table in the class.
(2) A virtual function table is a data structure for storing class member function pointers.
(3) the virtual function table is automatically generated and maintained by the compiler.
(4) the virtual member function will be placed in the virtual function table by the compiler.
(5) When a virtual function exists, each object has a pointer to the virtual function table.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink">
Vc3ryb25np1_vcd4kphbyzsbjbgfzcz0 = "brush: java;"> void run (Parent * p) {p-> func (1, 2 );}
Calling the rewrite function through the virtual function table pointer VPTR is performed when the program is running. Therefore, you need to perform addressing operations to determine the actually called function. While normal member functions determine the called functions at compilation. In terms of efficiency, the efficiency of virtual functions is much lower.
Note: For efficiency, it is not necessary to declare all member functions as virtual functions.
When is the VPTR pointer initialized in the object?
(1) When an object is created, the compiler initializes the VPTR pointer,
(2) the direction of the VPTR is final only when the object construction is complete.
(3) The VPTR of the parent class Object directs to the parent class virtual function table.
(4) The VPTR of the subclass object points to the virtual function table of the subclass.
Class Parent {public: Parent () {this-> func ();} virtual void func () {cout <"virtual void Parent: func ()" <
Conclusion: no polymorphism can be achieved when virtual functions are called in constructors.
Iii. Pure virtual functions
Object-oriented abstract classes
(1) abstract classes can be used to represent abstract concepts in the real world
(2) An abstract class is a class that can only define types but cannot generate objects.
(3) abstract classes can only be inherited and related functions can be rewritten.
(4) The direct feature of an abstract class is a pure virtual function.
Note: pure virtual functions only declare the function prototype and do not define the virtual functions of the function body.
Abstract classes and pure virtual functions
(1) abstract classes cannot be used to define objects
(2) abstract classes can only be used to define pointers and references
(3) The pure virtual function in the abstract must be overwritten by the quilt class.
class Shape{public:virtual double area()=0;};
Area is a pure virtual function. = 0 tells the compiler that this function is intentionally declared and not defined.
Class Shape {public: virtual double area () = 0 ;}; class Rectangle: public Shape {double m_a; double m_ B; public: Rectangle (double a, double B) {m_a = a; m_ B = B;} double area () {return m_a * m_ B; }}; class Circle: public Shape {double m_r; public: Circle (double r) {m_r = r;} double area () {return 3.14 * m_r ;}}; void area (Shape * s) {cout <
Area () <
Summary:
(1) function Overloading is different from function rewriting.
(2) polymorphism is implemented through virtual functions.
(3) Efficiency of virtual functions
(4) abstract classes are implemented through pure virtual functions.