Analysis of the difference between C++ virtual function and pure virtual Function

  • 2020-11-20 06:12:09
  • OfStack

First: Emphasize one concept

Define a function as a virtual function. It does not mean that the function is not implemented.

It is defined as a virtual function to allow the function of a subclass to be called with a pointer to the base class.

Define a function as a pure virtual function to mean that the function is not implemented.

Pure virtual functions are defined to implement an interface and act as a specification that must be implemented by the programmer whose specification inherits the class.

Introduction to the

Suppose we have the following class hierarchy:


class A
{
public:
  virtual void foo()
  {
    cout<<"A::foo() is called"<<endl;
  }
};
class B:public A
{
public:
  void foo()
  {
    cout<<"B::foo() is called"<<endl;
  }
};
int main(void)
{
  A *a = new B();
  a->foo();  //  Here, a Although pointing to A Pointer to, but the function being called (foo) It is B the !
  return 0;
}

This example is a typical application of virtual functions, so maybe you have some idea of virtual functions. It is essentially called "delayed linking" or "dynamic linking", where the invocation of a class function is determined not at compile time, but at run time. It is called a "virtual" function because you cannot determine whether the function of the base class or the derived class is being called when you write code.

Virtual functions can only be polymorphic by means of Pointers or references.

C++ pure virtual functions

Definition 1.

A pure virtual function is a virtual function declared in a base class. It is not defined in the base class, but any derived class is required to define its own implementation. The way to implement a pure virtual function in a base class is to add =0 after the function prototype:


virtual void funtion1()=0

2. Cause of introduction

1. To facilitate the use of polymorphism, we often need to define virtual functions in the base class.

2. In many cases, it is not reasonable for the base class itself to generate objects. For example, animals as a base class can be derived from tigers, peacocks, and other subclasses, but the animals themselves generate objects is obviously irrational.

In order to solve the above problems, the concept of pure virtual function is introduced and the function is defined as pure virtual function (method: virtual ReturnType Function()= 0;) , the compiler requires that it must be overridden in a derived class to implement polymorphism. Classes that also contain pure virtual functions are called abstract classes and cannot generate objects. This is a good solution to both of these problems.

The class that declares a pure virtual function is an abstract class. Therefore, a user cannot create an instance of a class, only an instance of its derived class.

The most striking feature of pure virtual functions is that they must be redeclared in an inherited class (without the = 0, otherwise the derived class cannot be instantiated), and they are often undefined in an abstract class.

The purpose of defining pure virtual functions is so that derived classes simply inherit the interface of the function.

The meaning of pure virtual functions is that all class objects (mainly derived class objects) can 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 function is telling the designer of the subclass, "You have to provide an implementation of a pure virtual function, but I don't know how you're going to implement it."

An introduction to abstract classes

An abstract class is a special kind of class that is created for abstract and design purposes and is at the upper level of the inheritance hierarchy.

(1) Definition of abstract classes: Classes with pure virtual functions are called abstract classes.

(2) Function of abstract class: The main function of abstract class is to organize the related operation as the result interface in an inheritance hierarchy, by which it provides a common root for derived class, and derived class will concretly implement the operation as the interface in its base class. So derived classes actually describe the generic semantics of the operating interfaces of a set of subclasses, which are passed on to subclasses, which can either implement the semantics or pass them on 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 pure virtual functions are not redefined in the derived class, but only pure virtual functions that inherit from the base class, then the derived class is still an abstract class. If a derived class is given an implementation of a base class pure virtual function, the derived class is no longer an abstract class, it is a concrete class that can build an object.
An abstract class cannot define an object.

Conclusion:

1. The pure virtual function is declared as follows: virtual void funtion1()=0 ; Pure virtual functions 1 are not defined and are used to regulate the behavior of derived classes, that is, interfaces. A class that contains pure virtual functions is an abstract class. An abstract class cannot define an instance, but it can declare a pointer or reference to a concrete class that implements the abstract class.

2. The virtual function declaration is as follows: virtual ReturnType FunctionName(Parameter) The virtual function must be implemented. If it is not, the compiler will report an error and the error message will be:

[

error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"

]

3. For virtual functions, there are versions of both parent and child classes. Dynamic binding when invoked by a polymorphic mode.

4. Realized the subclass of pure virtual function. The virtual function is programmed in the subclass.

5. Virtual function is a mechanism used in C++ to realize polymorphism (polymorphism). The core idea is to access functions defined by derived classes through base classes.

6. When there is dynamically allocated memory on the heap, the destructor must be virtual, but not necessarily pure virtual.

7, friends are not member functions, only member functions can be virtual, so friends can not be virtual functions. But you can solve the virtual friend problem by having the friend function call the virtual member function.

The destructor should be virtual and the destructor of the corresponding object type will be called, so if the pointer points to a subclass object, the subclass's destructor will be called, and then the base class's destructor will be automatically called.

Classes with pure virtual functions are abstract classes that cannot generate objects but can only derive. The pure virtual function of his derived class has not been overridden, so its derived class is still an abstract class.

Pure virtual functions are defined so that the base class cannot be instantiated, because it makes no sense to instantiate such an abstract data structure in itself, or to give an implementation.

In fact, I personally think that pure virtual functions were introduced for two purposes:

1. For the sake of safety, remind the subclasses to do what should be done to avoid any unknown result that needs to be specified but is caused by carelessness.
2. For efficiency, not for the efficiency of program execution, but for the efficiency of coding.

Above is C++ virtual functions and pure virtual functions of the difference between the detailed content, more about C++ virtual functions and pure virtual functions of the information please pay attention to other related articles on this site!


Related articles: