Polymorphism describes the ability to operate multiple types using pointers or references of the base class.
We know that sub-class pointers can be implicitly converted to parent classes, so if we want to process an unknown type during program design, you can declare the parameter type as a pointer to the parent class during method declaration.
This means that different methods are implemented based on the input type. The key here is that when the subclass overrides the virtual method of the parent class, it overwrites the virtual method of the parent class at the corresponding position of the virtual method table.
For example:
Header file classFile. h:
- #ifndef classFile_Header_File
- #define classFile_Header_File
- class father
- {
- public:
- virtual void show();
- };
- class son: public father
- {
- public:
- void show();
- void sayhi();
- };
- #endif
Here we implement overrideC ++ for the parent class in the subclass and do not provide the override keyword. Therefore, we must be very careful when rewriting the parent class ).
The TestPoly. cpp Code is as follows:
- #include<iostream>
- #include"classFile.h"
- using namespace std;
-
- void hello(father* fp)
- {
- fp->show();
- }
- int main()
- {
- father f;
- hello(&f);
- son s;
- hello(&f);
-
- }
-
- inline void father::show()
- {
- cout<<"I am father"<<endl;
- }
-
- inline void son::show()
- {
- cout<<"I am son"<<endl;
- }
-
- inline void son::sayhi()
- {
- cout<<"Hi, I am son"<<endl;
- }
Here, the pointer to the parent class will call father: show (). When the pointer to the Child class is passed in, although implicit type conversion is performed, however, because the subclass overwrites the show () method of the parent class in the corresponding position in the inherited virtual method table, the actually called son: show () content is used. Father: show () does not exist in the method table of the subclass. If we remove the virtual keyword, father: show () and son: show () the method table and the subclass will exist at the same time.
So how should we call the Child class if it does not use the parent class?
For example, here, the sayhi () method is implemented in our subclass. This is the address that the parent class pointer father * cannot obtain. Actually, it cannot be obtained directly, the manual pointer offset is still acceptable, but I don't know whether it is the advantage or disadvantage of C ++.) in this case, if we can perform operations through the actually passed object pointer type, it will be OK. Fortunately, C ++ mentioned this function for us, which is to introduce the keyword dynamic_cast.
We rewrite the hello () function as follows:
- Void hello (father * fp)
- {
- Son * ps = dynamic_cast <son *> (fp); // convert fp to son * type
- If (ps) // if the conversion fails, 0 is returned.
- Ps-> sayhi ();
- Else
- Fp-> show ();
- }
Call:
- Int main ()
- {
- Father f;
- Hello (& f); // The conversion fails. Run else.
- Son s;
- Hello (& s); // output Hi, I am son
-
- }
The following blog is a clear explanation of the virtual function table:
Http://blog.csdn.net/hairetz/archive/2009/04/29/4137000.aspx