C ++ object-oriented notes: Polymorphism

Source: Internet
Author: User

From the perspective of system implementation, there are two types of polymorphism: static polymorphism and dynamic polymorphism.

Static Polymorphism: Function overloading and operator overloading (essentially function overloading );

Dynamic Polymorphism: InProgramDuring the running process, the object targeted for the operation is dynamically determined, also known as the Runtime polymorphism, which is generally implemented through virtual functions.

C ++ realizes polymorphism through the combination of virtual functions and pointers to the base class:

    1. When a member function is declared as a virtual function, all functions with the same name in the derived class automatically become virtual functions. Therefore, when the virtual function is declared again in the derived class, you can add virtual, you can also do not add, but it is best to add virtual to make the program clear.
    2. If the virtual functions in the base class are not redefined in the derived class, the derived class inherits the virtual functions of its direct base class.

Static Association: during compilation, you can determine which class the virtual function is called, such as function overload and virtual function called by Object Name.

Dynamic Association: binds virtual functions and class objects in the running stage.

When to declare a virtual function:

    1. First, whether the class where the member function is located will act as the base class, and then whether the member function may be changed after the class is inherited. If you want to change its function, it is generally set as a virtual function.
    2. If the member function does not need to be modified after the class is inherited, or the derived class does not use this function, do not set it as a virtual function.
    3. When calling a member function, consider whether the object name is a base class pointer or reference. If it is accessed through a base class pointer or reference, it should be a virtual function.
    4. Sometimes, when defining a virtual function, it does not define the function body (its function body is empty), but defines a virtual function name. The specific function is left to the derived class to add.

When using virtual functions, the system has a certain amount of space overhead,When a class has a virtual function, the system constructs a virtual function table for the class (a pointer array that stores the entry address of each virtual function)

If a class contains a virtual function, the first member of the object will be a pointer to the virtual function table when the class is instantiated.

Pure virtual function, abstract class

Virtual int F () = 0; // declare a pure virtual function

If a class contains pure virtual functions, the class is an abstract class. The abstract class cannot be instantiated and can only be used as an inherited base class.

View code

 1 # Include <iostream> 2   3   Using   Namespace  STD;  4   5   // Define the base class-animal class  6   Class  Animals  7   {  8   Public  :  9 Animals (); //  Define Constructors  10 Animals ( Int ); //  Constructor with a parameter 11   Public  :  12       Int  ID;  13   Public  :  14       Virtual   Void Action (); //  Define a member function and declare it as a virtual function to implement Polymorphism  15   };  16  17 Animals: Animals (){} //  Implement Constructor  18 Animals: Animals ( Int ID): ID (ID ){} //  Implementation constructor. The initialization member list is not specified during definition and is only specified during function implementation.  19   Void Animals: Action () //  Implement member functions  20   {  21 Cout < " Animal's action  " < Endl;  22   }  23   24   25   //  Define a derived class-human  26   Class Person: Public  Animals  27   {  28  Public  :  29 Person ( Int , Int , Int ); //  Defines the constructor and calls the parent class constructor only when the constructor is implemented.  30   Public : //  Member variables  31       Int Weight; //  Weight 32       Int Height; //  Height  33   Public : //  Member Functions  34       Virtual   Void Action (); //  Define a member function and declare it as a virtual function to implement Polymorphism  35   };  36   // Implement the derived class constructor and display the call base class Constructor  37 Person: Person ( Int Weight, Int Height, Int  ID): Animals (ID)  38   {  39       This -> Weight = Weight;  40       This -> Height = Height;  41  }  42   43   //  Implement member functions of a derived class  44   Void  Person: Action ()  45   {  46 Cout < "  Person's action: Walk  " < Endl;  47   } 48   49   //  Define a derived class-monkey class  50   Class Monkey: Public  Animals  51   {  52   Public  :  53 Monkey ( Int , Int ,Int ); //  Defines the constructor and calls the parent class constructor only when the constructor is implemented.  54   Public : //  Member variables  55       Int Weight; //  Weight  56       Int Height; //  Height  57  Public : //  Member Functions  58       Virtual   Void Action (); //  Define a member function and declare it as a virtual function to implement Polymorphism  59   };  60   //  Implement the derived class constructor and display the call base class Constructor  61 Monkey: Monkey ( Int Weight, Int Height, Int  ID): Animals (ID)  62   {  63       This -> Weight = Weight;  64       This -> Height = Height;  65   }  66   67   //  Implement member functions of a derived class 68   Void  Monkey: Action ()  69   {  70 Cout < "  Monkey's action: Jump  " < Endl;  71   }  72   73   Int  Main ()  74  {  75       //  Define animal objects  76   Animals;  77       //  Define animal pointer  78 Animals * Pi;  79 Pi = & A;  80       //  Call behavior by pointer  81 Pi-> Action ();  82 Person B ( 10 , 10 , 0001  );  83 Pi = & B; //  Change pointer to person  84 Pi-> Action ();  85 Monkey C ( 10 , 10 ,0002  );  86 Pi = & C; //  Change pointer to monkey  87 Pi-> Action ();  88       Return   0  ;  89 }

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.