[C++] Virtual function and pure virtual function

In C++, all data members and methods are private by default, and their properties can be modified with the keyword public.

When we cannot or do not want to implement a method of the parent class, we can declare the function of the parent class as a virtual function, but the parent class can still implement this function. This is done to allow the pointer of the base class to call this subclass function.

However, if a function of the parent class is declared as a pure virtual function, this function cannot be implemented and is marked with =0. And this class becomes an abstract class and cannot be instantiated.

The purpose of defining pure virtual function is to play a normative role, and it is stipulated that derived classes of this class must implement this function.

Why must the destructor of the base class be a virtual function?

In order to ensure that the destructor of the subclass at the end of the inheritance relationship is called correctly. In other words, the destructor needs to be polymorphic.

http://blog.csdn.net/hackbuteer1/article/details/7558868

First of all: 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.
Defining him as a virtual function is to allow the pointer of the base class to call this function of the subclass.
Defining a function as a pure virtual function means that the function has not been implemented.

The purpose of defining a pure virtual function is to implement an interface and act as a specification. Programmers who inherit this class from the specification must implement this function.
1. Introduction
Suppose we have the following class hierarchy:

[cpp]  view plain  copy
  1. class A  
  2. {  
  3. public:  
  4.     virtual void 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!  
  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 lies in the so-called "deferred binding" or "dynamic binding". The call of a class function is not determined at compile time, but at runtime. When writing the code, it cannot be determined whether the function of the base class or the function of which derived class is called, so it is called a "virtual" function.
    Virtual functions can only achieve the effect of polymorphism by means of pointers or references.

C++ pure virtual function
1. Definition
 Pure virtual function is a virtual function declared in the base class. It is not defined in the base class, but it requires any derived class to define its own implementation method. The way to implement pure virtual functions in the base class is to add "=0" after the function prototype.
 virtual void funtion1()=0
2. Reasons for introduction
  1. In order to facilitate the use of polymorphic 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, as a base class, animals can derive subclasses such as tigers and peacocks, but the animals themselves generate objects that are obviously unreasonable.
  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;), then the compiler requires that it must be rewritten in the derived class to achieve polymorphism . At the same time, a class containing pure virtual functions is called an abstract class, and it cannot generate objects . This solves the above two problems well.

The class that declares the pure virtual function is an abstract class. Therefore, users cannot create instances of the class, only instances of its derived classes.
The most notable feature of pure virtual functions is that they must redeclare the function in the inherited class (do not follow = 0, otherwise the derived class cannot be instantiated), and they are often not defined in abstract classes.
The purpose of defining pure virtual functions is to make the derived class just inherit the function interface.
纯虚函数的意义,让所有的类对象(主要是派生类对象)都可以执行纯虚函数的动作,但类无法为纯虚函数提供一个合理的缺省实现。所以类纯虚函数的声明就是在告诉子类的设计者,“你必须提供一个纯虚函数的实现,但我不知道你会怎样实现它”。


抽象类的介绍
抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层。
(1)抽象类的定义:  称带有纯虚函数的类为抽象类。
(2)抽象类的作用:
抽象类的主要作用是将有关的操作作为结果接口组织在一个继承层次结构中,由它来为派生类提供一个公共的根,派生类将具体实现在其基类中作为接口的操作。所以派生类实际上刻画了一组子类的操作接口的通用语义,这些语义也传给子类,子类可以具体实现这些语义,也可以再将这些语义传给自己的子类。
(3)使用抽象类时注意:

•   抽象类只能作为基类来使用,其纯虚函数的实现由派生类给出。如果派生类中没有重新定义纯虚函数,而只是继承基类的纯虚函数,则这个派生类仍然还是一个抽象类。如果派生类中给出了基类纯虚函数的实现,则该派生类就不再是抽象类了,它是一个可以建立对象的具体的类。
•   抽象类是不能定义对象的。


总结:

1、纯虚函数声明如下: virtual void funtion1()=0; 纯虚函数一定没有定义,纯虚函数用来规范派生类的行为,即接口。包含纯虚函数的类是抽象类,抽象类不能定义实例,但可以声明指向实现该抽象类的具体类的指针或引用。
2、虚函数声明如下:virtual ReturnType FunctionName(Parameter);虚函数必须实现,如果不实现,编译器将报错,错误提示为:
error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"
3. For virtual functions, the parent class and the subclass have their own versions. Dynamic binding when called by polymorphism.
4. A subclass of pure virtual function is realized. The pure virtual function is programmed with a virtual function in the subclass. The subclass of the subclass, the grandchild class, can override the virtual function and bind dynamically when called by polymorphism.
5. Virtual function is a mechanism used to realize polymorphism in C++. The core idea is to access the functions defined by the derived class through the base class.
6. When there is a dynamic allocation of 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. But you can solve the virtual problem of friends by letting the friend function call the virtual member function.
8. The destructor should be a virtual function and will call the destructor of the corresponding object type. 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 but can only be derived. The pure virtual function of his derived class has not been rewritten, so its derived class is still an abstract class.
The purpose of defining pure virtual functions is to make the base class not instantiable,
because instantiation of such an abstract data structure itself does not make sense.
Or it makes no sense to give an implementation.
In fact, I personally think that the introduction of pure virtual functions is for two purposes.
1. For safety, to avoid any unknown results that need to be clear but caused by carelessness, and remind subclasses to do it What should be done.
2. For efficiency, it is not the efficiency of program execution, but the efficiency of coding.

Guess you like

Origin blog.csdn.net/michellechouu/article/details/50704625