C + + polymorphism __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 (polymorphism), 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;   & nbsp;  class a   {   public:       void foo ()        {           printf ("1\n") ;       }       virtual void fun ()         {           printf ("2\n");        }  };   class b : public a    {   public:       void foo ()         {           printf ("3\n");        }       void fun ()        {           printf ("4\n");       }   };   int main (void)    {       A a;       B b;       A *p = &a;       p->foo ();       p->fun ();        p = &b;       p->foo ();        p->fun ();       return 0;  }          the first P->foo () and P->fuu () are very well understood, are the base class pointers, the base class object, and the function of the base class itself, so the output result is 1, 2.
The second output result is 1, 4. P->foo () and P->fuu () are base class pointers to child class objects. Formally embodies the use of polymorphism, P->foo () because the pointer is a base class pointer, a function that points to a fixed offset, so you can only refer to the code of the base class's Foo () function at this point. As a result, the output is still 1. and the P->fun () pointer is a base class pointer, the point of fun is a virtual function, because each virtual function has a list of virtual functions, when P calls fun () is not a direct call to the function, but rather through the virtual function list to find the address of the corresponding function, so depending on the object pointed to, the function address will also be different , the address of the fun () function of the corresponding subclass is found, so the result of the output is also the result of the subclass 4.
There is also an alternative test method in the subject of the written examination. That
b *ptr = (b *) &a;  Ptr->foo (); Ptr->fun ();
Ask the output of these two calls. This is a base class object that uses the pointer of a subclass to point to a cast to the subclass address. As a result, the output of these two calls is 3, 2.
Not quite understanding this usage, in principle, since B is a subclass pointer, although it is given the base class object address, Ptr->foo () at the time of invocation, because the address offset is fixed, the offset is the offset of the subclass object, so even if you point to a base class object, Or a function called to a subclass, although there may not be any instantiation of the subclass object from the beginning to the end.
The invocation of Ptr->fun (), perhaps because of C + + polymorphism, because it points to a base class object, finds the address of the fun () function in the base class by reference to a list of virtual functions, and therefore calls the function of the base class. This shows that polymorphism is powerful, can adapt to all kinds of changes, whether the pointer is a base class or subclass, can find the correct implementation.
[CPP]  View Plain  copy  /summary: 1, there is a possibility of polymorphic phenomenon in virtual   // 2, no polymorphic (no virtual) call on the original type of     #include <iostream>   using namespace std;      class  base   {   public:       virtual void f (float x)   

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.