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:
Copy Code code as follows:
#include <iostream>
using namespace Std;
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;
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, are the base class objects, and call the functions of the base class itself, so the output 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.
Copy Code code as follows:
Summary: 1, there is a virtual can occur polymorphism phenomenon
2, does not occur polymorphic (no virtual) call is called by the original type
#include <iostream>
using namespace Std;
Class Base
{
Public
virtual void F (float x)
{
cout<< "Base::f (float)" << x <<endl;
}
void g (float x)
{
cout<< "base::g (float)" << x <<endl;
}
void h (float x)
{
cout<< "base::h (float)" << x <<endl;
}
};
Class Derived:public Base
{
Public
virtual void F (float x)
{
cout<< "Derived::f (float)" << x <<endl; Multi-state, overlay
}
void g (int x)
{
cout<< "derived::g (int)" << x <<endl; Hide
}
void h (float x)
{
cout<< "derived::h (float)" << x <<endl; Hide
}
};
int main (void)
{
Derived D;
Base *PB = &d;
Derived *PD = &d;
Good:behavior depends solely on type of the object
Pb->f (3.14f); Derived::f (float) 3.14
Pd->f (3.14f); Derived::f (float) 3.14
Bad:behavior depends on type of the pointer
Pb->g (3.14f); Base::g (float) 3.14
Pd->g (3.14f); Derived::g (int) 3
Bad:behavior depends on type of the pointer
Pb->h (3.14f); Base::h (float) 3.14
Pd->h (3.14f); Derived::h (float) 3.14
return 0;
}
a confusing hidden rule
It was not difficult to distinguish between overloads and overrides, but the hidden rules of C + + caused the complexity of the problem to increase precipitously.
Here "hiding" means that a function of a derived class masks a base class function with the same name as the following:
(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 time, whether there is no virtual
keyword, the functions of the base class are hidden (note that you are 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 has no virtual
Key words. At this point, the functions of the base class are hidden (note that you are confused with overrides).
In the above program:
(1) function derived::f (float) covers base::f (float).
(2) function derived::g (int) hides base::g (float) instead of overloading.
(3) function derived::h (float) hides base::h (float) instead of overwriting it.
C + + pure virtual functions
I. Definition
A pure virtual function is a virtual function declared in a base class that is not defined in a base class, but requires that any derived class define its own implementation method. The way to implement pure virtual functions in a base class is to add "= 0" after the function prototype
virtual void funtion () =0
Ii. Introduction of Causes
1. In order to facilitate the use of polymorphic features, we often need to define virtual functions in the base class.
2, in many cases, it is unreasonable for the base class itself to generate objects. For example, animals as a base class can derive from Tigers, peacocks, and other subclasses, but animals themselves produce objects that are clearly irrational.
In order to solve the above problem, 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 a derived class to achieve polymorphism. Classes that also contain pure virtual functions are called abstract classes and cannot generate objects. This is a good solution to these two problems.
Iii. The concept of similarity
1, polymorphism
When the same object receives a different message or a different object receives the same message, it produces a different implementation action. C + + supports two types of polymorphism: compile-time polymorphism, Run-time polymorphism.
A, compile-time polymorphism: implemented by overloaded functions
B, Run-time polymorphism: implemented by virtual functions.
2. Virtual function
A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement a dynamic overlay of member functions (Override)
3. Abstract class
Classes that contain pure virtual functions are called abstract classes. Because an abstract class contains a pure virtual function that is not defined, you cannot define an object for an abstract class.