In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to understand C++ polymorphism and virtual functions, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
The reason for deriving a class is not always to inherit or add new members, but sometimes to redefine the members of the base class so that the base class members are "reborn". The real power of object-oriented programming is not only to inherit, but also to allow derived class objects to be treated like base class objects, and its core mechanism is polymorphism and dynamic binding.
(1) Polymorphism
Polymorphism refers to different behaviors when the same message is received by different objects. The so-called message refers to the calls to the class member functions, and different behaviors refer to different implementations, that is, different functions are called.
1) Classification of polymorphisms
In a broad sense, polymorphism refers to the ability of a program to deal with multiple types of objects. In C++, this polymorphism can be done through overloading polymorphism (function and operator overloading), forced overloading (type casting), and type parameterized polymorphism (templates).
Including four ways to implement polymorphism (inheritance and virtual functions). Type parameterized polymorphism and inclusion polymorphism are called general polymorphism, which are used to systematically describe a group of types that are semantically related; overload polymorphism and mandatory polymorphism are called special polymorphism, which are used to describe the relationship between semantically unrelated types.
Virtual functions are used in C++ to implement inclusion polymorphism. Virtual functions provide C++ with a more flexible polymorphic mechanism, which can only be determined when the program is running, so virtual functions are the essence of polymorphism, and classes that contain at least one virtual function are called polymorphic classes. Inclusion polymorphism is frequently used in object-oriented programming.
2) static binding
Binding, also known as binding, is the process of merging modules or functions together to generate executable code, assigning a memory address to each module or function and providing the correct memory address for external access.
Binding a function implementation to a function call during the compilation phase is called static binding. Static binding must know all the information needed for the execution of all functions and modules during the compilation phase, and its choice of functions is based on the type of pointer (or reference) to the object. In C language, all links are static; in C++, static links are also common.
Class Point {public: void area () {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.