What are the different ways of C + + polymorphism?

Source: Internet
Author: User

C + + polymorphic mode:

(1) Static polymorphic (heavy, template)

The type of the calling function is determined at compile time .

(2) Dynamic polymorphism (overlay, virtual function implementation)

At run time , determine which function is called and bind dynamically. Run the base class pointer to the object of the derived class and invoke the function of the derived class.

virtual function Implementation principle: virtual function table and virtual function pointer.

Pure virtual function: Virtual int fun () = 0;

Introduction to Polymorphic Basics:

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

First, what is polymorphic (POLYMORPHISN)? Literally means "multiple shapes." I have not found a description of a polymorphic theoretical concept in the book on hand.

For the moment, quote Charlie Calverts's description of polymorphism--polymorphism is a technique that allows you to set a parent object to be equal to one or more of his sub-objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it (excerpt from "Delphi4 Programming Technology Insider ").

To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type. Polymorphism is implemented in Object Pascal and C + + through virtual functions (virtual function).

OK, then the "virtual function" (or "virtual method"). A virtual function is a member function that is allowed to be redefined by its child class. and subclasses redefine the practice of parent virtual functions, called Overrides, or overrides.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////

Here's a concept that beginners often confuse. Overwrite (override) and reload (overload).

As stated above, overrides refer to the practice of redefining the virtual function of a parent class by a subclass. Overloading, however, means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps with different number of parameters, perhaps different types of arguments, or both). In fact, the concept of overloading is not "object-oriented programming", the implementation of overloading is: The compiler according to the function of a different parameter table, the name of the same name is decorated, and then the same name function is a different function (at least for the compiler). For example, there are two functions of the same name: function func (p:integer): Integer, and function func (p:string): integer; Then the compiler has done a modified function name may be this: Int_func, Str_func. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound (early bound) at compile time, so overloading and polymorphism are irrelevant!

The true and polymorphic correlation is "overlay". when a subclass has redefined the virtual function of the parent class, the parent pointer is dynamically based on the different child-class pointers assigned to it (remember: it's dynamic!). Call to the function of the subclass, such that the function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given).
Therefore, such a function address is bound at run time (late bonding). The conclusion is that overloading is only a linguistic feature, independent of polymorphism, and irrelevant to object-oriented!

So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; Inheritance can extend existing code modules (classes); they are all designed to-code reuse.

And polymorphism is for another purpose--interface reuse! And the reality is often that it's hard to reuse code effectively, and the real most valuable reuse is interface reuse, because "interfaces are the most valuable resource for the company." Designing interfaces is more time consuming than using a bunch of classes to implement this interface. And the interface needs to spend more time with more expensive manpower. "

In fact, the reason that inheritance is used for code is getting weaker, because "combination" is a good substitute for inherited extensions of existing code, and "Combination" behaves better (at least to prevent "class explosion"). Therefore, I personally believe that the existence of inheritance to a large extent as the basis of "polymorphism" rather than extending the existing code way.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////

Overrides refer to the existence of a redefined function in a derived class whose function name, parameter column, and return value type must be strictly consistent with the corresponding function in the half-class, with the overriding function and the overridden function being the same as the body of the function (the part of the curly brace).

The overridden version of a subclass is automatically called when a derived class object invokes the same name function in the subclass, rather than the overridden version of the function in the parent class, which is called overwrite.

Here we tell the difference between overloading and overwriting from the angle of the member function.

Features that are overloaded by member functions are:

1) The same range (in the same class);

2) The function has the same name;

3) different parameters;

4) The virtual keyword is optional.

The features covered are:

1) different ranges (in the derived class and the base class, respectively);

2) The function has the same name;

3) the same parameters;

4) The base class function must have the virtual keyword.

Shadowing refers to a function of a derived class that masks a base class function with the same name as the following rule:

1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).

2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay).

What are the different ways of C + + polymorphism?

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.