The function and distinction of virtual function and pure virtual function in c++

  • 2020-04-02 02:35:16
  • OfStack

Virtual function in order to overload and polymorphic needs, is defined in the base class, even if the definition is empty, so in the subclass can be overridden can not write this function in the base class!

Pure virtual functions are undefined in base classes and must be implemented in subclasses, much like interface functions in Java!

Virtual functions

Reason for introduction: to facilitate the use of polymorphism, we often need to define virtual functions in the base class.


class Cman 
{ 
public: 
virtual void Eat(){ ... }; 
void Move(); 
private: 
}; 
class CChild : public CMan 
{ 
public: 
virtual void Eat(){ ... }; 
private: 
}; 
CMan m_man; 
CChild m_child; 
CMan *p ;//This is the essence of use, if you do not define a pointer to the base class to use, there is not much sense
p = &m_man ; 
p->Eat(); //Always call CMan's member function Eat, not CChild
p = &m_child; 
p->Eat(); //If the subclass implements (overrides) the method, the CChild's Eat function is always called
//CMan's Eat method is not called; If the subclass does not implement the function, CMan's Eat function is called
p->Move(); // There is no member function in the subclass, so it is called in the base class  

Pure virtual function

Reasons for introduction:

1. The same as "virtual function";
2. In many cases, it is unreasonable for the base class itself to generate objects. For example, animals as a base class can be derived from tigers, peacocks, and so on, but the animals themselves generate objects that are clearly irrational.

Pure virtual function is the base class only defined the function body, no implementation process, such as: virtual void Eat() = 0; Do not define in CPP; Pure virtual function is equivalent to interface, can not directly instance, need derived class to implement function definition;

Some of you might be thinking, what's the point of defining this, and I think it's useful, if you want to describe some property of something to someone else, but you don't want to implement it, you can define it as a pure virtual function. Let me be clear. For example, building, you are the boss, you give the construction company to describe the characteristics of your building, how many floors, the roof to have a garden or something, the construction company can according to your method to achieve, if you do not clarify these, may not understand the construction company you need the characteristics of the building. You can do a good job with the pure demand function

Virtual and pure virtual functions

Idea 1:

If the class is declared virtual, the function is implemented, even if it is null, in order for the function to be overloaded in its subclasses, so that the compiler can use late binding to achieve polymorphism

A pure virtual function is just an interface, a declaration of a function, which is left to a subclass to implement.


class A{
protected:
void foo();//Ordinary class function
virtual void foo1();//Virtual functions
virtual void foo2() = 0;// pure Virtual functions
}

Idea 2:

Virtual functions can also be unloaded in subclasses; But pure virtual must be implemented in a subclass, just like a Java interface. It's often a good practice to add virtual to many functions, sacrificing some performance but increasing object-oriented polymorphism, because it's hard to anticipate that the function in the parent class is not in the subclass without modifying its implementation

Idea 3:

Classes of virtual functions are used for "implementation inheritance", which inherits the implementation of the parent class as well as the interface. Of course, we can also complete our own implementation. Classes of pure virtual functions are used for "interface inheritance", mainly for communication protocols. The focus is on the unity of the interface, and the implementation is done by subclasses. In general, there are only pure virtual functions in an interface class.

Point four:

A class with a pure virtual function is called a virtual base class. This base class cannot generate an object directly, but can only be used if it is inherited and its virtual function is overridden. Such classes are also called abstract classes.
Virtual functions are designed to inherit interfaces and default behavior
Pure virtual functions simply inherit the interface, and the behavior must be redefined


Related articles: