天天看点

Virtual method and base-type pointer make polymorphism in C++

The core of object-oriented programming may be inheritance and polymorphism. C++ uses virtual method and base-type pointer to achieve this, which is different from Java. In Java, everything is object, and there is no concept of pointer (actually, we can treat the object handlers as "safe pointers"). Java uses some easily confusing mechanisms to achieve polymorphism, as we discussed before. However, in C++, there are not only pointers to objects, but also object variables, which allows programmer to operate on an object directly. Let's see the code below.

Virtual method and base-type pointer make polymorphism in C++

#include  < iostream >

Virtual method and base-type pointer make polymorphism in C++

using   namespace  std;

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

class  A

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

... {

Virtual method and base-type pointer make polymorphism in C++

public:

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

    void print()...{ cout<<"A.print()"<<endl; }

Virtual method and base-type pointer make polymorphism in C++

} ;

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

class  B :  public  A

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

... {

Virtual method and base-type pointer make polymorphism in C++

public:

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

    void print()...{ cout<<"B.print()"<<endl; }

Virtual method and base-type pointer make polymorphism in C++

} ;

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

void  print(A a) ... { a.print(); }

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

void  print(A *  a) ... { a->print(); }

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

main()

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

... {

Virtual method and base-type pointer make polymorphism in C++

    A a, *pa, *pb;

Virtual method and base-type pointer make polymorphism in C++

    B b;

Virtual method and base-type pointer make polymorphism in C++

    pa = &a;

Virtual method and base-type pointer make polymorphism in C++

    pb = &b;

Virtual method and base-type pointer make polymorphism in C++

    a.print();

Virtual method and base-type pointer make polymorphism in C++

    b.print();

Virtual method and base-type pointer make polymorphism in C++

    pa->print();

Virtual method and base-type pointer make polymorphism in C++

    pb->print();

Virtual method and base-type pointer make polymorphism in C++

    print(a);

Virtual method and base-type pointer make polymorphism in C++

    print(b);

Virtual method and base-type pointer make polymorphism in C++

    print(pa);

Virtual method and base-type pointer make polymorphism in C++

    print(pb);

Virtual method and base-type pointer make polymorphism in C++

}

Compile and run it, we find no polymorphism. It prints:

A.print()

B.print()

A.print()

A.print()

A.print()

A.print()

A.print()

A.print()

If we add keyword "virtual" to the head of print() in class A like this:

Virtual method and base-type pointer make polymorphism in C++

class  A

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

... {

Virtual method and base-type pointer make polymorphism in C++

public:

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

    virtual void print()...{ cout<<"A.print()"<<endl; }

Virtual method and base-type pointer make polymorphism in C++

} ;

It prints:

A.print()

B.print()

A.print()

B.print()

A.print()

A.print()

A.print()

B.print()

Look, print() called by pointers of type A all print the right thing according to polymorphism. But still, print() called by variables directly doesn't print the thing defined in subclass. So we come to the conclusion: C++ uses virtual method and base-type pointers to realize the polymorphism mechanism.

Additionally, virtual can be inherited through the whole hierarchy. We can add a class C in the program like this:

Virtual method and base-type pointer make polymorphism in C++

class  C :  public  B

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

... {

Virtual method and base-type pointer make polymorphism in C++

    public:

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

        void print() ...{ cout<<"C.print()"<<endl; }

Virtual method and base-type pointer make polymorphism in C++

}

and add a variable of C and a pointer of type A referencing to this variable, as well as the print statements:

Virtual method and base-type pointer make polymorphism in C++

A  * pc;

Virtual method and base-type pointer make polymorphism in C++

C c;

Virtual method and base-type pointer make polymorphism in C++

pc  =   & c;

Virtual method and base-type pointer make polymorphism in C++
Virtual method and base-type pointer make polymorphism in C++

c.print();

Virtual method and base-type pointer make polymorphism in C++

print(c);

Virtual method and base-type pointer make polymorphism in C++

print(pc);

It will do the similar thing.