The role of virtual functions in C ++

I went to the interview yesterday, and the interviewer asked: What does the virtual function do? I did n’t explain it for a long time. In fact, I still do n’t understand it enough, or I do n’t understand it. Even the subclasses rewrite the normal functions and subclasses I don't know the difference between the virtual functions of the class rewriting the parent class, it is really weak! ! 1

 

The role of virtual functions: explained by the following two procedures!

You can generalize the operation of member functions. When you use the pointer of the base class to point to an object of a different derived class, the 
base class pointer calls its virtual member function, and the member function that actually points to the object is called 
instead of the member defined in the base class Function (as long as the derived class rewrites the member function). 
If it were a virtual function, the derived class object regardless of which base class pointer, when calls are 
calls that function in the base class definition.

Procedure 1:

#include <iostream>
using namecpace std;
class B0 //基类B0声明

public:

 void display(){cout<<"B0::display()"<<endl;} //公有成员函数
};
class B1: public B0 
{
 public:
  void display(){cout<<"B1::display()"<<endl;} 
};
class D1: public B1 
{
public:
  void display(){cout<<"D1::display()"<<endl;} 
};
void fun(B0 *ptr) 
{

 ptr-> display (); // "object pointer-> member name"  

}
void main () // Main function

B0 b0; // Declare B0 class object
 B1 b1; // Declare B1 class object
 D1 d1; // Declare D1 class object
 B0 * p; // Declare B0 class pointer
 p = & b0; // B0 class pointer points to B0 class object
 fun (p);
 p = & b1; // B0 pointer points to B1 object
 fun (p);
 p = & d1; // B0 pointer points to D1 object
 fun (p);
}
Run result:
B0 :: display ( )
B0 :: display ()
B0 :: display ()

Program 2:
#include <iostream>
using namespace std;
class B0 // Base class B0 declaration
{public: // External interface
 virtual void display () // Virtual member function
     {cout << "B0 :: display ()" < <endl;} 
};
class B1: public B0 // public derivation
{public:
       void display () {cout << "B1 :: display ()" << endl;}
};
class D1: public B1 // public derivation
{public:
   void display () {cout << "D1 :: display ()" << endl;}
};
void fun (B0 * ptr) // Ordinary function
{ptr-> display ();}
void main () // Main function
{B0 b0, * p; // Declare base class object and pointer
 B1 b1; // Declare derived class object
 D1 d1; // Declare derived class object
 p = & b0;
 fun (p);// Call the base class B0 function member
 p = & b1;
 fun (p); // Call the function member of the derived class B1
 p = & d1;
 fun (p); // Call the function member of the derived class D1
}
Result of operation:
B0 :: display ()
B1 :: display ()
D1 :: display ()

Virtual functions are the basis of dynamic binding.
Is a non-static member function.
In the declaration of the class, write virtual before the function prototype.
virtual is only used to describe the prototype in the class declaration, and cannot be used in function implementation.
With inheritance, virtual functions are declared in the base class, regardless of whether they are specified in the derived class, the same prototype functions are automatically virtual functions.
The essence: not overloading the declaration but covering.
Calling method: Through the base class pointer or reference, it will decide which function to call according to the class of the object pointed to by the pointer during execution.

It's not finished yet. In fact, you can create a subclass object directly to call a subclass's ordinary function without using a pointer of the superclass type. Why do you have to use a virtual function?

Although this is said, it is not the case in the actual development process. When we use some libraries and frameworks, these libraries and frameworks are written in advance. When we use it, we cannot directly modify the source code of the class library. We can only derive the classes in the class library to cover some member functions to achieve our functions, but some of these member functions are called by the framework. In this case, using virtual functions is a good way.

 

=====================================================

10. Polymorphism refers to different implementation actions when the same object receives different messages or different objects receive the same message. C ++ supports two types of polymorphism: compile-time polymorphism and runtime polymorphism.
a. Compile-time polymorphism: through overloaded functions.
b Run-time polymorphism: through virtual functions.

 

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 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"
 virtual void funtion1 () = 0 after the function prototype

Published 115 original articles · Like 29 · Visitors 50,000+

Guess you like

Origin blog.csdn.net/huabiaochen/article/details/100576503