Summary of Polymorphism

Source: Internet
Author: User
I. Polymorphism

1. There are two types of Polymorphism

(1) polymorphism during compilation: implemented through function Overloading

(2) Runtime polymorphism, implemented through virtual functions, that is, dynamic Association

2. inheritance is not a Polymorphism

Class father {public: Virtual void run () {cout <"parent run" <Endl;} void jump () {cout <"parent Hop" <Endl ;}; class son: Public father {public: void run () {cout <"sub-Run" <Endl ;} void jump () {cout <"sub-Hop" <Endl ;}; int main () {FATHER * pF = new son; PF-> Run (); PF-> jump (); Delete P; return 0 ;}

Ii. Overload 1. For operator overloading, see Operator Overloading Summary 2. Function Overloading

1. Make B Public inherit from A. The following functions are all public

(1) A has several reloads of test (), and B does not have test ()

Then, B. Test () calls the corresponding overload of.

(2) A has several test () overload, B has test () overload 1, and no test () overload 2

Then: B. Hello () overload 1, the corresponding overload of B is called

B. If Hello () is overloaded with 2, an error occurs.

B. A: Hello (), the corresponding overload of A is called.

2. Void test (int I) const and void test (int I) are two different functions, which can exist at the same time.

3. For the overloading of multiple inheritance, see parent class and Child class and inheritance relationship Summary 3. Dynamic Association 1. What is dynamic Association?

(1) concatenation: A caller is connected to a correct caller.

Static joint Editing: this function is used during compilation and not during execution.

Dynamic Association: this feature is not associated during compilation and is associated during execution.

(2) high overhead of Dynamic Association editing

2. Use of Dynamic Association coding (1) correct use of polymorphism:
Class father {public: Virtual void test () {cout <"parent" <Endl ;}}; class son: Public father {public: void test () {cout <"sub" <Endl ;}}; class daughter: Public father {public: void test () {cout <"" <Endl ;}}; void one (father one) {one. test ();} void two (father * Two) {two-> test ();} void three (father & Three) {three. test () ;}int main () {int N; father * PS = new son; One (* PS); Delete pS; // 1 father * Pd = new daughter; two (PD); Delete PD; // 2 father * pF = new father (); three (* PF); Delete PF; // 3 return 0 ;}

Output:

Parent
Female
Parent
Explanation:

1. Because it is not called using pointers or references, even virtual functions cannot implement polymorphism.

2. The virtual function is called as a pointer, so it is a polymorphism.

3. The virtual function is called as a reference, so it is a polymorphism.

(2) Dynamic association can be realized only when pointers or references are used. The incorrect Dynamic Association is as follows:

class A{int x;public:A(){x = 1;cout<<"A"<<endl;}void get(){cout<<x<<endl;}};class B: public A{int x;public:B(){x = 2;cout<<"B"<<endl;}void get(){cout<<x<<endl;}};class C : public A{int x;public:C(){x = 3;cout<<"C"<<endl;}void get(){cout<<x<<endl;}};int main(){A p;int choice;while(cin>>choice){switch(choice){case 1:p = A();break;case 2:p = B();break;case 3:p = C();break;}p.get();}return 0;}

Output:

Input: 1

Output: A 1

Input: 2

Output: a B 1

Input: 3

Output: a c 1

No matter what the input is, P. Get (); outputs 1. No polymorphism is implemented.

(3) Use a member name in a virtual function to forcibly cancel Dynamic Association.

Class father {public: Virtual void test () {cout <"parent" <Endl ;}}; class son: Public father {public: void test () {cout <"sub" <Endl ;}; int main () {FATHER * pF = new son; PF-> test (); PF-> FATHER :: test (); Return 0 ;}

Output:

Child

Parent

(4) directly calling non-virtual functions does not result in polymorphism.

All virtual functions that call non-virtual functions can realize polymorphism.

(5) A function of the base class is a virtual function, and the function of the derived class is also a virtual function. 3. Examples of Dynamic Association

Father * pF = new son; // first constructs the parent and then the child, but pf points to the parent object. Therefore, PF can only call Father members, but cannot call son's member Delete PF; // only analyzes the parent and does not parse the child, resulting in Memory leakage

Solution:

1) PF is forcibly converted to son class. This method is not good and error-prone.

2) define father destructor as virtual to realize Polymorphism

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.