On object-oriented polymorphism and C + + practice

Source: Internet
Author: User

Polymorphic Properties:Mentioning object-oriented, it is natural to think of three major features:encapsulation, inheritance, polymorphism。 Their directories are: 1. Encapsulation, which enables the code to modularize the internal structure and state of the package. 2. Inheritance, used to extend the original code.3. polymorphic, convenient interface reuse, through the same interface and incoming objects invoke the implementation of different objects. Polymorphism at run-time binding functions, but not polymorphism at compile time has determined the function call address.
What are the benefits of polymorphism from an architectural design perspective? Reverse the source and run dependencies. Usually one function in the system calls another function, whether it is aRun-timeStill isin the source codeare dependent on the same direction, that is, the calling module relies on the called module. Polymorphism makes the source-dependent direction reverse, and the calling module no longer relies on the called module, which relies on only one polymorphic interface. This is how the plug-in works by injecting the dependent called module into the calling module through control inversion and dependency injection. In engineering practice, the plug-in architecture is robust, which makesstable, high-value business rules no longer rely on low-value, easily changing modules。
Conclusion: The robust business system architecture needs to use polymorphism.
C + + on the engineering practice of polymorphism: in the C + + language, two kinds of polymorphism are supported, that is, compile-time polymorphism (implemented by overloaded functions) and run-time polymorphism (implemented by virtual functions). This paper mainly discusses the practice of polymorphism at runtime.The most common use is to declare a base-class pointer, and then use that pointer to point to any subclass object and invoke the virtual function implementation of the corresponding subclass object. There are two different ways to do this:
1. Virtual Functions A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement a dynamic overwrite (override) of a member function.
2. Abstract classclasses that contain pure virtual functions (function declaration shapes such as:virtual void funtion () =0 ) are called abstract classes. Pure virtual function is a virtual function declared in the base class, because it is not defined in the base class, it is required that any derived class should define its own implementation method, and thus achieve the purpose of realizing polymorphism, and often use abstract class as interface in engineering practice.

On object-oriented polymorphism and C + + practice

Related Article

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.