Distinguish between virtual and pure virtual functions:
(1). Virtual function is a function before the keyword virtual, the general definition format is:
Virtual type function name (parameter table)
{
function body
}
(2). The life format of the pure virtual function is:
Virtual type function name (parameter table) = 0;
That is, there is no function body.
(3). Classes with pure virtual functions are abstract classes:
Cases:
Class name
{
Virtual type function name (parameter table) = 0;
} is an abstract class.
About Abstract Classes:
(1). An abstract class cannot define an object.
(2). Abstract classes can only be used as base classes.
(3). Constructors cannot be pure virtual functions, destructors can be pure virtual functions.
About virtual functions:
(1). Only member functions of a class can be described as virtual functions, because virtual functions only apply to
The class object of the inheritance relationship, so normal cannot be described as a virtual function.
(2). An inline function cannot be a virtual function, because an inline function determines its location at compile time.
(3). The constructor cannot be a virtual function, because the object is still an amorphous space when constructed.
(4). Destructors can be virtual functions, and they are usually declared as virtual functions.
Summarize:
(1). When a member function is defined as a virtual function in a base class, the virtual function defined in its derived class
Must have the same name as a virtual function in the base class, and the type, order, and number of arguments must correspond to one by one.
(2). To achieve this dynamic polymorphism, you must use a pointer variable or reference of the base class type to make the
Pointers point to objects of different derived classes and can achieve dynamic polymorphism by invoking a virtual function that the pointer refers to.
Basic steps to achieve polymorphism:
(1). In the base class, define member functions as virtual functions (virtual);
(2). Defines the public (common) derived class of the base class.
(3). "Overloads" the virtual function in the public derivation of the base class.
(4). Defines a pointer variable that points to the base class, which points to the object of the base class's public derived class.
Note: Overloading a virtual function is not a generic overloaded function, it requires a function name, return type, number of arguments
The parameter types and order are exactly the same.
C + + polymorphism and other knowledge points