Polymorphism in C++, its implementation, and two special cases of polymorphism.

1. What is polymorphism?

In layman’s terms, it is a variety of forms. The specific point is that when different objects accomplish certain things, they will produce different states.

For example: just like: when buying tickets, ordinary people, students, soldiers, etc., they have different results when buying tickets, ordinary people enjoy full-price tickets, students enjoy student-price tickets, soldiers have priority ticket purchases, and military tickets fare.

2. The realization of polymorphism

1. There are two conditions for polymorphism:

(1) It must be a pointer of the base class or a reference of the base class to call the virtual function.

(2) The called function must be a virtual function, and the derived class must rewrite the virtual function of the base class. (There are two specials)

To put it simply: 1. Virtual function rewriting 2. Parent class pointer or reference to call virtual function

2. What is a virtual function?

A virtual function is to add virtual decoration in front of the member function

3. Virtual function rewriting/coverage conditions:

(1) Virtual function + triple (function name, parameter, return value)

(2) If it does not conform to rewriting, it is a hidden relationship

Note: (two special)

(1) Subclass virtual functions without adding virtual still constitute rewriting (preferably added)

(2) Overridden covariance. The return value can be different, and it must be a pointer or reference of the parent-child relationship

Let’s take a look at the code, as follows:

class Person {
public:
virtual void BuyTicket() { cout << "Buy ticket full price" << endl; }
};

class Student : public Person
{
public:
virtual void BuyTicket() { cout << "Buy Ticket Half Price" << endl; }
};

class Soldier : public Person
{
public:
virtual void BuyTicket() { cout << "Buy Ticket Priority" << endl; }
};

//void Func(Person p) // If it is not a pointer or reference of the parent class, it is not polymorphic
void Func(Person & p)
{
p. BuyTicket();
}

int main()
{
Person p;
Student st;
Soldier so;

Func(p);
Func(st);
Func(so);

return 0;
}

After running, the result is as follows:

As can be seen from the above running results, different objects pass references to the parent class, and the functions they call are different.

4. Let me take a look at the two conditions for breaking polymorphism. As follows:

(1) Break the pointer or reference of the parent class

I changed the reference to a normal object call to see what the result is, as follows:

void Func(Person p) // If it is not a pointer or reference of the parent class, it is not polymorphic
//void Func(Person & p)
{
p. BuyTicket();
}

The result of the operation is as follows:

After the modification, the result is the member function of Person, why? Because it is a normal call after modification (I will elaborate on the details in the next article)

(2) Break the rewriting condition (rewriting: virtual function + three same)

① I remove the virtual function in the parent class, as follows:

class Person {
public:
//virtual void BuyTicket() { cout << "Buy ticket full price" << endl; }
void BuyTicket() { cout << "Buy ticket full price" << endl; }
};

The result of the operation is as follows:

The same is true, the result is to call the method of the parent class.

② Remove the same function names in the three same, here I change the names of the three functions to be different, as follows: (If one is changed, the other two are still the same, the result is the result printed by the respective member functions )

class Person {
public:
virtual void BuyTicket() { cout << "Buy ticket full price" << endl; }
//void BuyTicket() { cout << "Buy ticket full price" << endl; }
};

class Student : public Person
{
public:
virtual void Buy() { cout << "Buy Ticket Half Price" << endl; }
};

class Soldier : public Person
{
public:
virtual void BuyTi() { cout << "Ticket Buying Priority" << endl; }
};

The result of the operation is as follows:

5. Two special cases are explained below

(1) Subclass virtual functions without adding virtual still constitute rewriting (preferably added)

(2) Overridden covariance. The return value can be different, and it must be a pointer or reference of the parent-child relationship

First explain (1), as follows:

I removed the virtual of the member functions of student and soldier, so these two member functions are not virtual functions, right?

So how did it turn out? as follows:

class Person {
public:
virtual void BuyTicket() { cout << "Buy ticket full price" << endl; }
//void BuyTicket() { cout << "Buy ticket full price" << endl; }
};

class Student : public Person
{
public:
//virtual void BuyTicket() { cout << "Buy Ticket Half Price" << endl; }
void BuyTicket() { cout << "Buy Ticket Half Price" << endl; }
};

class Soldier : public Person
{
public:
//virtual void BuyTicket() { cout << "Ticket Buying Priority" << endl; }
void BuyTicket() { cout << "Ticket Buying Priority" << endl; }
};

The result of the operation is as follows:

The running results are still their own methods, why?

Because after the subclass inherits the parent class, it inherits the virtual function of the parent class and rewrites the method of the parent class. Simply put, after the subclass inherits, the subclass also has the same virtual function, but the virtual function The implementation of the function is still subclassed.

Finally explain (2), as follows:

Although the return value can be the same, it must be a pointer or reference to the parent-child relationship. Here I will explain it with a reference, as follows:

class Person {
public:
virtual Person & BuyTicket()
{
cout << "Buy ticket full price" << endl;
return *this;
}
};

class Student : public Person
{
public:
virtual Student & BuyTicket()
{
cout << "Half price for tickets" << endl;
return *this;
}
};

class Soldier : public Person
{
public:
virtual Soldier & BuyTicket()
{
cout << "Ticket Buying Priority" << endl;
return *this;
}
};

The result of the operation is as follows: