MOOC C ++ Notes (VI): Polymorphism

Polymorphism

Virtual function

In the definition of class, in front of the member function is virtual function virtual keyword.
only the virtual keyword in the class definition in the function declaration, when not writing the function body.
Constructors and static member functions can not be virtual.

Polymorphic forms

Base class pointer to the derived class

Pointer derived class can be assigned to the base class pointer.
When the same name by a base pointer calls the base class and a derived class virtual function:
(1) if the object is a pointer to a base class, it is called a base class virtual function.
(2) If the pointer to an object of a derived class, it is called a derived class virtual function.
This mechanism is called "polymorphism."

    class CBase
    {
    public:
        virtual void SomeVirtualFunction(){}
    };
    class CDerived :public CBase
    {
    public:
        virtual void SomeVirtualFunction() {}
    };
    int main()
    {
        CDerived ODerived;
        CBase*p = &ODerived;
        p->SomeVirtualFunction();//调用哪个函数取决于p指向哪个类的对象。
        return 0;
    }

Base class derived class object reference

Derived class objects can be assigned to the base class referenced
when referencing calls the base class and a derived class virtual function by the same name in the base class:
(1) if the object is referenced by reference the base class, the base class is called virtual function.
(2) if the reference is a reference of a derived class object, then it is called a derived class virtual function.
This mechanism is also called "polymorphism."

    class CBase
    {
    public:
        virtual void SomeVirtualFunction(){}
    };
    class CDerived :public CBase
    {
    public:
        virtual void SomeVirtualFunction() {}
    };
    int main()
    {
        CDerived ODerived;
        CBase&r = ODerived;
        r.SomeVirtualFunction();//调用哪个函数取决于r引用哪种类型的对象。
        return 0;
    }

Non constructor and destructor non-virtual function call

    class Base
    {
    public:
        void fun1() { this->fun2; }
        virtual void fun2() { cout << "Base::fun2()" << endl; }
    };
    class Derived :public Base
    {
    public:
        virtual void fun2() { cout << "Derived::fun2()" << endl; }
    };
    int main()
    {
        Derived d;
        Base*pBase = &d;
        pBase->fun1();//输出Derived::fun2(),调用哪一个fun2,取决于指向的对象类型。
        return 0;
    }

In a non-virtual function call the constructor and destructor non-virtual function is invoked which depends on the type of the object pointed to or referenced by the type of the object.
Note: call the virtual function is not polymorphic destructor and constructor. Can be determined at compile time, the function call is a function of their class or base class definition, do not wait until run time decided to call their own or of a derived class.
Further, to ensure that polymorphic forms of the derived parameters to the function and the same base class, the derived class virtual can not be declared, but if the base class is then used to achieve polymorphic must use virtual declaration.

The principle polymorphism

Dynamic key is a base class pointer or reference by calling a virtual function call of uncertainty in the end is the base class at compile time or a derived class, the runtime determines ---- called "dynamic binding."

Vtable

Each class has virtual functions (virtual function or derived class) has a virtual function table, any object class are placed in the virtual function table pointer. Virtual function table lists the address of the class virtual functions.
Each class has virtual functions are extra 4 bytes (depending on machine word), these extra byte is used to put the address of the virtual function table (stored in the header space usually placed class) .
Function call statement polymorphic be compiled into a series of look virtual function address in the virtual function table based on the address vtable objects base class pointer points (or base class references cited) stored in, and the virtual function call instructions.
Multi-state needs to pay extra overhead on some time and space.

When directed to polymorphic changed by changing the virtual address of the virtual function table function (to help understand that the principles of polymorphism)

    #include<iostream>
    using namespace std;
    class A
    {
    public:
        virtual void Func() { cout <<"A::Func" << endl; }
    };

    class B:public A
    {
    public:
        virtual void Func() { cout << "B::Func" << endl; }
    };

    int main(void)
    {
        A a;
        A*pa = new B;
        pa->Func();
        //64位程序指针为8字节
        long long *p1 = (long long*)&a;
        long long *p2 = (long long *)pa;
        *p2 = *p1;
        pa->Func();
        system("pause");
    }

The output is:
B :: Func
A :: Func
herein second Func is call the A Func only because we use the virtual function table pointer changes the address of the object.

Virtual destructor

When deleting a derived object through a pointer base class, usually only call the destructor of the base class, but the object is deleted a derived class, should call the destructor derived class then calls the destructor for the base class .
The solution is the destructor for the virtual base class declaration:
destructor virtual derived class can not be declared
by the destructor pointer derived class to delete the base class, and then calls the base class destructor.
In general, if the definition of a class virtual function, the destructor will be defined as virtual functions. Alternatively, a class intend to use as a base class, it should be defined as a virtual function destructor.

Abstract classes and pure virtual function

Pure virtual function

No function body to the virtual function, particularly for the wording virtual void Fun()=0.

Abstract class

Containing pure virtual functions is called an abstract class.
Abstract classes can also serve as a base class to derive a new class use, you can not create objects of abstract classes.
Abstract class pointers and references to an object may be derived from the abstract class class. (You can define an abstract class pointers and references, but can not create the entity classes).
In the member function abstract class can call a pure virtual function, but can not be called pure virtual function inside the constructor or destructor (because destructors and constructors are not polymorphic, and calls itself empty pure virtual function is meaningless).
If a class derived from the abstract derived, then if and only if it implements all pure virtual function in the base class, it can become non-abstract class.

Polymorphic differences, overloading and cover

See blog: polymorphism, overloading, cover the difference and connection

Polymorphic role

In object-oriented design in the abstract state of an abstract can be enhanced scalability, i.e. to modify or increase the functions of the program when less need to change and the increase of the code.

Guess you like

Origin www.cnblogs.com/z-y-k/p/11618419.html