[C++] Polymorphism (polymorphic conditions, virtual function rewriting, override, final, override hidden comparison)


Preface

The concept of polymorphism: In layman's terms, it means multiple forms. The specific point is to complete a certain behavior. When different objects complete it, different states will be produced.

For example: Polymorphism is when class objects with different inheritance relationships call the same function, resulting in different behaviors. For example, Student inherits Person. Person objects buy tickets at full price, and Student objects buy tickets at half price.

1. Definition and implementation of polymorphism

1.Conditions for polymorphism:

  1. Virtual functions must be called through a pointer or reference to the base class
  2. The called function must be a virtual function , and the derived class must override the virtual function of the base class
    Insert image description here

2. Virtual function

Virtual function: A class member function modified by virtual is called a virtual function.

1. Rewriting of virtual functions

Rewriting (overwriting) of virtual functions: There is a virtual function in the derived class that is exactly the same as the base class (that is, the return value type, function name, and parameter list of the derived class virtual function and the base class virtual function are exactly the same ), which is called a subclass The virtual function overrides the virtual function of the base class.

Insert image description here

2. Exceptions to virtual function overriding (covariance)

Covariance (the return value type of the base class and derived class virtual functions is different) When the derived class overrides the base class virtual function, the return value type is different from the base class virtual function. That is, when a base class virtual function returns a pointer or reference to a base class object, and a derived class virtual function returns a pointer or reference to a derived class object, it is called covariance. (A base class virtual function cannot return a pointer or reference to a derived class, and a derived class virtual function cannot return a pointer or reference to a base class)

class A
{
    
    };

class B : public A
{
    
    };

class Person {
    
    
public:
	virtual	A* BuyTicket() const {
    
     
		cout << "买票-全价" << endl;
		return 0;
	}
};

class Student : public Person {
    
    
public:
	virtual B* BuyTicket() const {
    
     
		cout << "买票-半价" << endl;
		return 0;
	}
};

3. Virtual function of destructor (the destructor names of base class and derived class are different)

Question: Adding virtual to the destructor means rewriting the virtual function?
Answer: Yes, because class destructors are all processed into a unified name of destructor

Question: Why is it done this way?
Answer: Because they need to be rewritten

1. General situation without adding virtual:

Insert image description here

2. Abnormal situations may occur if virtual is not added.

Insert image description here

3. After adding virtual to the destructor

After adding virtual, we can use polymorphism to call the destructor corresponding to the object according to the type
pointed by the pointer . If it is just a normal destructor, it will only call the destructor corresponding to the type according to the type of the object.

Insert image description here

3. C++11 override and final

1.override

Check whether the derived class virtual function overrides a virtual function of the base class. If it does not, a compilation error will be reported.

Insert image description here

2.final

1. Modify the virtual function to indicate that the virtual function cannot be overridden.
Insert image description here

2. Design classes that do not want to be inherited
Insert image description here

4. Comparison of overloading, overwriting (rewriting), and hiding (redefinition)

Insert image description here

Small scale chopper

Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/m0_74774759/article/details/132136171