Polymorphism-polymorphism (function polymorphism, macro polymorphism, static polymorphism, dynamic polymorphism)

Source: Internet
Author: User

Polymorphism)

Literally, the behavior of the same method varies with the context.

Wikipedia: Polymorphism (computer science), The ability incomputer programming to present the same interface for differing underlyingforms (data types ).

 

1,Function Polymorphism(Function polymorphism): that is, function overload)


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/4B/wKiom1QGxxDjcyKYAAHP7VCAaPA651.jpg "Title =" ..jpg "alt =" wkiom1qgxxdjcykyaahp7vcaapa651.jpg "/>

The compiler compiles different functions based on different parameter lists. That is, for the same function name, the compiler can generate different functions based on different parameter lists.


2,Macro Polymorphism(Macro polymorphism ):

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/4C/wKioL1QGxx_CU1huAAHPIstpucg661.jpg "Title =" ..jpg "alt =" wkiol1qgxx_cu1huaahpistpucg661.jpg "/>

During compilation, replace the add (I1, I2) and add (S1, S2) expressions with specific expressions. Macro polymorphism is actually implemented when a simple static polymorphism program is compiled, the macro will be replaced with a specific expression, according to the form parameters to identify the specific operation


3,Static Polymorphism:

Static polymorphism is essentially the presence of templates. After being processed by the compiler, two different functions run_vehicle () are obtained.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/4C/wKioL1QGxy-QCU01AAJN7d1gH3c515.jpg "Title =" .jpg "alt =" wKioL1QGxy-QCU01AAJN7d1gH3c515.jpg "/>

Design Philosophy: For related object types, their respective definitions are directly implemented without the need for base classes. It only specifies that the interface names with the same functions must be the same in the implementation of specific classes. In essence, static polymorphism is the implementation of templates. It is mainly implemented by templates, also known as compile-time polymorphism.


4,Dynamic Polymorphism: Based on the Inheritance Mechanism and virtual functions

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/4C/wKioL1QGx0bBtzfzAAKGD6anzCQ445.jpg "Title =" ..jpg "alt =" wkiol1qgx0bbtzfzaakgd6anzcq445.jpg "/>

Design Philosophy: For related object types, determine a common function set between them, and declare these common functions as multiple common virtual function interfaces in the base class. Each subclass overrides these virtual functions to complete specific functions. Dynamic polymorphism is based on the Inheritance Mechanism and virtual functions, also known as Runtime polymorphism.


Implementation principle of virtual functionsBlock diagram:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/48/4B/wKioL1QGw_uBkahWAAEuljQsC3o414.jpg "Title =" .jpg "alt =" wkiol1qgw_ubkahwaaeuljqsc3o414.jpg "/>

Key points: virtual functions

Static Association: it is used for association during compilation;

Dynamic binding: with virtual functions, the compiler generates code that selects the correct virtual method when running the program.

 

Static and dynamic comparison:

Differences:

1. the nature is different. Static polymorphism is determined during the compilation period and is implemented by the template. Dynamic polymorphism is determined during the runtime and implemented by inheritance and virtual functions;

2. In dynamic polymorphism, the interface is explicit, with the function signature as the center, the polymorphism is implemented through the virtual function at runtime, and the static multi-platform interface is implicit, with the valid expression as the center, static polymorphism is completed by the template with the current compilation period.

Similarities:

1. All can achieve polymorphism, static polymorphism-polymorphism during compilation, dynamic polymorphism-polymorphism during runtime;

2. Both interfaces and implementations can be separated. One is the template-defined interface, the type parameter definition implementation, and the other is the basic class virtual function-defined interface, and the inheritance class is responsible for implementation.

 

 

 


Polymorphism-polymorphism (function polymorphism, macro polymorphism, static polymorphism, dynamic polymorphism)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.