gpt4 book ai didi

c++ - C++ 中的多态性和我的考试

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:38 26 4
gpt4 key购买 nike

几天前我参加了 C++ 考试,我得到了这个问题,但我发现它对我来说不是很清楚,问题是:

Explain the difference among the terms Polymorphism, virtual function and overriding. By using example getting the area of shape for rectangle and triangle, write two different snippet codes to show the implementation of polymorphism and implementation of polymorphism and virtual function.
Give example of output from both snippet codes.

而根据 Absolute C++ 5th p669 多态性的定义是:

Polymorphism refers to the ability to associate many meanings to one function name by means of the late-binding mechanism. Thus, polymorphism, late binding, and virtual functions are really all the same topic.

我可以从这个定义中理解不使用虚函数就没有多态性,不是吗?所以这个问题没有两个不同的片段实现,对吗?只有一个通过使用虚函数

我的问题是:这是一个有效的问题吗?

最佳答案

My question is: Is this a valid question to be asked?

不,这与看起来的预期答案“只有虚函数在 C++ 中提供多态性” 无关。这个问题太狭隘了,容易产生误导。

Polymorphism refers to the ability to associate many meanings to one function name by means of the late-binding mechanism. Thus, polymorphism, late binding, and virtual functions are really all the same topic.

我可以从这个定义中理解不使用虚函数就没有多态性,不是吗?

实际上,您可以在没有 virtual 函数的情况下拥有多态性。

这叫做静态多态,查找CRTP patternSFINAE .

嗯,后期绑定(bind) 的强调实际上缩小了动态多态性和(纯)虚拟 函数的问题。但恕我直言,这仍然是一个糟糕的考试,而且问题太狭窄/不明确。

关于c++ - C++ 中的多态性和我的考试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com