Polymorphism Definition

1. What is polymorphism?

Polymorphism can be simply summarized as "one interface, multiple methods", and the program decides the function to call at runtime . It is a core concept in the field of object-oriented programming.

Only rewriting virtual functions can reflect the polymorphism of C++


Virtual function:

  • Virtual functions play a decisive role in polymorphism, and only virtual functions can constitute polymorphism.
  • 1) You only need to add the virtual keyword to the declaration of the virtual function, which may or may not be added to the function definition.
  • 2) For convenience, you can only declare functions in the base class as virtual functions, so that all functions of the same name with shadowing (overriding) relationships in derived classes will automatically become virtual functions.
  • 3) When a virtual function is defined in the base class, if the derived class does not define a new function to shadow this function, then the virtual function of the base class will be used.
  • 4) Only the virtual function of the derived class shadows the virtual function of the base class (the function prototype is the same) to constitute polymorphism (accessing the derived class function through the base class pointer)
  • 5) The constructor cannot be a virtual function.
  • 6) Destructors can be declared as virtual functions, and sometimes they must be declared as virtual functions.
  • Condition that constitutes polymorphism
  • There must be an inheritance relationship;

    There must be virtual functions of the same name in an inheritance relationship, and they are shadowing (overriding) relationships.

    There is a pointer to the base class through which the virtual function is called.

     

    It must be through the upward transformation of the pointer. If the upward transformation of the object is used, it can only be an ordinary upward assignment.

     

    Ordinary pointer upcasting, the base class calls the member methods of the base class and uses the member variables of the derived class.

    But if it is a virtual function, then the (virtual) member methods of the derived class and the member variables of the derived class are used. Don't think of him as an upcast, think of him as a polymorphism.

 

example code

#include <iostream>
using namespace std;

//Base class Base
class Base{
public:
	virtual void func();
	virtual void func(int);
};
void Base::func(){
	cout << "void Base::func()" << endl;
}
void Base::func(int n){
	cout << "void Base::func(int)" << endl;
}

//Derived class Derived
class Derived : public Base{
public:
	void func();
	void func(char *);
};
void Derived::func(){
	cout << "void Derived::func()" << endl;
}
void Derived::func(char *str){
	cout << "void Derived::func(char *)" << endl;
}

int main(){
	Base *p = new Derived();
	p->func(); //output void Derived::func()
	p->func(10); //output void Base::func(int)
	p->func("hello");  //compile error

	return 0;
}

Notice:
In this inheritance relationship, the base class cannot call the member functions declared by the derived class. In the final analysis, the base class pointer is still the base class!


Guess you like

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