1 minute to understand the difference between C++ static polymorphism and dynamic polymorphism

Table of contents

1. The concept and classification of polymorphism

2. The role of polymorphism

3. Static polymorphism

4. Dynamic polymorphism

5. Summary


1. The concept and classification of polymorphism

        Polymorphism (Polymorphisn) is an important feature of object-oriented programming (OOP). Polymorphism literally means multiple states. In object-oriented languages, one interface, multiple implementations is polymorphism. Polymorphism in C++ is embodied in two stages of compilation and operation. 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.

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

2. The role of polymorphism

        Why use polymorphism? Encapsulation can make code modular, inheritance can extend existing code, and their purpose is for code reuse. The purpose of polymorphism is for interface reuse. Static polymorphism, the same interface is implemented differently, and different implementations are called according to different parameters (number or type) passed in. Dynamic polymorphism, no matter which class of object is passed, the function can call the method implemented by the respective object through the same interface.

3. Static polymorphism

        Static polymorphism is often implemented through function overloading and templates (generic programming), as shown in 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()2in add_double_doube()2.2in func tempaltea

4. Dynamic polymorphism

        The most common usage of dynamic polymorphism is to declare the pointer of the base class, use the pointer to point to any subclass object, call the corresponding virtual function, and call different methods according to the subclass 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 the rewritten function 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 makes it impossible to "implement one interface, 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();        
}

        Sequential output:

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

        As can be seen from the above example, when using a base class pointer or reference to a subclass object, the function called is a function rewritten in the subclass, thus realizing the dynamic binding of the function address at runtime, that is, dynamic binding . Dynamic polymorphism is realized through "inheritance + virtual function". Only when the program is running (non-compilation period) can the actual type of the referenced object be judged, and the corresponding method can be called according to its actual type. The specific format is to use the virtual keyword to modify the member function of the 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

In application form:

        Static polymorphism is divergent, allowing the same implementation code to be applied to different occasions.

        Dynamic polymorphism is convergent, allowing different implementation codes to be applied to the same occasion.

In the way of thinking:

        Static polymorphism is a generic programming style, which focuses on the universality of algorithms.

        Dynamic polymorphism is an object-based programming style, which values ​​the separation of interface and implementation.


↓↓↓ For more technical content and book information acquisition, please pay attention to "Mingjie Embedded" for technical exchanges in the group ↓↓↓ 

Guess you like

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