c ++多态_C ++中的多态性和方法覆盖

本文深入探讨了C++中的多态性和函数重写概念,解释了如何通过方法重写实现多态,以及在继承中重写函数的具体要求。通过示例展示了基类和派生类中函数调用的不同绑定方式及其结果。

c ++多态

In this tutorial we will cover the concepts of Polymorphism in C++ and Function overriding in C++. We will also see both of these in action using simple code examples.

在本教程中,我们将介绍C ++中的多态性和C ++中的函数重写的概念。 我们还将使用简单的代码示例来同时查看这两种方法。

C ++中的多态 (Polymorphism in C++)

Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is done, by method overriding, when both super and sub class have member function with same declaration bu different definition.

多态性意味着一件事物具有多种形式。 在继承中,当父类和子类都具有具有相同声明但定义不同的成员函数时,通过方法重写实现多态。

C ++中的方法重写 (Method Overriding in C++)

If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding

如果我们将一个类继承到派生类中,并在派生类中再次为基类的功能之一提供一个定义,则该功能被认为已被覆盖 ,这种机制称为“ 函数覆盖”。

覆盖功能的要求 (Requirements for Overriding a Function)

  1. Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.

    继承应该在那里。 函数重写不能在类内完成。 为此,我们需要一个派生类和一个基类。

  2. Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.

    在基类和派生类中,重新定义的函数必须具有完全相同的声明,即相同的名称,相同的返回类型和相同的参数列表。

C ++中的函数重写示例 (Example of Function Overriding in C++)

class Base
{
    public:
    void show()
    {
        cout << "Base class";
    }
};

class Derived:public Base
{
    public:
    void show()
    {
        cout << "Derived Class";
    }
}

In this example, function show() is overridden in the derived class. Now let us study how these overridden functions are called in main() function.

在此示例中,函数show()在派生类中被覆盖。 现在让我们研究如何在main()函数中调用这些重写的函数。

类对象的函数调用绑定 (Function Call Binding with class Objects)

Connecting the function call to the function body is called Binding. When it is done before the program is run, its called Early Binding or Static Binding or Compile-time Binding.

将函数调用连接到函数主体称为Binding 。 在程序运行之前完成此操作时,其称为“ 早期绑定”或“ 静态绑定”或“ 编译时绑定”。

class Base
{
    public:
    void shaow()
    {
        cout << "Base class\n";
    }
};

class Derived:public Base
{
    public:
    void show()
    {
        cout << "Derived Class\n";
    }
}

int main()
{
    Base b;       //Base class object
    Derived d;     //Derived class object
    b.show();     //Early Binding Ocuurs
    d.show();   
}

Base class Derived class

基类派生类

In the above example, we are calling the overrided function using Base class and Derived class object. Base class object will call base version of the function and derived class's object will call the derived version of the function.

在上面的示例中,我们使用基类和派生类对象调用重写的函数。 基类对象将调用函数的基本版本,派生类的对象将调用函数的派生版本。

使用基类指针的函数调用绑定 (Function Call Binding using Base class Pointer)

But when we use a Base class's pointer or reference to hold Derived class's object, then Function call Binding gives some unexpected results.

但是,当我们使用基类的指针或引用来保存派生类的对象时,函数调用绑定将产生一些意外结果。

class Base
{
    public:
    void show()
    {
        cout << "Base class\n";
    }
};

class Derived:public Base
{
    public:
    void show()
    {
        cout << "Derived Class\n";
    }
}

int main()
{
    Base* b;       //Base class pointer
    Derived d;     //Derived class object
    b = &d;
    b->show();     //Early Binding Occurs
}

Base class

基类

In the above example, although, the object is of Derived class, still Base class's method is called. This happens due to Early Binding.

在上面的示例中,尽管对象是Derived类,但仍调用Base类的方法。 发生这种情况是由于早期绑定。

Compiler on seeing Base class's pointer, set call to Base class's show() function, without knowing the actual object type.

编译器在看到基类的指针后 ,将调用设置为基类的show()函数,而不知道实际的对象类型。

翻译自: https://www.studytonight.com/cpp/function-overriding.php

c ++多态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值