1. function Rewriting review
(1) The overridden function in the parent class is still inherited to the subclass
(2) A function overridden in a subclass overrides a function in the parent class
(3) through the scope resolution (::) can access functions in the parent class
Child C; Parent* p = &c;c.parent::p rint (); // Inherit C.print () from the parent class ; // overriding in subclasses P->print (); // defined in parent class
2. expected behavior in object-oriented
(1) Determine how to invoke an overriding function based on the actual object type
(2) parent class pointer (reference)
When ① points to the parent class object , the function defined in the parent class is called
When ② points to a subclass object , the overridden function defined in the subclass is called .
3. The concept of multi-state in object-oriented
(1) Determine the specific target of a function call based on the actual object type
(2) The same invocation statement has many different forms of expression in the actual operation
(3) C + + language directly supports the concept of polymorphism
① support for polymorphism by using the virtual keyword
② a function that is declared by virtual is rewritten with polymorphic properties
The function ③ is declared by virtual is called a virtual function .
"Programming Experiment" polymorphic initial experience (can be compared to the non-polymorphic version of Lesson 48th)
#include <iostream>#include<string>using namespacestd;classparent{ Public: Virtual voidprint () //polymorphic version, the only difference with 48 lessons! {cout<<"I ' m Parent."<<Endl; }};classChild: Publicparent{ Public: voidPrint ()//virtual function, the parent class has been added to virtual, there is no need to write{cout<<"I ' m child."<<Endl; } };voidHow_to_print (parent*p) {P->print ();//show polymorphic behavior.}intMain () {Parent p; Child C; //Polymorphic , calling virtual functions according to the actual object typeHow_to_print (&P);//I ' m Parent.How_to_print (&C);//I ' m child. return 0;}
4. The meaning of polymorphism
(1) Demonstrate dynamic characteristics during program operation
(2) Function rewriting must be implemented in a polymorphic way, otherwise meaningless
(3) Polymorphism is the basic characteristic of object-oriented component design
5. Concepts in the theory
(1) Static linking: A specific function call (such as function overloading) can be determined during the compilation of a program
(2) Dynamic linking: After the actual operation of the program to determine the specific function calls (such as function rewriting)
(3) Summary:
① When using a parent pointer (reference) , the function adds virtual to the dynamic Union , without adding virtual as a static binder.
② declares an object using the parent class or subclass itself , and the addition of virtual is statically linked .
"Case analysis" dynamic linking and static linking
"Programming Experiment" the lake and the enmity
6. Summary
(1) function overrides can only occur between the parent class and the child class
(2) Determine the specific function of the call based on the type of the actual object
(3) The virtual keyword is the only way to support polymorphism in C + +
(4) The overridden virtual function can exhibit polymorphic characteristics
The concept and significance of the 49th class polymorphism