多态类

发布于 2025-01-27 01:54:18 字数 930 浏览 1 评论 0 原文

在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 = &rect;
  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;
}

In a C++ program in which there is a base class with a virtual function and two derived classes with redefinition of that virtual function , which class is a polymorphic class?
either derived classes or base and derived classes together?

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2025-02-03 01:54:18

一般计算机科学中的多态性含义:

来自

多态性是为不同类型的实体提供单个接口[1]或使用单个符号代表多种不同类型的实体。

因此,多态性是一个涉及几个实体的概念。我要说的是,您的示例整体使用了此概念,因为 polygon 充当 Rectangle triangle 的接口。这是上述定义的第一部分。当您使用界面时,您将执行上述定义第二部分中提到的事情。

您可以制作 polygon :: aind()纯虚拟(通过添加 = 0 并省略实现),以强调 polygon polygon 只是接口。

您也可以将方法从 polygon 中提取到仅具有纯虚拟方法的另一个基类中。这将是定义接口的“经典”方式。

严格的C ++中的多态性含义:

根据标准(请参阅:多态对象):

宣布或继承至少一个虚拟函数的类类型的对象是多态对象。

因此,在您的示例中,所有三个类( polygon 矩形 triangle )都是多态 Polygon 声明一种虚拟方法,而其他方法则继承它。

您还可以使用类型特征检查课程是否是多态的:请参见 :is_polymorphic

下面的代码使用 std :: is_polymorphic 在您的情况下:

#include <iostream>

class A {};

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:
    virtual int area() override { return width * height; }
};

class Triangle : public Polygon {
public:
    virtual int area() override { return (width * height / 2); }
};

int main() {
    std::cout << "A: " << std::is_polymorphic<A>::value << std::endl;
    std::cout << "Polygon: " << std::is_polymorphic<Polygon>::value << std::endl;
    std::cout << "Rectangle: " << std::is_polymorphic<Rectangle>::value << std::endl;
    std::cout << "Triangle: " << std::is_polymorphic<Triangle>::value << std::endl;
    return 0;
}

输出:

A: 0
Polygon: 1
Rectangle: 1
Triangle: 1

最后注:

  1. 尽管不是必须的,但要推荐使用 Override 标记您的覆盖方法。这迫使编译器确保此方法实际上覆盖了虚拟基类方法。我还添加了 virtual (在覆盖方法中也不是必须的),对我来说似乎更可读。

  2. 最好避免使用命名空间std - 参见“为什么使用命名空间std”;被认为是不良练习吗?

Polymorphism in the general computer science meaning:

From Polymorphism (computer science) - Wikipedia:

polymorphism is the provision of a single interface to entities of different types[1] or the use of a single symbol to represent multiple different types.

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 by Rectangle and Triangle. 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 that Polygon 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):

Objects of a class type that declares or inherits at least one virtual function are 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:

#include <iostream>

class A {};

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:
    virtual int area() override { return width * height; }
};

class Triangle : public Polygon {
public:
    virtual int area() override { return (width * height / 2); }
};

int main() {
    std::cout << "A: " << std::is_polymorphic<A>::value << std::endl;
    std::cout << "Polygon: " << std::is_polymorphic<Polygon>::value << std::endl;
    std::cout << "Rectangle: " << std::is_polymorphic<Rectangle>::value << std::endl;
    std::cout << "Triangle: " << std::is_polymorphic<Triangle>::value << std::endl;
    return 0;
}

Output:

A: 0
Polygon: 1
Rectangle: 1
Triangle: 1

2 last notes:

  1. 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 add virtual (also not a must in an overriding method), seems more readable to me.

  2. Better to avoid using namespace std - see Why is "using namespace std;" considered bad practice?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文