Polymorphism in C + +

Source: Internet
Author: User

I. Polymorphism definitions in C + +: Polymorphism is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the character object currently assigned to it. Simply put, in a nutshell: A pointer to the parent class type is allowed to assign a pointer to the child class type.


Two. Dynamic binding is the basis of polymorphism.

Definition of dynamic binding: finds the correct function that is called by the object pointer or reference at the time the program runs.

Dynamic binding principle: a virtual function table (an array of pointers) is built for a function class containing virtual types, and a virtual function is stored in the table, and when the class calls the constructor, at least one virtual pointer to the virtual function table is generated for each object, and when the object of this class calls a virtual function, it points to the virtual table The virtual function address of the polygon to correctly call the function.


Three. Conditions required for polymorphism.

1> to have inheritance

Coverage of the 2> function

3> to have a virtual function

4> a reference to a base class pointer or base class to a derived class object


On the code:

Class a{private:    int a;public:    a (int i): A (i) {}     virtual void print () {        cout << "A=" <<a<<endl;    }    int get_a () {  return a; }};class b{private:    int b;public:     B (int j): B (j) {}    virtual void print () {         cout<< "b=" <<b<<endl;    }    int  get_b () { return b; }};class c : public a,public b{private:     int c;public:    c (int i,int j,int k): A (i), B (j) {  c=k; }    virtual void print () {         a::p rint ();  &nbsP;      b::p rint ();        cout<< " C= "<<c<<endl;    }    void get_ab () { cout< <get_a () << " " <<get_b () <<endl; }};


#include <iostream> #include "muti_inheritance.h" int main (int argc, const char * argv[]) {//Insert code here ...    C x (5,8,10);    A *ap = &x;   A pointer to the Class C object Ap->print ();  Print in output Class A (implemented by dynamic bindings) X.A::p rint ();    Print X.get_ab () for output Class A;    (rewrite) Call Class C of Get_ab () std::cout << "Hello, world!\n"; return 0;}


This article is from the "c0c0s" blog, make sure to keep this source http://9362125.blog.51cto.com/9352125/1690296

Polymorphism in C + +

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.