Difference between virtual function and pure virtual function

First: Emphasize that a concept
defines a function as a virtual function, which does not mean that the function is a function that is not implemented.
It is defined as a virtual function to allow the function of the subclass to be called with the base class pointer.
Defining a function as a pure virtual function means that the function is not implemented.

Defining a pure virtual function is to implement an interface and play a normative role. Programmers who standardize this class must implement this function.
1. Introduction
Suppose we have the following class hierarchy:
  1. class A  
  2. {  
  3. public:  
  4.     virtualvoid foo()   
  5.     {  
  6.         cout<<"A::foo() is called"<<endl;  
  7.     }  
  8. };  
  9. class B:public A  
  10. {  
  11. public:  
  12.     void foo()  
  13.     {  
  14.         cout<<"B::foo() is called"<<endl;  
  15.     }  
  16. };  
  17. int main(void)  
  18. {  
  19.     A *a = new B();  
  20.     a->foo();    // Here, although a is a pointer to A, the called function (foo) is B's!  
  21.     return 0;  
  22. }  
     This example is a typical application of virtual functions. Through this example, you may have some concepts about virtual functions. It is virtual in the so-called "deferred binding" or "dynamic binding", the invocation of a class function is not determined at compile time, but is determined at run time. Since it is not possible to determine whether the function of the base class or the function of the derived class is called when writing the code, it is called a "virtual" function.
    Virtual functions can only be polymorphic by means of pointers or references.

C++ pure virtual function
1. Definition A
 pure virtual function is a virtual function declared in the base class. It is not defined in the base class, but any derived class is required to define its own implementation method. The way to implement a pure virtual function in the base class is to add "=0" after the function prototype
 virtual void funtion1()=0
Second, the introduction reason
  1. In order to facilitate the use of polymorphism, 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 the object. For example, animals as a base class can derive subclasses such as tigers and peacocks, but it is obviously unreasonable for animals to generate objects by themselves.
  In order to solve the above problems, the concept of pure virtual function is introduced, and the function is defined as a pure virtual function (method: virtual ReturnType Function()= 0;), the compiler requires that it must be rewritten in the derived class to achieve polymorphism . A class that also contains pure virtual functions is called an abstract class, which cannot generate objects . This solves the above two problems very well.

A class that declares a pure virtual function is an abstract class. Therefore, users cannot create instances of a class, only instances of its derived classes.
The most notable characteristics of pure virtual functions are that they must be redeclared in the inherited class (without the trailing = 0, otherwise the derived class cannot be instantiated), and they are often not defined in the abstract class.
The purpose of defining pure virtual functions is to make derived classes just inherit the interface of the function.
The meaning of pure virtual function allows all class objects (mainly derived class objects) to perform the actions of pure virtual functions, but classes cannot provide a reasonable default implementation for pure virtual functions. So the declaration of a class pure virtual is telling the designer of the subclass, "You must provide an implementation of a pure virtual function, but I don't know how you would implement it".


Introduction to
abstract classes An abstract class is a special class that is established for abstraction and design purposes, and is at the upper level of the inheritance hierarchy.
(1) Definition of abstract class: A class with pure virtual functions is called an abstract class.
(2) The role of
abstract classes: the main role of abstract classes is to organize related operations as result interfaces in an inheritance hierarchy, which provides a common root for derived classes, which will be implemented in its base class. in the operation as an interface. Therefore, derived classes actually describe the general semantics of a set of subclasses' operational interfaces, and these semantics are also passed to subclasses. Subclasses can implement these semantics specifically, or they can pass these semantics to their own subclasses.
(3) Note when using abstract classes:

• Abstract classes can only be used as base classes, and the implementation of their pure virtual functions is given by derived classes. If the derived class does not redefine the pure virtual function, but only inherits the pure virtual function of the base class, the derived class is still an abstract class. If the implementation of the base class pure virtual function is given in the derived class, the derived class is no longer an abstract class, it is a concrete class that can create objects.
• Abstract classes cannot define objects.


Summary:

1. The pure virtual function is declared as follows: virtual void funtion1()=0 ; The pure virtual function must not be defined. The pure virtual function is used to standardize the behavior of the derived class, that is, the interface. A class containing pure virtual functions is an abstract class, an abstract class cannot define an instance, but can declare a pointer or reference to a concrete class that implements the abstract class.
2. The virtual function is declared as follows: virtual ReturnType FunctionName(Parameter); the virtual function must be implemented. If it is not implemented, the compiler will report an error, and the error message is:
error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"
3. For virtual functions, both parent and child classes have their own versions. Dynamic binding when called by polymorphism.
4. A subclass of pure virtual function is implemented. The pure virtual function is programmed with a virtual function in the subclass. The subclass of the subclass, that is, the grandchild class, can override the virtual function and dynamically bind it when it is called by polymorphism.
5. Virtual functions are the mechanism used to implement polymorphism in C++. The core idea is to access functions defined by derived classes through the base class.
6. When there is dynamically allocated memory on the heap, the destructor must be a virtual function, but it is not necessary to be pure virtual.
7. A friend is not a member function. Only a member function can be virtual, so a friend cannot be a virtual function. But the virtual problem of friends can be solved by having the friend function call a virtual member function.
8. The destructor should be a virtual function, and the destructor of the corresponding object type will be called. Therefore, if the pointer points to a subclass object, the destructor of the subclass will be called, and then the destructor of the base class will be called automatically.

A class with pure virtual functions is an abstract class and cannot generate objects, only derivation. The pure virtual function of his derived class has not been rewritten, so its derived class is still an abstract class.
Defining pure virtual functions is to make the base class uninstantiable
because instantiating such an abstract data structure doesn't make sense in itself.
Or it doesn't make sense to give the implementation.
In fact, I personally think that the introduction of pure virtual functions is for two purposes
. 1. For safety, because to avoid any unknown results that need to be clear but caused by carelessness, remind subclasses to do it should be done.
2. For efficiency, not the efficiency of program execution, but the efficiency of coding.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324678083&siteId=291194637