First: emphasize a concept
Defining a function as virtual does not mean the function is an unimplemented function.
Defining it as virtual is to allow using a base class pointer to call this function of the derived class.
Only defining a function as a pure virtual function means the function is not implemented.
Defining a pure virtual function is to implement an interface, serving as a specification, specifying that programmers who inherit this class must implement this function.
1. Introduction
Suppose we have the following class hierarchy:
Example
This example is a typical application of virtual functions. Through this example, perhaps you will gain some concept of virtual functions. It is virtual in the so-called "late binding" or "dynamic binding": the call to a class function is not determined at compile time, but at run time. Because when writing code, it cannot be determined whether the function being called is the base class function or which derived class's function, it is called a "virtual" function.
Virtual functions can only achieve polymorphism with the help of pointers or references.
C++ Pure Virtual Functions
1. Definition
A pure virtual function is a virtual function declared in the base class. It has no definition in the base class, but requires that any derived class define its own implementation method. The way to implement a pure virtual function in the base class is to add after the function prototype=0:
virtual void funtion1()=0
2. Reasons for introduction
1. To conveniently use polymorphism features, we often need to define virtual functions in the base class.
2. In many cases, it is unreasonable for the base class itself to generate objects. For example, an animal as a base class can derive subclasses such as tiger and peacock, but generating an object from animal itself is obviously unreasonable.
To solve the above problems, the concept of pure virtual functions is introduced, and the function is defined as a pure virtual function (method:virtual ReturnType Function()= 0;), then the compiler requires that it must be overridden in derived classes to achieve polymorphism. At the same time, a class containing pure virtual functions is called an abstract class, which cannot generate objects. This nicely solves the above two problems.
A class that declares a pure virtual function is an abstract class. Therefore, users cannot create instances of the class, but can only create instances of its derived classes.
The most notable characteristic of pure virtual functions is: they must redeclare the function in the inheriting class (do not include the following =0, otherwise the derived class also cannot be instantiated), and they often have no definition in the abstract class.
The purpose of defining pure virtual functions is to make derived classes only inherit the interface of the function.
The significance of pure virtual functions is to allow all class objects (mainly derived class objects) to perform the actions of the pure virtual function, but the class cannot provide a reasonable default implementation for the pure virtual function. Therefore, the declaration of a pure virtual function in a class is telling the designers of subclasses, "You must provide an implementation of the pure virtual function, but I don't know how you will implement it."
Introduction to Abstract Classes
An abstract class is a special class established for the purposes of abstraction and design. It is at a higher level of the inheritance hierarchy.
(1) Definition of an abstract class:A class with pure virtual functions is called an abstract class.
(2) Role of an abstract class:The main role of an abstract class is to organize related operations as interfaces in an inheritance hierarchy, so that it provides a common root for derived classes. Derived classes will concretely implement the operations that serve as interfaces in their base class. Therefore, a derived class actually describes the common semantics of a group of subclasses' operation interfaces. These semantics are also passed to subclasses, which can concretely implement these semantics or pass them on to their own subclasses.
(3) Notes when using abstract classes:
- An abstract class can only be used as a base class, and its pure virtual functions are implemented by derived classes. If a derived class does not redefine the pure virtual functions but only inherits the pure virtual functions from the base class, then this derived class is still an abstract class. If a derived class provides implementations for the base class's pure virtual functions, then this derived class is no longer an abstract class; it is a concrete class from which objects can be created. Abstract classes cannot define objects.
Summary:
-
1. A pure virtual function is declared as follows:virtual void funtion1()=0;A pure virtual function necessarily has no definition. Pure virtual functions are used to standardize the behavior of derived classes, i.e., the interface. A class containing pure virtual functions is an abstract class. An abstract class cannot define instances, but pointers or references to concrete classes that implement the abstract class can be declared.
-
2. A virtual function is declared as follows:virtual ReturnType FunctionName(Parameter)A virtual function must be implemented. If it is not implemented, the compiler will report an error, with the error message:
error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"
-
3. For virtual functions, both the parent class and the child class have their own versions. When called in a polymorphic way, they are dynamically bound.
-
4. In a subclass that implements a pure virtual function, that pure virtual function becomes a virtual function in the subclass. The subclass's subclass, i.e., the grandchild class, can override this virtual function, and it is dynamically bound when called in a polymorphic way.
-
5. Virtual functions are the mechanism in C++ used to implement polymorphism. The core idea is to access functions defined in derived classes through base classes.
-
6. When dynamically allocating memory on the heap, the destructor must be a virtual function, but it does not need to be pure virtual.
-
7. Friends are not member functions. Only member functions can be virtual, so friends cannot be virtual functions. However, the virtual problem of friends can be solved by having friend functions call virtual member functions.
-
8. The destructor should be a virtual function, so that the destructor of the corresponding object type will be called. Therefore, if the pointer points to a subclass object, the subclass's destructor will be called, and then the base class's destructor will be called automatically.
A class with pure virtual functions is an abstract class; it cannot generate objects and can only be derived from. If the pure virtual functions of its derived class have not been overridden, then its derived class is still an abstract class.
Defining pure virtual functions is to make the base class non-instantiable, because instantiating such an abstract data structure is itself meaningless, and providing an implementation is also meaningless.
Actually, in my personal opinion, the introduction of pure virtual functions serves two purposes:
- 1. For safety: to avoid unknown results caused by carelessness where explicit implementation is needed, reminding subclasses to implement what they should.
- 2. For efficiency: not the efficiency of program execution, but the efficiency of coding.
Original URL: https://blog.csdn.net/hackbuteer1/article/details/7558868