Virtual functions and pure virtual functions in C++

virtual function

The core of object-oriented is polymorphism, and virtual functions are the core of polymorphism. Before talking about virtual functions, we must first talk about polymorphism, what is polymorphism. Speaking human words: the pointer of the parent class type points to the subclass, and then uses the pointer of the parent class to call the function of the subclass. But there is a problem. The function called with the parent class pointer will execute the method in the parent class regardless of the type.

#include <iostream>
using namespace std;

class AA {
public:
    void show() {
        cout << "调用父类AA的show()方法" << endl;
    }
};

class BB : public AA{
public:
    void show() {
        cout << "调用子类BB的show()方法" << endl;
    }
};

int main() {
    AA* a = new BB();
    a->show();
}

Obviously, the pointer of AA points to BB, but the method of running is indeed AA

To solve this problem, there are virtual functions. Let's add virtual before AA's show method. try it first

#include <iostream>
using namespace std;

class AA {
public:
    virtual void show() {
        cout << "调用父类AA的show()方法" << endl;
    }
};

class BB : public AA{
public:
    void show() {
        cout << "调用子类BB的show()方法" << endl;
    }
};

int main() {
    AA* a = new BB();
    a->show();
}

It is very obvious that the method of BB is called.

The difference between virtual and non-virtual functions

The non-virtual function is actually determined according to the type of the object, reference or pointer that calls the function at compile time. In the first running program, the pointer type of a is AA, so the show method in the AA class is called.

The virtual function can only determine which function to call at runtime, and dynamically selects the appropriate member function during the program running phase. If there is a rewrite in the subclass, the rewritten version will be run. If not, the virtual function (parent class version).

How virtual functions work

Virtual functions are implemented through a virtual function table. VTable for short. In this table, it is mainly a virtual function address table of a class. This table solves the problem of inheritance and coverage, which is equivalent to a map, indicating the actual function that should be called.

pure virtual function

A pure virtual function is a special kind of virtual function. In some cases, the base class cannot provide a meaningful implementation for the virtual function, so it is declared as a pure virtual function.

For example: an animal class can have cats, dogs, tigers, lions, etc. But it is obviously unreasonable for you to use the animal itself to generate objects. For example, cats can climb trees, but not all animals can climb trees. In order to prevent directly using the parent class (animal class) to create objects, pure virtual functions must be used.

#include <iostream>
using namespace std;

class animal {
public:
    virtual void  pashu() = 0;

class cat : public animal {
public:
    void pashu() {
        cout << "我是小猫,我会爬树" << endl;
    }
};


int main() {
    animal* a = new cat();
    a->pashu();

    animal* a = new animal();   // 直接报错

}

A pure virtual function reserves the name of a function in the base class for the derived class so that the derived class can redefine it. Polymorphism cannot be supported if the function name is not preserved in the base class. A class containing pure virtual functions is called an abstract class, objects cannot be instantiated, and pointers and references can be created.

The derived class must redefine the pure virtual function in the abstract class, otherwise it is also an abstract class.

The declaration of a pure virtual function is telling the designer of the subclass, "You must provide an implementation of a pure virtual function, but I don't know how you will implement it"

Guess you like

Origin blog.csdn.net/qq_35326529/article/details/128975709