Note:
1. after a member function is declared as a virtual function, classes in the same type of family cannot define a non-virtual 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.
2. What are the considerations for declaring a member function as a virtual function?
(1) Whether the class where the member function is located serves as the base class.
(2) Is it possible for a member function to be changed after the class is inherited? If you want to change its function, you should declare it as a virtual function.
If a member function does not need to be modified after the class is inherited, or the derived class does not use this function, do not declare it as a virtual function.
Do not declare all member functions in the class as virtual functions only for the basis class.
(3) Whether the call is accessed through the object name or through a base class pointer or reference. If it is accessed through a base class pointer or reference, it should be declared as a virtual function.
Note: When using virtual functions, the system requires a certain amount of space overhead. When a class has a virtual function, the compilation system constructs a virtual function table for the class, which is a pointer array that stores the entry address of each virtual function. The cost of the system for Dynamic Association is very small, improving the efficiency of polymorphism.
3. The role of the Destructor is to undo the class object from the memory before the object is revoked. Generally, the system only executes the destructor of the base class and does not execute the destructor of the derived class.
You only need to declare the destructor of the base class as a virtual function, that is, the virtual destructor. In this way, the base class object is revoked and the object of the derived class is revoked, this process is completed dynamically.
4. if you declare the destructor of the base class as a virtual function, the destructor of all the derived classes of the base class automatically become virtual functions, even if the name of the derived class's destructor is different from that of the base class.
It is best to declare the destructor of the base class as a virtual function. This will make the destructor of all derived classes automatically become virtual functions. If the explicit Delete operator in the program deletes an object, and the operation object uses the base class pointer pointing to the object of the derived class, the system calls the destructor of the corresponding class.
5.Constructors cannot be declared as virtual functions.
6. Pure virtual functions
Sometimes, the virtual functions in the base class are declared and defined for use in the derived class, which has no meaning in the base class. These functions are called pure virtual functions. They do not need to be written as empty functions, but must be declared as follows:
Virtual float area () const = 0;
General Format: virtual function type function name (table of parameters) = 0;
Note:Pure virtual functions do not have function bodies;
"= 0" does not mean that the return value of the function is 0. It only serves the form and tells the compilation system. "This is a pure virtual function ";
This is a declaration statement and should end with a semicolon.
Pure virtual functions only have function names but do not have function functions and cannot be called. This function can be called only when it is defined in a derived class.