C + + polymorphic

Source: Internet
Author: User

1. Polymorphism literally means multiple states. Polymorphism is used when there is a hierarchy between classes, and when the classes are associated by inheritance.

2. Virtual functions are a mechanism for implementing polymorphism in C + +, and the core idea is to access the functions defined by the derived class through the base class .

Static polymorphism (static link)

The following program, class book is the base class, the derived class class Book1,class Book2 two subclasses.

1#include <iostream>2 3 using namespacestd;4 5 //base class6 classbook{7  Public:8     intGetbookname (void){9cout <<"base class Call! "<<Endl;Ten         return 0; One     } A }; - //Derived Classes - classBook1: Publicbook{ the     intGetbookname (void){ -cout <<"derived class Book1 call! "<<Endl; -         return 0; -     } + }; - //Derived Classes + classBOOK2: Publicbook{ A     intGetbookname (void){ atcout <<"derived class Book2 call! "<<Endl; -         return 0; -     } - }; -  - intMainvoid){ inBook *Book ; - Book1 Book1; to Book2 Book2; +  -Book = &Book1; theBook->getbookname (); *  $Book = &Book2;Panax NotoginsengBook->getbookname (); -}

After compiling the execution, the following results are output:

1 base class Call!  2 base class calls!

The reason for the above error is that the calling function getbookname() is set to the version in the base class by the compiler, which is called static polymorphism , or static linking -function calls are ready before the program executes.

Define the base class with the following modifications:

1 //base class2 classbook{3  Public:4     Virtual intGetbookname (void){5cout <<"base class Call! "<<Endl;6         return 0;7     }8};

After compiling the execution, the following results are output:

1 derived class Book1 call!  2 derived class Book2 call!

As you can see above, each subclass has an independent implementation of the function area (). This is the general way to use polymorphism . With polymorphism, you can have several different classes, all with the same name but with different implementations, and the parameters of the function can even be the same.

Second, virtual function

A virtual function is a function that uses the keyword virtual declaration in a base class. When a virtual function defined in a base class is redefined in a derived class, the compiler is told not to statically link to the function.

What we want is that any point in the program can choose which function to invoke based on the type of object being called, which is called dynamic linking , or late binding .

Three, pure virtual function

You might want to define a virtual function in a base class to redefine the function in a derived class better for the object, but you cannot give a meaningful implementation of the virtual function in the base class, which will use pure virtual functions. The pure virtual function declaration is as follows:

1 Virtual void function () =0;

= 0 tells the compiler that the function has no body and that the virtual function above is a pure virtual function .

C + + polymorphic

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.