The concept of Polymorphism
Polymorphism is an important feature of object-oriented programming. With polymorphism, a scalable system can be designed and implemented.
In C ++ programming, polymorphism refers to a function with different functions that can be called by a function name.
In object-oriented methods, polymorphism is generally expressed as follows: send the same message to different objects, and different objects will produce different behaviors (that is, methods) during receiving ). That is to say, each object can respond to a common message in its own way.
From the perspective of system implementation, there are two types of polymorphism: static polymorphism and dynamic polymorphism.
The previously learned function overloading and operator overloading realize the static polymorphism of the polymorphism attributes. In the program compilation system, the system can decide which function to call. Therefore, static polymorphism is also called the polymorphism during compilation. Static polymorphism is implemented through function overloading (Operator Overloading is actually a function overload ).
Dynamic polymorphism is the object for dynamically determining the operation only when the program is running. It is also known as Runtime polymorphism. Dynamic polymorphism is implemented through virtual functions.
Virtual Functions
Role of virtual sweat
In the inheritance hierarchy of a class, functions with the same name, the same number of parameters, and different types can appear in different layers. The system determines the called object based on the overwrite principle of the same name.
The role of a virtual function is to allow you to redefine functions with the same name as the base class in the derived class, and to access functions with the same name in the base class pointer or reference.
How to use virtual functions
1) use virtual to declare a member function as a virtual function in the base class. In this way, you can redefine this function in a derived class, assign it new functions, and be conveniently called. when defining a virtual function outside the class, you do not need to add the virtual function
2) to redefine this function in a derived class, the function name, function type, number and type must all be the same as the virtual function of the base class, and the function body should be redefined according to the needs of the derived class.
C ++ requires that when a member function is declared as a virtual function, all functions of the same name in the derived class automatically become virtual functions. Therefore, when the derived class re-declares the virtual function, virtual can be added or not, but virtual is usually added at each layer to declare the function, making the program clearer.
If the virtual functions of the base class are not redefined in the derived class, the derived class simply inherits the virtual functions of its direct base class.
3) define a pointer variable pointing to the base class object and point it to the object that needs to call this function in the same class family.
4) call this virtual function using the pointer variable. In this case, the function of the same name is called for the object executed by the pointer variable.
By using virtual functions in conjunction with pointer variables of base class objects, you can easily call functions of the same name of different classes in the same class family, as long as you point to the base class pointer first. If the pointer continuously points to objects of different classes in the same class family, it can continuously call functions of the same name in these objects.
It should be noted that sometimes the non-virtual functions defined in the base class are redefined in the derived class. If the base class pointer is used to call the member function, the system calls the member functions of the basic class in the object. If the pointer of the derived class calls the member function, the system calls the member functions of the object in the derived class, this is not a polymorphism (using different types of pointers) and does not use the functions of virtual functions.
Previously, the function overload mentioned above is a function with the same name at the same level, while the virtual function handles functions with the same name at different derived levels. The former is a horizontal overload, the latter can be understood as a vertical overload, but unlike the overload, the header of the virtual functions of the same class family is the same, when a function is overloaded, the function header is different (the number and type of parameters are different)
Virtual functions should be declared in the case of declaration
When using virtual functions, pay attention to the following two points:
1) You can only declare a member function of a class using virtual to make it a virtual function, rather than declaring a common function outside the class as a virtual function, because the role of a virtual function is to allow the definition of the virtual function of the base class in the derived class, it can only be used in the inheritance hierarchy of the class.
2) After a member function is declared as a virtual function, classes in the same class do not need to define a non-virtual function, but have the same parameters (including the number and type) as the virtual function) a function of the same name as the function return value type.
It should be noted that when a virtual function is used, the system requires a certain amount of space overhead. When a class has a virtual function, the compilation system constructs a virtual function table (vtable) for this class. It is a pointer array that stores the entry address of each virtual function, the system has little time overhead when performing multi-state association. Therefore, polymorphism is efficient.
Virtual destructor
The role of the Destructor is to "Clean up the field" necessary before the object is revoked. when the object of the derived class is revoked from the memory, the destructor of the derived class is called first, then, call the destructor of the base class.
However, if a temporary object is created using the new operator, if a destructor exists in the base class and a pointer variable pointing to the base class is defined, when a program uses the delete operator with a pointer parameter to cancel an object, the system only executes the destructor of the base class instead of the destructor of the derived class.
When the destructor of the base class is a virtual function, no matter which object the Pointer Points to in the same class family, the system uses dynamic Association to call the corresponding destructor, clears the object.
If you declare the destructor of the base class as a virtual function, the destructor of all the derived classes of the base class will automatically become virtual functions, even if the name of the derived class destructor is different from that of the base class destructor.
It is best to declare the destructor of the base class as a virtual function, so that all the destructor of the derived class will automatically become a virtual function. In this way, if the delete operator is used in the program to delete an object, and the delete operator uses a base class pointer to the object of the derived class, the system calls the destructor of the corresponding class.
The concept and usage of virtual destructor are simple, but it is an important technique in object-oriented programming. Professionals are generally used to declaring virtual destructor. Even if the base class does not need a destructor, a virtual destructor with an empty function body is displayed. To ensure proper processing when the dynamic space allocation is revoked
Pure virtual functions and abstract classes
Pure virtual functions
Sometimes, a member function is declared as a virtual function in the base class. It is not the requirement of the base class, but a function name is reserved in the base class considering the needs of the derived class, the specific function is left to the derived class as needed.
A pure virtual function is a function initialized to 0 when a virtual function is declared. The general form of a pure virtual function is
Virtual function type function name (parameter list) = 0;
Note: 1) pure virtual functions do not have function bodies
2) The final "= 0" does not mean that the return value of the function is 0. It only plays a formal role and tells the compiling system that this is a pure virtual function.
3) This is a declaration statement and should end with a semicolon
Pure virtual functions only have function names, but do not have function functions. They cannot be called. They only notify the compilation system. Here, a virtual function is declared and left for definition in the derived class.
After a definition is provided in a derived class, it can have the function and can be called.
A pure virtual function is used to reserve the name of a function for the derived class in the base class, so that the derived class can define it as needed.
If the function name is not retained in the base class, polymorphism cannot be implemented.
If a pure virtual function is declared in a class but is not defined in its derived class, the virtual function is still a pure virtual function in the derived class.
Abstract class
If a class is declared, it can be used to define objects. However, in Object-Oriented Programming, there are usually some classes that do not need to be generated, the only purpose of defining these classes is to use them as the base class to create a derived class.
This class is called abstract class, instead of defining objects and only being used as a basic type for inheritance, because it is often used as a base class. It is usually an abstract base class.
All classes that contain pure virtual functions are abstract classes, because pure virtual functions cannot be called, and classes that protect pure virtual functions cannot create objects. Abstract Classes Act as the public base class of a class family, or provide a public interface for a class family.
Although abstract classes cannot define objects (or abstract classes cannot be instantiated), they can point to pointer variables of abstract class data.
When a derived class becomes a specific class, you can use this pointer to point to the object of the derived class, and then if this pointer calls a virtual function to implement multi-state operations.
If a virtual function is declared in the base class, all functions in the derived class that have the same name, function type, number of parameters, and type as the function, all are virtual functions (whether or not virtual declaration is used in the derived class)