Detailed explanation of polymorphism in C++

[TOC]
#Virtual functions and polymorphism

virtual function

Definition: The virtual keyword is added in front of a member function of a class, and the member function is called a virtual function.

###Code example:

class Person
{
public:
    virtual void Buyticket()
    {
        cout << "买票—全价" << endl;
    }
protected:
    char* _name;
};

virtual function override

Definition: When a virtual function that is exactly the same as the parent class is defined in the subclass, the function of the subclass is said to override (also called overwrite) the virtual function of the parent class.

Code example:

class Person
{
public:
    virtual void Buyticket()
    {
        cout << "买票—全价" << endl;
    }
protected:
    char* _name;
};

class Student : public Person
{
public:
    virtual void Buyticket()
    {
        cout << "买票—半票" << endl;
    }
protected:
    char* _name;
};

##Polymorphism
###Definition: An object takes multiple forms
###Code example:

class Person
{
public:
    virtual void Buyticket()
    {
        cout << "买票—全价" << endl;
    }
protected:
    char* _name;
};

class Student : public Person
{
public:
    virtual void Buyticket()
    {
        cout << "买票—半票" << endl;
    }
protected:
    char* _name;
};

void Fun(Person &p)//一个函数实现两种形态
{
    p.Buyticket();
}

int main()
{
    Person p;
    Student s;
    Fun(p);
    Fun(s);
    system("pause");
    return 0;
}

Take a look at the result of polymorphism:
write picture description here

Features of polymorphism:

  1. The derived class rewrites the virtual function of the base class to implement polymorphism, requiring the function name, parameter list, and return value to be exactly the same. (except for covariation)
    1. A virtual function is defined in the base class, and the function always maintains the characteristics of a virtual function in the derived class.
    2. Only member functions of a class can be defined as virtual functions.
    3. Static member functions cannot be defined as virtual functions.
    4. If a virtual function is defined outside the class, virtual can only be added when the function is declared, and virtual cannot be added when the function is defined outside the class.
    5. The constructor cannot be a virtual function. Although operator= can be defined as a virtual function, it is best not to define operator= as a virtual function, because it is easy to
      cause .
    6. Don't call virtual functions inside constructors and destructors, where objects are incomplete and undefined behavior may occur.
    7. It is better to declare the destructor of the base class as a virtual function. (Why? In addition, the destructor is special, because the destructor of the derived class has a different name from the destructor of the base class,
      but it constitutes coverage, because the compiler has done special processing)

Guess you like

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