On the polymorphism of C + +

Source: Internet
Author: User

Turn from: http://blog.csdn.net/hackbuteer1/article/details/7475622

C + + programming language is a wide range of applications, supporting a variety of programming language computer programming.   Today we will give you a detailed description of the C + + polymorphism of some of the basic knowledge to facilitate everyone in the learning process can have a full grasp. Polymorphism can be simply summed up as "an interface, a variety of methods", the program at runtime to determine the call function, which is the core concept of object-oriented programming domain. Polymorphism (POLYMORPHISN), literal meaning of many shapes.
C + + polymorphism is implemented through virtual functions, which allow subclasses to redefine member functions, while subclasses redefine the parent class as overriding (override), or as overrides. (Here I feel to add that there are two ways to rewrite the words, overriding a member function and overriding a virtual function, only if the virtual function is overridden to represent C + + polymorphism, but overloading allows multiple functions with the same name, which have different argument lists that allow different parameter types, Or the two are different. The compiler modifies the name of a function of the same name, based on the different lists of these functions, to generate some preprocessing functions of different names to implement the overload problem with the function call of the same name. But this does not reflect polymorphism.
The real difference between polymorphic and non-polymorphic is whether the function address is early bound or late bound. If a function is called, the invocation address of the function can be determined during compiler compilation, and the production code is static, which means that the address is early bound. If the address of a function call cannot be determined during the compiler, it needs to be determined at run time, which is a late binding.
So what is the role of polymorphism, encapsulation can make code modular, inheritance can extend existing code, their purpose is for code reuse. The purpose of polymorphism is to reuse the interface. That is, the function can be invoked through the same interface to the implementation of the respective object, regardless of what is passed over to the object of that class.

The most common use is to declare a pointer to a base class, use it to point to any subclass object, call the corresponding virtual function, and implement different methods depending on the subclass of the point. If you do not use a virtual function, that is, if you do not use C + + polymorphism, when you invoke the corresponding function with the base class pointer, you are always limited to the base class function itself, and you cannot invoke the overridden function in the subclass. Because there is no polymorphism, the address of the function call will be certain, and the fixed address will always call to the same function, which can not implement an interface, the purpose of many methods.

Written questions:

[CPP] view plain copy #include <iostream> using namespace std;       Class A {public:void foo () {printf ("1\n");       virtual void Fun () {printf ("2\n");   }   }; Class B:public A {public:

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.