C++ Polymorphism

Polymorphism definition : related different classes generated by inheritance, send the same message to their objects, and different objects will produce different behaviors (ie methods) after receiving them.
    A member function declared as vitual in a base class and redefined in one or more derived classes,

The usage format is: vitual function return type function name (parameter table) {function body};

To achieve polymorphism, access the overriding member function of the same name in the derived class through the base class pointer or reference to the derived class.

The role of virtual functions : The operation of member functions can be generalized. When the pointer of the base class points to objects of different derived classes, the base class pointer calls its virtual member function, and the member function that actually points to the object will be called instead of the base class. A member function defined in the class (as long as the derived class overrides the member function) . If it is not a virtual function, no matter which derived class object the base class pointer points to, the function defined in the base class will be called when it is called.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324949823&siteId=291194637