Virtual function and pure virtual function

1. The pure virtual function is declared as follows: virtual void funtion1()=0; Pure virtual functions must not be defin...
  • 1. The pure virtual function is declared as follows: virtual void funtion1()=0;
    Pure virtual functions must not be defined. Pure virtual functions are used to standardize the behavior of derived classes, that is, interfaces. A class containing pure virtual functions is an abstract class. An abstract class cannot define an instance, but can declare a pointer or reference to a specific class that implements the abstract class.

  • 2. The virtual function is declared as follows: virtual ReturnType
    FunctionName(Parameter); The virtual function must be implemented. If it is not implemented, the compiler will report an error. The error prompt is: error LNK * * * *:
    unresolved external symbol "public: virtual void __thiscall
    ClassName::virtualFunctionName(void)"

  • 3. For virtual functions, the parent and child classes have their own versions. Dynamic binding when called by polymorphic mode.

  • 4. The subclass of the pure virtual function is realized. The pure virtual function programs the virtual function in the subclass. The subclass of the subclass, that is, the grandson class, can cover the virtual function and bind dynamically when called in a polymorphic way.

  • 5. Virtual function is a mechanism used to implement polymorphism in C + +. The core idea is to access the functions defined by derived classes through base classes.

  • 6. When there is dynamic allocation of memory on the heap, the destructor must be virtual, but it is not necessary to be pure virtual.

  • 7. Friends are not member functions. Only member functions can be virtual, so friends cannot be virtual functions. However, the virtual problem of friends can be solved by making friend functions call virtual member functions.

  • 8. The destructor should be a virtual function and will call the destructor of the corresponding object type. Therefore, if the pointer points to a subclass object, the destructor of the subclass will be called, and then the destructor of the base class will be called automatically.

A class with pure virtual functions is an abstract class. It cannot generate objects, but can only derive. If the pure virtual function of his derived class has not been rewritten, then its derived class is still an abstract class. Pure virtual functions are defined so that the base class cannot be instantiated, because instantiating such an abstract data structure itself is meaningless, or giving an implementation is meaningless
In fact, I personally think that the introduction of pure virtual functions is for two purposes,

1. For security, avoid any unknown results that need to be clear but caused by carelessness. Remind subclasses to do what they should do.
2. For efficiency, not the efficiency of program execution, but the efficiency of coding.

#include <iostream> using namespace std; class base{ public: virtual void f(){ cout << "i am base f" << endl; } void g(){ cout << "i am base g" << endl; } void y(){ cout << "i am base y" << endl; } }; class base2{ public: virtual void f(){ cout << "i am base2 f" << endl; } void g(){ cout << "i am base2 g" << endl; } void y(){ cout << "i am base2 y" << endl; } }; class Child final:public base, public base2{ public: void f(){ cout << "i am child f" << endl; } void g(){ cout << "i am child g" << endl; } void m(){ cout << "i am child m" << endl; } }; class vbase{ public: virtual void f()=0; virtual void g(){ cout << "i am vbase g" << endl; }; }; class vchild: public vbase{ public: virtual void f(){ cout << "i am vchild f" << endl; } }; int main() { Child a; base *p = &a; base2 *p2 = &a; Child *q = &a; p->f(); p->g(); //p->m(); // Illegal access. The parent class cannot access a function that is not overridden by a subclass, although there is the address of the subclass's own function in the virtual function table. p2->f(); p2->g(); q->f(); q->g(); q->base::y(); // Add action domain q->base2::y(); // Add action domain //Pure virtual function cout << "\n\nvbase" << endl; vbase *va = new vchild(); va->g(); va->f(); return 0; }

Output:

i am child f i am base g i am child f i am base2 g i am child f i am child g i am base y i am base2 y vbase i am vbase g i am vchild f

1 November 2021, 02:52 | Views: 6432

Add new comment

For adding a comment, please log in
or create account

0 comments