A deep analysis of virtual function and pure virtual function (the difference between C + + and Java virtual function) _c language

Source: Internet
Author: User

C++
virtual function
1. Definition: A member function declared as virtual in a base class and redefined in one or more derived classes [1]
2. Syntax: virtual function return type function name (parameter table) {function Body}
3. Purpose: To achieve polymorphism, by pointing to the base class pointer of the derived class, accessing the overridden member function of the same name in the derived class, that is, allowing subclasses to override the same method as the parent class.
The function of virtual function is to realize dynamic interlock, which is to select the appropriate member function dynamically in the running phase of the program. After you define a virtual function, you can redefine the virtual function in the derived class of the base class, and the function redefined in the derived class should have the same number of parameters and formal parameter types (that is, exactly the same method) as the virtual function. cannot be just the same as the function name. )。 To achieve a unified interface, a different definition process. If a virtual function is not redefined in a derived class, it inherits the virtual function of its base class.

When the program discovers the virtual function name, it will automatically be used as a dynamic binder, which is to select the appropriate member function dynamically when the program is run. Virtual functions are a manifestation of C + + polymorphism. The dynamic binder stipulates that a virtual function can be invoked only by referring to a pointer to a base class or a reference to a base class object, in the form of:
1. Pointer variable name to base class-> virtual function name (argument list)
2. The reference name of the base class object. virtual function name (argument list)
Using virtual functions, we are flexible to dynamically bind, of course, at the expense of a certain cost. If the function (method) of the parent class is not necessary or cannot be implemented, to be completely dependent on subclasses, you can set this function (method) to virtual function name = 0, for example: virtual void fun () = 0, we call such a function (method) pure virtual functions. If a class contains pure virtual functions, this class is called an abstract class.
Summary: If a subclass wants to override the parent class's method, then the parent class's Member method must be virtual, i.e. the method must be a virtual function.

Java
in Java, all methods are virtual by default, so long as the method is not declared to be final, it is a virtual function, not a declaration for a method. In <core Java2:volum i> mentioned: "In Java, you don't need to declare a as virtual." Dynamic binding is the default behavior. If you don't want a, you are virtual, you tag it as final. So we found that in Java, subclasses can override the (override) method of the parent class, and the parent class does not declare virtual.

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.