简体   繁体   English

方法覆盖(没有虚拟方法或指针)是否被视为多态的一部分?

[英]Is method overriding (without virtual methods or pointers) considered a part of Polymorphism?

First of all, I would like to say that I searched for similar questions, but the answers seem to focus on different things. 首先,我想说的是,我搜索了类似的问题,但是答案似乎集中在不同的事情上。

I started learning C++ but I have a problem understanding what exactly is considered as polymorphism . 我开始学习C ++,但是在理解什么才是多态性时遇到了一个问题。 I have written two different programs. 我写了两个不同的程序。

In the first, I don't use virtual methods or pointers to objects. 首先,我不使用虚拟方法或对象的指针。

In the second, I use a virtual method (area) and an array with pointers to objects. 在第二篇文章中,我使用了一个虚拟方法 (区域)和一个带有指向对象的指针的数组。

I understand that the second program is using polymorphism (a parent class pointer is used to point to a child class object). 我知道第二个程序正在使用多态性(父类指针用于指向子类对象)。

But since the first program produces the exact same results by overriding the function area, is it considered to be using polymorphism too? 但是,由于第一个程序通过覆盖功能区产生了完全相同的结果,因此是否也考虑使用多态性?

Program 1 程序1

#include <iostream>

using namespace std;

class Polygon {
protected:
    float base;
    float height;
public:
    Polygon(): base(0), height(0) {}
    Polygon(float b, float h): base(b), height(h) {}
    float area() {return -1;}
};

class Triangle:public Polygon {
public:
    Triangle(): Polygon() {}
    Triangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height/2;}
};

class Rectangle:public Polygon {
public:
    Rectangle(): Polygon() {}
    Rectangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height;}
};

int main() {
    //Overriding area method without pointers
    Triangle t1, t2(10,10);
    Rectangle r1, r2(5,5);
    cout<<t1.area()<<endl;  //Output: 0
    cout<<t2.area()<<endl;  //Output: 50
    cout<<r1.area()<<endl;  //Output: 0
    cout<<r2.area()<<endl;  //Output: 25
    cout<<t1.Polygon::area()<<endl; //Output: -1
}

Program 2 程序2

#include <iostream>

using namespace std;

class Polygon {
protected:
    float base;
    float height;
public:
    Polygon(): base(0), height(0) {}
    Polygon(float b, float h): base(b), height(h) {}
    virtual float area() {return -1;} //virtual method area
};

class Triangle:public Polygon {
public:
    Triangle(): Polygon() {}
    Triangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height/2;}
};

class Rectangle:public Polygon {
public:
    Rectangle(): Polygon() {}
    Rectangle(float b, float h): Polygon(b,h) {}
    float area() {return base*height;}
};

int main() {
    //Polymorphism
    Triangle t1, t2(10,10);
    Rectangle r1, r2(5,5);
    Polygon *ptr[]={&t1,&t2,&r1,&r2};
    cout<<ptr[0]->area()<<endl;  //Output: 0
    cout<<ptr[1]->area()<<endl; //Output: 50
    cout<<ptr[2]->area()<<endl; //Output: 0
    cout<<ptr[3]->area()<<endl; //Output: 25
    cout<<ptr[0]->Polygon::area()<<endl;    //Output: -1
}

But since the first program produces the exact same results by overriding the function area, is it considered to be using polymorphism too? 但是,由于第一个程序通过覆盖功能区产生了完全相同的结果,因此是否也考虑使用多态性?

Well, no. 好吧,不。 There's no overriding in the 1st sample, but shadowing of the base class function implementations. 在第一个示例中没有覆盖 ,但是隐藏了基类函数的实现。


That your examples produce the same results is because of the fact, that in your 1st sample 您的示例产生相同的结果是因为事实上,在您的第一个示例中

    cout<<t1.area()<<endl;  //Output: 0
    cout<<t2.area()<<endl;  //Output: 50
    cout<<r1.area()<<endl;  //Output: 0
    cout<<r2.area()<<endl;  //Output: 25

you are calling functions with concrete class instances. 您正在使用具体的类实例调用函数。

Something like 就像是

    Polygon *ptr[]={&t1,&t2,&r1,&r2};
    cout<<ptr[0]->area()<<endl;  //Output: 0
    cout<<ptr[1]->area()<<endl; //Output: 50
    cout<<ptr[2]->area()<<endl; //Output: 0
    cout<<ptr[3]->area()<<endl; //Output: 25

would fail being used with this code. 将无法与此代码一起使用。


Polymorphism means that you have some interface declared, that might be accessed through a base class reference. 多态意味着您声明了一些接口,可以通过基类引用对其进行访问。

In c++ you have generally the choice of 在C ++中,您通常可以选择

  • Dynamic polymorphism (ie abstract/virtual base classes) and resolve overridden function calls at run time 动态多态性(即抽象/虚拟基类),并在运行时解析覆盖的函数调用
  • Static polymorphism (ie using the CRTP to resolve overridden function calls at compile time) 静态多态性(即在编译时使用CRTP解析覆盖的函数调用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM