In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, Xiaobian will bring you about how to achieve C++ polymorphism through functions. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
C++ polymorphism is mainly reflected in the call of virtual functions. In C++, the call of virtual functions uses dynamic binding, which is also called late binding, that is, the function to be called is decided at the time of program execution. Even if the programmer does not know the object type, the program can still make appropriate behavior for the object type.
In simple terms, virtual functions are defined by adding the virtual keyword to the function prototype. Once a function is declared virtual, it is still virtual from that point in the inheritance hierarchy, even if the class did not declare it virtual when overriding it. If a function in a base class is declared virtual and has different implementations in several subclasses, then we can use pointers or references to the base class to indicate the subclass object and call the function of the corresponding subclass when creating different objects of several subclasses.
Virtual functions are defined as follows:
Static member functions of a class cannot be defined as virtual functions;
The constructor of a class cannot be defined as a virtual function;
Non-class functions cannot be defined as virtual functions.
A pure virtual function is a function initialized to 0 when it is declared.
#include #include using namespace std; Virtual void print() const = 0;
abstract class
Simply put, a class with one or more unimplemented purely virtual functions is an abstract class. If a class inherits from an abstract class, but it does not implement all the pure virtual functions in the parent class, then those pure virtual functions are still pure virtual in the subclass, and the subclass is still abstract and still cannot instantiate objects.
An abstract class is a special class that can only be used as a base class, with pure function implementations given by derived classes. Although abstract classes cannot be instantiated, we can declare pointers and references to an abstract class that can be implemented in different subclasses when instantiating objects, thus implementing polymorphic operations. A derived class ceases to be abstract only if it implements all the purely virtual functions in its base class.
C++ polymorphism
Broadly speaking, C++ polymorphism refers to the ability of a program to handle multiple types of objects. In C++ language, polymorphism can be implemented in many forms, such as forced polymorphism, overload polymorphism, type parameterization polymorphism, and inclusion polymorphism. Polymorphism is the inheritance of classes that allows the same function to respond differently depending on the type of object it is called on. It also inherits and overloading the three object-oriented programming features that together make up it.
C++ polymorphism is implemented by virtual functions. When we call virtual functions with pointers to base classes, the program chooses its own function implementation based on the properties of the object. Even if the programmer does not know the object type, the program can still behave appropriately for that object type.
The C++ language supports two types of polymorphism: one at compile time (static polymorphism) and one at runtime (dynamic polymorphism). Polymorphism at compile time is achieved through static binding, while polymorphism at run time is achieved through dynamic binding.
The above is how to achieve C++ polymorphism through functions shared by Xiaobian for everyone. If there are similar doubts, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
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.