C + + Learning Summary of the four: virtual function and polymorphism

Source: Internet
Author: User
Tags abstract define function prototype inheritance reference

Polymorphism, together with the data encapsulation and inheritance mentioned above, constitutes the three important mechanisms of object-oriented programming.

1. Static and dynamic joint-edit

Because of the existence of a function overload, when a function called the same name occurs in a program, the compiler calls the code that performs which of the same name functions, depending on the parameter type and number of functions, the process of linking the invocation of a function to the appropriate function implementation code, called the binder. According to the different stages of the implementation of the joint, it can be divided into two kinds: static and dynamic joint.

Static link is to determine the corresponding relationship between a function call and the function implementation code in the program compiling stage, and then the corresponding function code is invoked according to the corresponding relation in the process of program running, and the corresponding relation is always kept unchanged in the process of program running.

The dynamic binder is not able to decide which of the same-named-called functions to perform at compile time, only to decide which class member functions to perform, depending on the type of object to be processed during the program's operation.

2. Polymorphism

Polymorphism refers to a phenomenon in which the same message is completely different when it is received by different objects of the class. The message described here is a call to a member function of a class.

The overload of a function can realize polymorphism, but the polymorphism here is realized by virtual function, and the virtual function must exist in the inheritance environment.

The C + + language supports two types of polymorphism: one is polymorphic at compile time (static polymorphism) and the other is polymorphic (dynamic polymorphism) at runtime. The polymorphism at compile time is realized by the static joint, while the polymorphism of the runtime is realized by the dynamic joint-coding.

3. virtual function

The method of declaring a virtual function is to add the keyword virtual before the prototype of the member function in the base class. The format is as follows:

Class Name {

......

Virtual type function name (parameter table);

......

};

When a member function of a class is declared as a virtual function, this means that the member function may have a different implementation in the derived class, that is, the function might need to define the same function in a derived class as its base class virtual function prototype.

Virtual function is the foundation of dynamic Link, when the pointer or reference method of the base class type points to different derived class objects, the system automatically chooses the appropriate function according to the different objects in the program running, thus realizes the Run-time polymorphism.

When an object is identified by a base class pointer or reference, and a member function is called, because the base class pointer can point to different derived class objects of the base class, there is a possibility that a dynamic binder is required, but whether the dynamic binder is used, and whether the call is a virtual function.

A virtual function can be redefined in one or more derived classes, but it requires that it be redefined in a derived class exactly the same as the function prototype in the base class, including the function name, the return value type, the number of parameters, and the order of the parameter types.

Only member functions of a class can be declared as virtual functions, but constructors of classes and global and static member functions cannot be declared as virtual functions.

4. To point to a public derived class object with a base class pointer

A pointer to a base class can naturally point to an object of its publicly derived class. However, because the type of the base class pointer itself does not change, the base class pointer can only access the base class part of the derived class.

5. Pure virtual functions and abstract classes

When you define a base class that expresses abstract concepts, you may sometimes be unable to give specific implementations of some member functions. At this point, you can declare these functions as pure virtual functions.

The declaration format for a pure-demand function is as follows:

Virtual type function name (parameter table) = 0;

A base class that declares a pure virtual function is used only for inheritance, as an interface, and the specific functionality is implemented in its derived classes.

A class that declares a pure virtual function, called an abstract class. Abstract classes can only be used as base classes to derive new classes, not to create objects.

Related articles

C + + Learning Summary bis: constructors and destructors

Http://www.bianceng.cn/Programming/cplus/200911/12302.htm

One of the C + + Learning Abstracts: Classes and objects

Http://www.bianceng.cn/Programming/cplus/200911/12301.htm

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.