To facilitate the description of polymorphism, let's start with a simple example.
# Include <iostream> using namespace STD; Class Parent // defines a parent class {public: // constructor parent (int A = 1) {This-> A = A;} // print the parent data member void print () {cout <"A =" <A <Endl;} PRIVATE: int A; // data member}; // The parent class derives from the Child class child: Public parent {public: // constructor child (int B = 2) {This-> B = B;} // print the data member void print () {cout <"B =" <B <Endl;} private of the Child class: int B; // data member}; // function parameter (pointer as function parameter) void howtoprintf (parent * base) {base-> Print ();} // function parameter (reference as function parameter) void howtoprintf (parent & base) {base. print (); // implement multiple functions in the same sentence, with multiple forms} void main () {parent P1; // define the parent class Object P1 p1.print (); // print the data member child C1 of P1; // define the object C1 c1.print () of the Child class (); // print C1 data member // Use Pointer to access the parent class object and subclass object cout <"\ n use pointer to access the object:" <Endl; parent * base = NULL; base = & P1; base-> Print (); base = & C1; base-> Print (); // functions that do not print child classes // use references to access the parent class object and Child class Object cout <"\ n use reference Access Object:" <Endl; // P2 is the reference of P1, P2 is the alias of P1, P2 is the parent of P1 itself & p2 = p1; p2.print (); // C2 is the reference of C1, c2 is the alias of C1, C2 IS C1 P2 = p1; p2.print (); // use the function to access the parent class object and the subclass object cout <"\ n use the function to access the object (the parent class pointer is used as the function parameter):" <Endl; // function parameter (pointer as function parameter) howtoprintf (& P1); howtoprintf (& C1 ); // use the function to access the parent class object and the subclass object cout <"\ n use the function to access the object (the parent class reference is used as the function parameter):" <Endl; // function parameters (referenced as function parameters) howtoprintf (P1); howtoprintf (C1); System ("pause ");}
Execution result:
Program description:
In the program, the compile function is designed to call the print () function of the parent class, print the data members of the parent class, call the print () function of the subclass, and print the data members of the subclass, then, only when the function of the parent class member is called directly in the program can the data members of the parent class and the data members of the subclass be printed out. In order to solve this problem, the keyword virtual must be added before the member functions with the same parent class and subclass.
Add the keyword virtual to implement the above functions.
Modified code:
# Include <iostream> using namespace STD; Class Parent // defines a parent class {public: // constructor parent (int A = 1) {This-> A = A;} // print the parent data member virtual void print () {cout <"A =" <A <Endl;} PRIVATE: int A; // data member}; // The parent class derives from the Child class child: Public parent {public: // constructor child (int B = 2) {This-> B = B;} // print the virtual void print () {cout <"B =" <B <Endl;} PRIVATE: int B; // data member}; // function parameter (pointer as function parameter) void howtoprintf (parent * base) {base-> Print ();} // function parameter (reference as function parameter) void howtoprintf (parent & base) {base. print (); // implement multiple functions in the same sentence, with multiple forms} void main () {parent P1; // define the parent class Object P1 p1.print (); // print the data member child C1 of P1; // define the object C1 c1.print () of the Child class (); // print C1 data member // Use Pointer to access the parent class object and subclass object cout <"\ n use pointer to access the object:" <Endl; parent * base = NULL; base = & P1; base-> Print (); base = & C1; base-> Print (); // functions that do not print child classes // use references to access the parent class object and Child class Object cout <"\ n use reference Access Object:" <Endl; // P2 is the reference of P1, P2 is the alias of P1, P2 is the parent of P1 itself & p2 = p1; p2.print (); // C2 is the reference of C1, c2 is the alias of C1, C2 IS C1 P2 = p1; p2.print (); // use the function to access the parent class object and the subclass object cout <"\ n use the function to access the object (the parent class pointer is used as the function parameter):" <Endl; // function parameter (pointer as function parameter) howtoprintf (& P1); howtoprintf (& C1 ); // use the function to access the parent class object and the subclass object cout <"\ n use the function to access the object (the parent class reference is used as the function parameter):" <Endl; // function parameters (referenced as function parameters) howtoprintf (P1); howtoprintf (C1); System ("pause ");}
Execution result:
Note:
1. The code above implements indirect access to achieve the output of data members of the parent class and subclass, and is implemented using the same function, this method of implementing multiple functions of the same statement in object-oriented systems is called polymorphism.
2. There are two print () functions in the code above. It is easy to regard the print () function as a function overload. In fact, the print () function is not a function overload, function overloading must be in the same scope or in the same class. The print () function in the above Code is in two different classes respectively, so it is not a function overload, in addition, function overloading refers to the same class of functions with the same name and different function parameters. Functions in the Code do not have any parameters and do not meet the most basic conditions of the overloaded function, while showtoprintf () in the code () the showtoprintf () function is a function overload.
Three conditions for establishing Polymorphism
1. There must be inheritance
2. Function rewriting is required.
3. The parent class Pointer Points to the subclass object.
Top-level equipment in the object-oriented model "multi-day"