Detailed explanation of the difference between static polymorphism and dynamic polymorphism in C++

Table of contents

1. Concept and classification of polymorphism

2. The role of polymorphism

3. Static polymorphism

4. Dynamic polymorphism

5. Summary


1. Concept and classification of polymorphism

        Polymorphism is an important feature of object-oriented programming (OOP). Polymorphism literally means multiple states. In object-oriented languages, an interface with multiple implementations is polymorphism. Polymorphism in C++ is embodied in two stages: compilation and runtime. Compile-time polymorphism is static polymorphism, and the interface used can be determined at compile time. Runtime polymorphism is dynamic polymorphism, and the specific referenced interface can only be determined at runtime.

picture

        The difference between static polymorphism and dynamic polymorphism is actually when to associate function implementation and function call, whether at compile time or run time, that is, whether the function address is bound early or late. Static polymorphism means that the calling address of the function can be determined during compilation and the code is produced. This is static, which means that the address is bound early. Static polymorphism is often also called static binding. Dynamic polymorphism means that the address of a function call cannot be determined during the compiler and needs to be determined at runtime. It is late binding. Dynamic polymorphism is often also called dynamic binding.


2. The role of polymorphism

        Why use polymorphism? Encapsulation can modularize code, and inheritance can extend existing code. Their purpose is to reuse code. The purpose of polymorphism is for interface reuse. Static polymorphism implements the same interface in different ways and calls different implementations according to different parameters (different numbers or types) passed in. Dynamic polymorphism means that no matter which class of object is passed, the function can call the methods implemented by the respective objects through the same interface.


3. Static polymorphism

        Static polymorphism is often implemented through function overloading and templates (generic programming). For details, see the following code:

#include <iostream>
using namespace std;

//两个函数构成重载
int add(int a, int b)
{
  cout<<"in add_int_int()"<<endl;
  return a + b;
}
double add(double a, double b)
{
  cout<<"in add_double_doube()"<<endl;
  return a + b;
}

//函数模板(泛型编程)
template <typename T>
T add(T a, T b)
{
  cout<<"in func tempalte"<<endl;
  return a + b;
}

int main()
{
  cout<<add(1,1)<<endl;           //调用int add(int a, int b)
  cout<<add(1.1,1.1)<<endl;       //调用double add(double a, double b)
  cout<<add<char>('A',' ')<<endl; //调用模板函数,输出小写字母a
}

        Program output:

in add_int_int()
2
in add_double_doube()
2.2
in func tempalte
a

4. Dynamic polymorphism

        The most common use of dynamic polymorphism is to declare a base class pointer, use the pointer to point to any subclass object, and call the corresponding virtual function. Different methods can be called according to the different subclasses pointed to. If virtual functions are not used, that is, C++ polymorphism is not used, when using the base class pointer to call the corresponding function, it will always be limited to the base class function itself, and cannot call overridden functions in the subclass. Because there is no polymorphism, the address of the function call will be certain, and the fixed address will always call the same function, which defeats the purpose of "implementing one interface and multiple implementations".

#include <iostream>
using namespace std;

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

class Derived : public Base
{
public:
  virtual void func()
{
   cout << "Derived::fun()" << endl;
  }
};

int main()
{
  Base* b=new Derived;     //使用基类指针指向派生类对象
  b->func();               //动态绑定派生类成员函数func
  
  Base& rb=*(new Derived); //也可以使用引用指向派生类对象
  rb.func();        
}

        Program output:

Derived::fun()
Derived::fun()

        As can be seen from the above example, when using a base class pointer or reference to point to a subclass object, the function called is a function rewritten in the subclass. This achieves dynamic binding of the runtime function address, that is, dynamic binding. . Dynamic polymorphism is achieved through "inheritance + virtual function". Only during the running of the program (not during compilation) can the actual type of the referenced object be determined and the corresponding method called according to its actual type. The specific format is to use the virtual keyword to modify a member function of a class, indicating that the function is a virtual function, and the derived class needs to reimplement the member function, and the compiler will implement dynamic binding.


5. Summary

        Application form:

        Static polymorphism is divergent, allowing the same implementation code to be used in different situations.

        Dynamic polymorphism is convergent, allowing different implementation codes to be used in the same situation.

        In terms of thinking:

        Static polymorphism is a generic programming style that values ​​the universality of algorithms.

        Dynamic polymorphism is an object-based programming style that values ​​the separation of interfaces and implementations.


↓↓↓ For more technical content and books and materials, please pay attention to "Clear Embedded" for technical exchanges in the group ↓↓↓ 

Guess you like

Origin blog.csdn.net/helloqusheng/article/details/133100991