Polymorphism in C + +

Source: Internet
Author: User

The so-called polymorphic refers to: The parent class and the subclass have the same name function fun (), when the parent class pointer p points to the parent class object, when calling the P->fun () function, the parent class's fun () function is executed, when the parent class pointer p points to the subclass object (which is allowed by the type compatibility principle), the p-> Fun () function, the fun () function of the parent class is also performed. The reason is that the subclass inherits a fun () function from the parent class and defines a fun () function, and there are actually two fun () functions with the same name in the subclass. Instead, the parent pointer points to the child class object, which in fact points to the fun () function that inherits from the parent class in the subclass object.     So while the parent pointer p points to the subclass object, when the P->fun () function is called, it is actually performed by the fun () function that inherits from the parent class. The function we actually want is: when the parent class and subclass have the same name function fun (), if the parent pointer points to the parent class object, the fun () function of the parent class is executed, and if the parent pointer points to the subclass object, the child class's fun () function is called polymorphic. In order to implement this polymorphic function, we need to add a keyword in front of the fun () function of the parent class, and the fun () function in the subclass can be declared without the virtual keyword, but in general we want to make it obvious that people who read the program know that there can be polymorphic functions, the fun of subclasses ( ) before the function is also added to the virtual keyword,

The following code is an example of a polymorphic implementation:

1#include <iostream>2 using namespacestd;3 4 classParent5 {6  Public:7Parent (int_a): A (_a) {}8     voidPrint () {cout<<"Print Parent Class A:"<<a<<Endl;}9 Ten  Public: One     intA; A }; -  -  the classChild: PublicParent - { -  Public: -Child (intAarint_a_,int_b):P arent (_a), A (_a_), B (_b) {} +     voidPrint () {cout<<"Print Child Class B:"<<b<<Endl;} -  +  Public: A     intA; at     intb; - }; -  -  - intMain () - { inChild C ( -, -, +); -  toParent *Base; +  -     Base=&C; the     Base->print (); *  $     return 0;Panax Notoginseng}

The 8th line of the print () function is not preceded by the virtual keyword, so the above code does not implement polymorphic functionality, the result of the code is: print the parent class a:20 can be seen, executes the child class inherits from the parent class of the print () function.

If we add the virtual keyword to the 8th line of print (the 0 function), the result of the code is: Print the child class b:40 this enables polymorphic functionality

Note: Pointers to base classes, however, can only point to the properties and methods of the base class, and methods that have the same name as the base class in the subclass (excluding attributes with the same name as the base class in the subclass, because the property cannot be declared as virtual). Base class pointers are not callable for properties of subclasses, and for functions with different names in subclasses and base class functions.

The three main concepts of object-oriented programming in C + +:

Encapsulation: Break through the concept of function in C, when using a class to do function parameters, you can use the object's properties and methods at the same time;

Inheritance: The implementation of code reuse, you can use the previous person to write code;

Polymorphic: Constructs a frame, defines the function call the interface, then Yimeimei as long as follows this framework to write the code, the function call method does not change, now writes the code in the same method can call Yimeimei writes the code, equivalent uses the future ...

3 conditions to implement polymorphism (for all object-oriented programming):

1. to have inheritance;

2. To have a virtual function rewrite;

3. Point to a child class object with a parent pointer (or parent class reference)

Polymorphism in C + +

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.