I. Why does C ++ introduce virtual functions? What are the purposes of virtual functions?
1. Non-virtual world
Object self-adaptability: each object will make an appropriate response to the same function call.
B. Call a function by pointing to the base class pointer of the subclass object:
Only member functions of the base class can be called, although the Pointer Points to the subclass object.
Once a member function specific to a subclass is called, a compilation error occurs.
C. Call a function by pointing to the child of the base class object:
You can call member functions of the subclass, although the pointer is directed to the base class object.
Access member variables of child classes directly or in brief, with unpredictable consequences
D. Name hiding
Member functions of subclass hide member functions of the base class with the same name.
# Include <iostream> # include <string> using namespace STD; class base {public: Base (int data = 0): m_data (data) {} void print (void) const {cout <m_data <Endl;} PRIVATE: int m_data;}; Class derived: public base {public: derived (INT data, const string & info ): base (data), m_info (Info) {} void show (void) const {print (); cout <m_info <Endl;} void Foo (void) {cout <"derived: Foo (void)" <Endl; // m_info = "ABC" ;}private: String m_info ;}; int main (void) {Base B (100); derived * Pd = static_cast <derived *> (& B);/* pointer to the parent class * // * Pd-> show (); * // * The call can be successful but the call result is undefined because the compiler only looks at the type during the call. */Pd = NULL; Pd-> Foo (); // This is acceptable, because the compiler only looks at the type during compilation. (derived *) 0)-> Foo ();/* The function does not exist in the object. When calling a member function, as long as the called type is correct, you can call the corresponding member functions according to the type. however, in this case, the member variable */return 0;} cannot be accessed ;}
2. Hide virtual functions and names
Class Name
{
Virtual return type function name (parameter table ){...}
}
Is called a virtual function or method.
Overwrite:
If the member functions of the subclass and the virtual functions of the base class have the same function prototype, then the Member
A function is also a virtual function, no matter whether it has a virtual keyword or not, and the virtual function of the base class
Overwrite.
In addition, if there is no virtual function, the function will overwrite if the function names are the same.
3. Polymorphism
If the subclass provides effective coverage for the base class virtual function, you can use a base class pointer pointing to the subclass object,
You can also reference the base class reference of the subclass object to call the virtual function. The actual called class will be overwritten by the subclass.
Version, not the primitive in the base class. This phenomenon is called polymorphism.
The significance of polymorphism is that, under normal circumstances, the member function of the class called is referenced by the caller pointer or
Its type is determined, and when polymorphism occurs, which class of member function is called
It is determined by the type of the transmitter pointer or referenced actual target object.
4. The overwrite of valid virtual functions must meet the following conditions:
This function must be a member function, neither a global function nor a static member function.
This function must be declared as a virtual function using the vitual keyword in the base class.
The overwriting version and the base version must have exactly the same signature, that is, the function name. The row parameter table and the common attribute are strictly consistent.
If the base-class version returns the basic data type, the overwritten version must return the same type of data.
If the base class version returns the pointer or reference of the class type object, overwrite the version to return
Pointer or reference of a subclass type object.