C++ static polymorphism and dynamic polymorphism

Static polymorphism and dynamic polymorphism


Static polymorphism

  • Static polymorphism is also called compile-time polymorphism, which means which method to call at compile time;
  • Static polymorphism generally refers to method repetition ;
  • As long as the method overload is constituted, it can be considered that the condition of static polymorphism is formed;
  • Static polymorphism is not necessarily related to whether inheritance occurs

As follows:

void test(int a){ cout << "1" << endl; }
void test(int a,int y){ cout << "2" << endl; }
void test(int a,int y,int z){ cout << "2" << endl; }
int main()
{
    test(1);
    test(1, 2);
    test(1, 2, 3);

Although the names of the three functions that are called are the same, they have been compiled until which function is called.

The different responses to the same function are known before compilation.


Dynamic polymorphism

  • Dynamic polymorphism is also called runtime polymorphism, which means which method to call can only be determined at runtime;
  • The following conditions must be specified to form dynamic polymorphism: Inheritance is the primary prerequisite for achieving dynamic polymorphism .
  • There must be method coverage in inheritance;
  • The reference of the base class must point to the instance of the derived class, and the overridden method must be called through the reference of the base class.

This place refers to the code of another blog of mine.

That is to say, animal is used as a base class, and then three derived classes cat, dog, and pig are generated, and an animal array is defined. Each position of the array is converted between the base class and the derived class.

At this time, when compiling, when the show function is called in all three positions of the array, the show function of which class object cannot be distinguished from it, and then it is known at runtime. This is called dynamic polymorphism.

#include<iostream>
using namespace std;

class animal
{
public:
	virtual void show()
	{
		cout << "我是animal类" << endl;
	}
};

class dog :public animal
{
public:
	void show()
	{
		cout << "我是dog类" << endl;
	}
};

class cat :public animal
{
public:
	void show()
	{
		cout << "我是cat类" << endl;
	}
};

class pig :public animal
{
public:
	void show()
	{
		cout << "我是pig类" << endl;
	}
};

int main()
{
	cat a; dog b; pig c;
	animal *x[3] = { &a, &b, &c };
	
	for (int i = 0; i < 3; i++)
	{
		x[i]->show();
	}
	system("pause");
	return 0;
}

Compare

Static polymorphism

advantage:

  1. Since static polymorphism is completed during compilation, it is more efficient and the compiler can also optimize;
  2. It has strong adaptability and loose coupling, such as partial specialization and full specialization to handle special types;
  3. The most important point is that static polymorphism brings the concept of generic design to C++ through template programming, such as the powerful STL library.

Disadvantages:

  1. Because it is a template to achieve static polymorphism, the shortcomings of templates are the disadvantages of static polymorphism, such as difficulty in debugging, time-consuming compilation, code expansion, and compatibility of compiler support
  2. Inability to handle heterogeneous object collections

Dynamic polymorphism

advantage:

  1. OO design, intuitive understanding of the objective world;
  2. Implementation and interface separation, reusable
  3. The powerful power to deal with heterogeneous object collections under the same inheritance system

Disadvantages:

  1. Runtime binding, resulting in a certain degree of runtime overhead;
  2. The compiler cannot optimize the virtual function
  3. The bulky class inheritance system, the modification of the interface affects the entire class hierarchy;

difference:

  1. The essence is different. Static polymorphism is determined at compile time and completed by template realization, while dynamic polymorphism is determined at runtime and realized by inheritance and virtual functions;
  2. The interface in dynamic polymorphism is explicit, centered on the function signature, polymorphism is realized at runtime through virtual functions, and the interface in static multi-station is implicit, centered on valid expressions, and polymorphism is present at compile time through templates. carry out

Same point:

  1. All can achieve polymorphism, static polymorphism/compile-time polymorphism, dynamic polymorphism/run-time polymorphism;
  2. Both can separate the interface and the implementation. One is the template definition interface, the type parameter defines the realization, the other is the base class virtual function definition interface, and the inherited class is responsible for the implementation;

 

Guess you like

Origin blog.csdn.net/qq_46423166/article/details/112385698