The concept and precondition of C + + learning 22 polymorphism

Source: Internet
Author: User

As mentioned in the "assignment of C + + base classes and derived Classes" section, pointers to base classes can also point to derived class objects. Take a look at the following example:

#include <iostream>using namespacestd;classpeople{protected:    Char*name; Public: People (Char*name): Name (name) {}voidDisplay () {cout<<"people:"<<name<<Endl;}};classStudent: Publicpeople{ Public: Student (Char*name):P eople (name) {}voidDisplay () {cout<<"Student:"<<name<<Endl;}};intMain () {people*p =NewPeople ("Xiao Ming"); P-display (); P=NewStudent ("Li Lei"); P-display (); return 0;}

Operation Result:
People:xiao Ming
People:li Lei

We generally think that if the pointer points to a derived class object, then the member variables and member functions of the derived class should be used, which is in line with people's thinking habits.

But the result of this example tells us that when the base-class pointer p points to an object of the derived class Student, although the Student member variable is used, it does not use its member function, causing the output to be nondescript and non-conforming to our expectations.

If you want to access the member function of the Student class through the P pointer, you can declare the member function as a virtual function, see the following code:

#include <iostream>using namespacestd;classpeople{protected:    Char*name; Public: People (Char*name): Name (name) {}//Add virtual keyword declared as virtual function    Virtual voidDisplay () {cout<<"people:"<<name<<Endl;}};classStudent: Publicpeople{ Public: Student (Char*name):P eople (name) {}//Add virtual keyword declared as virtual function    Virtual voidDisplay () {cout<<"Student:"<<name<<Endl;}};intMain () {people*p =NewPeople ("Xiao Ming"); P-display (); P=NewStudent ("Li Lei"); P-display (); return 0;}

Operation Result:
People:xiao Ming
Student:li Lei

Compared to the above code, this code simply adds a virtual keyword before the display () function declaration, declaring the member function to be virtual (virtual function). This allows the member functions of the Student class to be called through the P pointer, which is also demonstrated by the running result.

With virtual functions, a base-class pointer can use either a member function of a base class or a member function of a derived class, which has many forms, or multiple manifestations, which is polymorphic (polymorphism).

The same statement is in the code above, and p->display(); when P points to a different object, the action it performs is not the same. The same statement can perform different actions and appear to behave differently, which is polymorphic

Polymorphism is one of the main characteristics of object-oriented. In C + +, the only use of virtual functions is to form polymorphism.

C + + provides polymorphism by using a base-class pointer to "omni-directional" access to member variables and member functions of all derived classes, including direct and indirect derivations, especially member functions. If there is no polymorphism, we can only access member variables.

Conditions that constitute polymorphism

Three conditions for polymorphic presence:

    • There must be an inheritance relationship;
    • Virtual functions with the same name must be in an inheritance relationship, and they are overwrite relationships (overloads are not allowed).
    • There is a pointer to the base class through which the virtual function is called.

Note: Virtual functions in derived classes must override (not reload) virtual functions in the base class to be accessed through a base-class pointer. Take a look at the following code:

#include <iostream>using namespacestd;classbase{ Public:    voidA () {cout<<"base::a ()"<<Endl;} Virtual voidB () {cout<<"base::b ()"<<Endl;} Virtual voidC () {cout<<"base::c ()"<<Endl;}};classDerived: Publicbase{ Public:    //overriding general member functions of the base class, not constituting polymorphic    voidA () {cout<<"derived::a ()"<<Endl;} //overriding the base-class virtual function to form polymorphic    Virtual voidB () {cout<<"derived::b ()"<<Endl;} //overloading a virtual function of a base class does not constitute a polymorphic    Virtual voidCintN) {cout<<"derived::c ()"<<Endl;} //new functions for derived classes    intD () {cout<<"Derived::d ()"<<Endl;}};intMain () {Base*p =NewDerived; P-A (); P-b (); PC (0);//Compile ErrorP-D ();//Compile Error    return 0;}

The concept and precondition of C + + learning 22 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.