C + + polymorphism and virtual functions

Source: Internet
Author: User

2017-06-27 19:17:52

One of the important features of C + + object-oriented programming is polymorphism, and the implementation of polymorphism relies on the help of virtual functions.

One, polymorphic role:

    1. Hide implementation details to make the code modular;
    2. interface reuse, implementation of "one interface, a variety of methods";

Second, the declaration of the virtual function:

Class Base

{

Virtual return value type function name (formal parameter list) {}

}

    • When a member function of a base class is declared as a virtual function, a function in its subclass is considered a virtual function if it returns a value type, the function name, and the formal parameter list, whether or not the virtual keyword is added. Conversely, it is hidden with the same name.
    • The virtual keyword must be added when it is declared in a class and does not need to add the virtual keyword when it is defined outside the class
    • constructors, static member functions cannot be virtual functions, destructors can be virtual functions , and even destructors are encouraged to be written as virtual functions
    • A virtual function of a derived class can be embodied only by invoking the object reference of the base class with its object references. It is not possible to realize the characteristics of this dynamic union by simply calling virtual functions through subclasses.

A * p=&b

a& p=b

Three, the realization of polymorphism

You can use the object pointers of the base class to point to different class objects in the same class of families, so that the same calling method is used to invoke a virtual function of the same name for different derived classes.

classa{intA; Public: A (intx) {a=x; }    Virtual void Get() {cout<<"A::"<<a<<Endl;}};classB: Publica{intb; Public: B (intXinty): A (x) {b=y; }    void Get() {cout<<"B::"<<b<<Endl;}};classC: Publicb{intC; Public: C (intXintYintz): B (x, y) {c=Z; }    void Get() {cout<<"C::"<<c<<Endl;}};intMain () {C C1 (1,2,3); A* p1=&C1; B* p2=&C1; P1-Get(); P2-Get(); return 0; }

Four, pure virtual function

When defining a base class, you can declare a virtual function as pure virtual function if you can't give it a concrete implementation, and the implementation of pure virtual function depends entirely on each derived class.

Declaration of a pure virtual function:

Virtual return value type function name (formal parameter list) = 0;

    • Because the definition of pure virtual function cannot be given, there is no function body, and no function body and function body are two concepts, to be differentiated;
    • The function name is assigned a value of 0, essentially assigning the address of a pointer to the function to 0. cannot be used until a derived class has been redefined.

V. Abstract class

An abstract class is a special class that cannot define an object only as a base class, but you can define an object pointer to achieve polymorphism.

There are two types of abstract classes:

    1. Base class with pure virtual function
    2. Constructors or destructors are defined as protected access

C + + polymorphism and virtual functions

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.