6, polymorphism-3, virtual function

Source: Internet
Author: User

Virtual functions are the basis of dynamic binding. Virtual function must be non-static member function, after the virtual function is derived, in the class family can realize the polymorphism in the running process.

Depending on the type compatibility rule, you can use the object of the derived class instead of the base class object. If you use a pointer to a base class type to point to a derived class object, you can access the object through this pointer, and the problem is access to only the members of the same name inherited from the base class. The solution to this problem is that if you need a pointer to a base class to point to an object of the derived class and access a member with the same name as the base class, you first describe the same name function as a virtual function in the base class. Thus, by using pointers of the base class type, different objects belonging to different derived classes can be produced with different behaviors, thus realizing the polymorphism of the running process.

1. General virtual function members

The declaration syntax for a general virtual function member is:

virtual function type function name (formal parameter list)

{

function body

}

A virtual function declaration can only appear in a function prototype declaration in a class definition, not when a member function is implemented.

In the process of running the polymorphism needs to meet three conditions, the first class satisfies the type compatibility rule, the second is to declare the virtual function, the third is to be called by the member function or through pointers, references to access the virtual function. If you use object names to access virtual functions, the bindings can be made (statically bound) during compilation, without having to run the process.

Example: virtual function member; When you use a pointer to a base class type, it points to the object of the derived class, and you can access which object has a virtual member function with the same name.

#include <iostream>
using namespace Std;
Class B0
{
Public
virtual void display () {cout<< "B0::d isplay ()" <<ENDL;} Virtual member function
};
Class B1:public B0
{
Public
virtual void display () {cout<< "B1::d isplay ()" <<ENDL;} Virtual member function
};
Class D1:public B1
{
Public
virtual void display () {cout<< "D1::d isplay ()" <<ENDL;} Virtual member function
};
void Fun (B0 *ptr)
{
Ptr->display ();
}
int main ()
{
B0 b0,*p;
B1 B1;
D1 D1;
p=&b0;
Fun (p);
p=&b1;
Fun (p);
p=&d1;
Fun (p);
GetChar ();
}

The class B0, B1, D1 in the program belong to the same class family, and are derived from the public, thus satisfying the type compatibility rules, the function member display () of the base class B0 is declared as a virtual function, the program uses the object pointer to access the function member, so that the binding process is done in the run, It realizes the polymorphism in operation. A pointer to a base class type gives you access to the members of the object that you are pointing to, so that you can handle objects in a uniform class family in a uniform way, with a higher degree of abstraction and a simpler and more efficient program.

In this program, the derived class does not explicitly give a virtual function declaration, and the system follows the following rules to determine whether a function member of a derived class is a virtual function:

A, whether the function has the same name as the virtual function of the base class;

b, whether the function has the same number of arguments as the virtual function of the base class and the same corresponding parameter type.

c, whether the function has the same return value as the virtual function of the base class, or a pointer that satisfies the type compatibility rule, the return value of the reference type.

If a function of a derived class satisfies the above criteria, it is automatically determined to be a virtual function if it is checked from the name, parameter, and three aspects of the return value. At this point, the virtual function of the derived class overrides the virtual function of the base class. In addition, virtual functions in derived classes also hide all other overloaded forms of functions with the same name in the base class.

Only virtual functions are dynamically bound, and if the derived class needs to modify the behavior of the base class (that is, to override a function with the same name as the base class function), the corresponding function should be declared as a virtual function in the base class. The non-virtual functions declared in the base class typically represent functions that do not want to be changed by the derived class, and are not polymorphic.

When overriding inherited virtual functions, never redefine different values if the function has default parameter values. The reason is that although virtual functions are dynamically bound, the default parameter values are statically bound. That is, a virtual function of a derived class can be accessed through a pointer to a base class that points to a derived class object, but the default parameter value can only be derived from the definition of the base class .

6, polymorphism-3, virtual function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.