The example demonstrates the implementation process of C ++ polymorphism.

Source: Internet
Author: User

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:

 
 
  1. #ifndef classFile_Header_File   
  2. #define classFile_Header_File   
  3. class father   
  4. {   
  5. public:   
  6.     virtual void show();   
  7. };   
  8. class son: public father   
  9. {   
  10. public:   
  11.     void show();   
  12.     void sayhi();   
  13. };   
  14. #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:

 
 
  1. #include<iostream>   
  2. #include"classFile.h"   
  3. using namespace std;  
  4.  
  5. void hello(father* fp)   
  6. {   
  7.         fp->show();   
  8. }   
  9. int main()   
  10. {   
  11.     father f;   
  12.     hello(&f);   
  13.     son s;   
  14.     hello(&f);  
  15.  
  16. }  
  17.  
  18. inline void father::show()   
  19. {   
  20.     cout<<"I am father"<<endl;   
  21. }  
  22.  
  23. inline void son::show()   
  24. {   
  25.     cout<<"I am son"<<endl;   
  26. }  
  27.  
  28. inline void son::sayhi()   
  29. {   
  30.     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:

 
 
  1. Void hello (father * fp)
  2. {
  3. Son * ps = dynamic_cast <son *> (fp); // convert fp to son * type
  4. If (ps) // if the conversion fails, 0 is returned.
  5. Ps-> sayhi ();
  6. Else
  7. Fp-> show ();
  8. }

Call:

 
 
  1. Int main ()
  2. {
  3. Father f;
  4. Hello (& f); // The conversion fails. Run else.
  5. Son s;
  6. Hello (& s); // output Hi, I am son
  7.  
  8. }

The following blog is a clear explanation of the virtual function table:

Http://blog.csdn.net/hairetz/archive/2009/04/29/4137000.aspx

Related Article

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.