Article source: blog.csdn.net/hackbuteer1…

C++ programming language is a widely used computer programming language that supports a variety of programs. We will introduce some basic knowledge of C++ polymorphism in detail today, so that you can have a full grasp of it in the learning process. Polymorphism can be summed up simply as “one interface, many methods”, the function that the program decides to call only at run time, and it is a core concept in the field of object-oriented programming. Polymorphisn literally means a variety of shapes. C++ polymorphism is implemented through virtual functions, which allow subclasses to redefine member functions. The practice of subclasses redefining parent classes is called override, or overriding. (I think to add here, rewriting the words can have two kinds, direct rewriting a member function and rewrite the virtual functions, only override the virtual function can be classified as embodies the c + + polymorphism) and overloading is allowed to have more of the same name function, and the function’s parameter list, allowing different number of parameters, parameter types are different, or both. According to the different lists of these functions, the compiler will modify the names of the functions with the same name, so as to generate some preprocessors with different names, so as to realize the overload problem when calling the functions with the same name. But it doesn’t show polymorphism. The essential difference between polymorphic and non-polymorphic is whether the function address is early bound or late bound. If the call to a function, which the compiler can determine during compilation and produce code, is static, that is, the address is early bound. If the address of the function call cannot be determined at compiler time and needs to be determined at run time, it is late binding. So what’s the role of polymorphism? Encapsulation makes code modular, inheritance extends existing code, and both aim for code reuse. The purpose of polymorphism is interface reuse. That is, functions can be called through the same interface to implementation methods appropriate to each object, regardless of which class object is being passed. \

The most common use is to declare a pointer to a base class, use the pointer to point to any subclass object, call the corresponding virtual function, and implement different methods depending on the subclass to point to. If virtual functions are not used, that is, C++ polymorphism is not taken advantage of, calls to the corresponding functions using base class Pointers are always limited to the base class function itself, and cannot be called to functions overridden in subclasses. Because there is no polymorphism, the address of the function call will be fixed, and the fixed address will always call the same function, which will not achieve the purpose of one interface, multiple methods.

Written test topics:

[cpp]  view plain copy

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. class A  
  5. {  
  6. public:  
  7.     void foo()  
  8.     {  
  9.         printf(“1\n”);  
  10.     }  
  11.     virtual void fun()  
  12.     {  
  13.         printf(“2\n”);  
  14.     }  
  15. };  
  16. class B : public A  
  17. {  
  18. public:  
  19.     void foo()  
  20.     {  
  21.         printf(“3\n”);  
  22.     }  
  23.     void fun()  
  24.     {  
  25.         printf(“4\n”);  
  26.     }  
  27. };  
  28. int main(void)  
  29. {  
  30.     A a;  
  31.     B b;  
  32.     A *p = &a;  
  33.     p->foo();  
  34.     p->fun();  
  35.     p = &b;  
  36.     p->foo();  
  37.     p->fun();  
  38.     return 0;  
  39. }  

The first p->foo() and p->fuu() are both fairly straightforward, pointing to base objects and calling base functions, so the output is 1 and 2. The second output is 1, 4. P ->foo(); p->foo(); p->foo(); p->foo(); P ->fun() is a pointer to the base class. Fun is a virtual function. Since every virtual function has a list of virtual functions, p calls fun() not directly, but finds the address of the corresponding function from the list of virtual functions. This will find the address of the fun() function of the corresponding subclass, so the output will also be the result of the subclass, 4. There is also an alternative test in the written test. B * PTR = (B *)&a; ptr->foo(); ptr->fun(); Ask for the output of both calls. This is a pointer to a base class object cast to the address of a subclass. As a result, the output of these two calls is 3,2. Is not very understand the usage and come up from theory to explain, because B is subclass pointer, although the address have been given a base class object, but the PTR – > foo () at the time of call, due to the fixed address offset, the offset is a subclass object offset, so even in a base class object, or call the subclasses of function, Although there may be no instantiation of the subclass object from start to finish. PTR ->fun(); PTR ->fun(); PTR ->fun(); PTR ->fun() This shows the power of polymorphism, can adapt to all kinds of changes, whether the pointer is a base class or a subclass, can find the correct implementation method. \

[cpp]  view plain copy

  1. // Summary: 1. Polymorphism can occur only when virtual exists
  2. // 2. Call the same type without virtual
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. class Base  
  7. {  
  8. public:  
  9.     virtual void f(float x)  
  10.     {  
  11.         cout<<“Base::f(float)”<< x <<endl;  
  12.     }  
  13.     void g(float x)  
  14.     {  
  15.         cout<<“Base::g(float)”<< x <<endl;  
  16.     }  
  17.     void h(float x)  
  18.     {  
  19.         cout<<“Base::h(float)”<< x <<endl;  
  20.     }  
  21. };  
  22. class Derived : public Base  
  23. {  
  24. public:  
  25.     virtual void f(float x)  
  26.     {  
  27. cout<<“Derived::f(float)”<< x <
  28.     }  
  29.     void g(int x)  
  30.     {  
  31. cout<<“Derived::g(int)”<< x <
  32.     }  
  33.     void h(float x)  
  34.     {  
  35. cout<<“Derived::h(float)”<< x <
  36.     }  
  37. };  
  38. int main(void)  
  39. {  
  40.     Derived d;  
  41.     Base *pb = &d;  
  42.     Derived *pd = &d;  
  43.     // Good : behavior depends solely on type of the object  
  44. Pb – > f (3.14 f); / / Derived: : f (float) 3.14
  45. Pd – > f (3.14 f); / / Derived: : f (float) 3.14
  46.   
  47.     // Bad : behavior depends on type of the pointer  
  48. Pb – > g (3.14 f); / / Base: : 3.14 g (float)
  49. Pd – > g (3.14 f); // Derived::g(int) 3
  50.   
  51.     // Bad : behavior depends on type of the pointer  
  52. Pb – > h (3.14 f); / / Base: : 3.14 h (float)
  53. Pd – > h (3.14 f); / / Derived: : 3.14 h (float)
  54.     return 0;  
  55. }  

Confusing hiding rules should not be difficult to distinguish between overloading and overwriting, but C++ ‘s hiding rules make the problem exponentially more complicated. (1) If the function of a derived class has the same name as the function of the base class, but the parameters are different. At this point, functions of the base class are hidden with or without the virtual keyword (be careful not to be confused with overloading). (2) If the function of the derived class has the same name as the function of the base class and has the same parameters, but the base class function does not have the virtual keyword. At this point, the functions of the base class are hidden (note not to be confused with overwriting). (1) The Derived::f(float) function overrides Base::f(float). (2) The function Derived::g(int) hides Base::g(float) instead of overloading. (3) The function Derived::h(float) hides Base::h(float) instead of overwriting it. A pure virtual function is a virtual function declared in a base class. It is not defined in the base class, but requires that any derived class define its own implementation method. Virtual void funtion()=0; virtual void funtion()=0; 2. In many cases, it makes no sense that the base class itself generates objects. For example, animals as a base class can be derived from tiger, peacock and other subclasses, but the animal itself to generate objects is obviously unreasonable. In order to solve the above problems, the concept of pure virtual Function is introduced, and the Function is defined as pure virtual Function(method: virtual ReturnType Function()= 0;). , the compiler requires that it must be overridden in derived classes to implement polymorphism. Classes that also contain pure virtual functions are called abstract classes and cannot generate objects. This solves both of these problems. 1. Polymorphism refers to different implementation actions when the same object receives different messages or different objects receive the same message. C++ supports two types of polymorphism: compile-time polymorphism and run-time polymorphism. A. Compile-time polymorphism: implemented through overloaded functions b. Runtime polymorphism: implemented through virtual functions A virtual function is a member function that is declared virtual in a base class and redefined in a derived class to Override the member function. An object of an abstract class cannot be defined because it contains pure virtual functions that are not defined. \