C ++ polymorphism is mainly reflected in virtual function calls. In C ++, virtual function calls use dynamic binding, and some people say it is late binding, that is, the function that is called is determined only when the program runs. Even if the programmer does not know the object type, the program can still make a behavior suitable for this object type.
Simply put, a virtual function is to add the virtual keyword before the function prototype. Once a function is declared as a virtual function, it is still a virtual function even if the class does not declare it as a virtual function when it is rewritten. If a function in the base class is declared as a virtual function and has different implementations in several subclasses, when we create different objects in several subclasses, you can use a base class pointer or reference to specify the subclass object and call the function of the corresponding subclass.
The virtual function is defined as follows:
◆ Static member functions of the class cannot be defined as virtual functions;
◆ Class constructors cannot be defined as virtual functions;
◆ Non-class functions cannot be defined as virtual functions.
A pure virtual function is a function initialized to 0 when a virtual function is declared.
- #include<memory>
- #include<iostream>
- using namespace std;
- Virtual void print() const = 0;
Abstract class
In short, classes with one or more unimplemented pure virtual functions are abstract classes. If a class inherits from an abstract class but does not fully implement all pure virtual functions in the parent class, those pure virtual functions are still pure virtual in the subclass, this subclass is still an abstract class and cannot be instantiated.
An abstract class is a special class that can only be used as a base class. Its pure function implementation is provided by the derived class. Although the abstract class cannot be instantiated, we can declare the pointer and reference of an abstract class. when instantiating an object, we can use different subclasses to implement the multi-polymorphism operation. Only when the derived class implements all pure virtual functions in the base class, it is no longer an abstract class.
C ++ Polymorphism
Broadly speaking, C ++ polymorphism refers to the ability of a program to process multiple types of objects. In C ++, polymorphism can be implemented through forced polymorphism, heavy load polymorphism, type parameterized polymorphism, and inclusion polymorphism. The so-called polymorphism means that through the inheritance of classes, the same function can make different responses based on the type of the called object. It also inherits and reloads to form three Object-Oriented Programming features.
C ++ polymorphism is implemented through virtual functions. When we call virtual functions using a base class pointer, the program selects its own function implementation based on the object attributes. Even if the programmer does not know the object type, the program can still make the action that suits the object type.
C ++ supports two types of polymorphism: one is the polymorphism static polymorphism at compile time) and the other is the polymorphism dynamic polymorphism at runtime ). Polymorphism during compilation is implemented through static association, while polymorphism during runtime is implemented through dynamic Association.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code