Three characteristics of the state as much as C ++

Three feature of C ++: encapsulation, inheritance, polymorphism. The code may be such that the modular package, inheritance can extend existing code, and polymorphic purpose is to reuse the interface. The following mainly describes the main polymorphism.

What is polymorphic

C ++ polymorphism is one sentence: adding virtual keyword before the base class, the derived class override this function, it will be calling the corresponding function according to the actual type of the object at runtime.

Polymorphism refers to the production of different actions to achieve the same object receives different messages when different objects or the same message is received. C ++ supports two polymorphisms: compile-time polymorphism, run-time polymorphism.

a、编译时多态性(静态多态):通过重载函数实现
b、运行时多态性(动态多态):通过虚函数实现。

Compile-time polymorphism:

Also known as compile-time polymorphism, that is, you can determine which function to execute the program during compilation system. For example: function overloading class members designated by the arithmetic operator.

class A {
public:
    A() {}
    A( int x ) {}
    void fun() {}
    void fun( int x ) {}
};

class B {
public:
    B() {}
    void fun() {}
    void fun( int x ) {}
};

Run-time polymorphism:

The most common use is to base class pointer declaration, with which a pointer to any subclass object, call the corresponding virtual function, can be achieved in different ways according to different subclasses directed. If no time virtual functions, then calls the base class pointer using the corresponding function will always be limited to a base class function itself, and can not call to the subclass of being rewritten. Because there is no polymorphism, the address of the function call will be certain, fixed address will always be a call to the same function, which can not implement an interface, the purpose of the various methods.

Polymorphic usage

note:

1、用virtual关键字申明的函数叫做虚函数,虚函数肯定是类的成员函数。  
2、存在虚函数的类都有一个一维的虚函数表叫做虚表,类的对象有一个指向虚表开始的虚指针。虚表是和类对应的,虚表指针是和对象对应的。  
3、多态性是一个接口多种实现,是面向对象的核心,分为类的多态性和函数的多态性。  
4、多态用虚函数来实现,结合动态绑定.  
5、纯虚函数是虚函数再加上 = 06、抽象类是指包括至少一个纯虚函数的类。
#include <iostream>
 
using namespace std;
 
class Base
{
public:
    // 父类虚函数必须要有 virtual 关键字
    virtual void fun_test()
    {
        cout << "parent class" << endl;
    }

    void test()
    {
        cout << "is parent" << endl;
    }   
};
 
class Derived : public Base
{
public:
    // 子类有没有 virtual 关键字都可以
    void fun_test()
    {
        cout << "child class" << endl;
    }
};
 
int main(int argc, char const *argv[])
{

    Base *p = NULL; // 创建一个父类的指针
    Derived child;

    p = &child; // 指向子类的对象
    p->fun_test(); // 执行的是子类的 fun_test() 函数

    return 0;
}

Output:

Here Insert Picture Description
The compiler at compile time, there are found in the Base class virtual function, then the compiler class comprises a virtual function for each create a virtual table (i.e. the vtable), the table is a one-dimensional array stored in the array address for each virtual function.

Polymorphism is achieved by running C ++ virtual functions, virtual functions allow subclasses to redefine member functions, the subclass approach redefined as cover parent class (Override), otherwise known as rewriting.

Reference:
https://www.cnblogs.com/cxq0017/p/6074247.html

Published 71 original articles · won praise 42 · views 10000 +

Guess you like

Origin blog.csdn.net/chen1415886044/article/details/104113786