多态类
在C ++程序中,有一个具有虚拟函数的基类和两个带有该虚拟函数的派生类的派生类,哪个类是多态类别? 派生的类或基础和派生类?
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()
{ return 0; }
};
class Rectangle: public Polygon {
public:
int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon poly;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
Polygon * ppoly3 = &poly;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly3->set_values (4,5);
cout << ppoly1->area() << '\n';
cout << ppoly2->area() << '\n';
cout << ppoly3->area() << '\n';
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般计算机科学中的多态性含义:
来自:
因此,多态性是一个涉及几个实体的概念。我要说的是,您的示例整体使用了此概念,因为
polygon
充当Rectangle
和triangle
的接口。这是上述定义的第一部分。当您使用界面时,您将执行上述定义第二部分中提到的事情。您可以制作
polygon :: aind()
纯虚拟(通过添加= 0
并省略实现),以强调polygon
polygon 只是接口。您也可以将方法从
polygon
中提取到仅具有纯虚拟方法的另一个基类中。这将是定义接口的“经典”方式。严格的C ++中的多态性含义:
根据标准(请参阅:多态对象):
因此,在您的示例中,所有三个类(
polygon
,矩形
,triangle
)都是多态。Polygon
声明一种虚拟方法,而其他方法则继承它。您还可以使用类型特征检查课程是否是多态的:请参见 :is_polymorphic 。
下面的代码使用
std :: is_polymorphic
在您的情况下:输出:
最后注:
尽管不是必须的,但要推荐使用
Override
标记您的覆盖方法。这迫使编译器确保此方法实际上覆盖了虚拟基类方法。我还添加了virtual
(在覆盖方法中也不是必须的),对我来说似乎更可读。最好避免使用命名空间std - 参见“为什么使用命名空间std”;被认为是不良练习吗?。
Polymorphism in the general computer science meaning:
From Polymorphism (computer science) - Wikipedia:
So polymorphism is a concept that involves several entities. I would say that your example as a whole uses this concept, since
Polygon
acts as an interface implemented byRectangle
andTriangle
. This is the first part of the definition above. When you'll use the interface, you will be doing what is mentioned in the second part of the above definition.You could make
Polygon::area()
pure virtual (by adding=0
and omitting the implementation), in order to emphasize the fact thatPolygon
is only an interface.You could also extract the methods from
Polygon
into another base class with only pure virtual methods. This would be the "classic" way of defining an interface.Polymorphism in a strict C++ meaning:
According to the standard (see: Polymorphic objects):
Therefore, in your example, all three classes (
Polygon
,Rectangle
,Triangle
) are polymorphic.Polygon
declares a virtual method, and the others inherit it.You could also use type traits to check if a class is polymorphic: see
std::is_polymorphic
.The code below demonstrates using
std::is_polymorphic
in your case:Output:
2 last notes:
Although it's not a must, it's recomended to tag your overriding methods with
override
. This forces the compiler to ensure this method actually overrides a virtual base class method. I also addvirtual
(also not a must in an overriding method), seems more readable to me.Better to avoid
using namespace std
- see Why is "using namespace std;" considered bad practice?.