我试图理解多态性,但我不明白如果静态多态性可以很好地调用类的成员,为什么我们需要运行时多态性。
就像,假设这是一个问题。
#include <bits/stdc++.h>
using namespace std;
class base{
public:
virtual void fun(){
cout<<"base called"<<endl;
}
};
class derived:public base{
public:
void fun(){
cout<<"derived called"<<endl;
}
};
int main() {
base b,*b1;
derived d;
b1 = &d;
b1->fun();
// b.fun();
// d.fun();
}
假设这是我的代码,我想访问派生类或基类的函数,我可以通过简单地创建该类的对象来做到这一点,所以如果没有问题,那么为什么我们尝试使用引用调用对象(运行时多态性)。
有人可以解释对运行时多态性的实际需求吗,或者如果可能的话,您可以使用任何 来解释它吗?真实场景 ??
最佳答案
多态性被认为是面向对象编程的重要特征之一。 C++中的多态主要分为两种:
现在考虑以下场景。
假设我们有一个名为
Shape
的基类它具有以下界面。class Shape {
public:
Shape(int init_x, int init_y);
virtual ~Shape() = default;
virtual void scale(int s) = 0;
protected:
int x;
int y;
};
现在我们要继承另外两个名为
Rectangle
的类。和 Circle
从中。class Rectangle : public Shape {
public:
Rectangle(int init_x, int init_y, int w, int h);
void scale(int s) override;
private:
int width;
int height;
};
class Circle : public Shape {
public:
Circle(int init_x, int init_y, int r);
void scale(int s) override;
private:
int radius;
};
您可能知道,圆形和矩形的
scale
有不同的实现。方法,所以我无法在 Shape
中实现它类(class)。现在假设我们有一个程序将所有形状存储在一个容器中,如
vector<Shape*>
。 (例如,它一次从用户那里获取所有形状并将它们存储在此容器中)。如果我们想使用
scale
方法,我们实际上不知道我们正在处理哪种形状,但它会绑定(bind)我们的 scale
方法调用其适当的实现。
关于c++ - 为什么我们实际上需要运行时多态性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62113098/