实现一个shape类

您所在的位置:网站首页 python几何图形设计一个表示几何图形的shape类 实现一个shape类

实现一个shape类

2024-07-12 19:11| 来源: 网络整理| 查看: 265

题目

这个问题包括实现一个抽象类Shape,从这个抽象类派生出具体的子类Square、Rectangle、Triangle和Circle。为了简化,这里的三角形指的是一个直角三角形(一个有90°角的三角形)。Shape类应该具有允许访问区域、周长和角的数量的成员函数。这些函数应该命名为CalculateArea、CalculatePerimeter和NumberCorners。由于所有子类都应该具有相同的函数,但它们的计算可能不同,因此您应该考虑将其中一些类编写为虚函数。此外,这四种形状不能以类似的方式创建。因此,它们应该有不同的构造函数。(所有参数都是double类型。)

代码 #include #include #include #include #include #include const double PI = 3.14159; /*********************************************************************** * Declaration/Definition of the base-class Shape * ***********************************************************************/ class Shape { public: // constructors and destructor Shape() {}; virtual ~Shape() {}; // Accessors; you should decide which should be virtual or pure virtual. virtual double CalculateArea() const =0; virtual double CalculatePerimeter() const = 0; virtual int NumberCorners() const = 0; private: // member variables; }; /*********************************************************************** * Declaration/Definition of the child-classes * ***********************************************************************/ class Square : public Shape { public: Square(double a1) :Shape(){ a = a1; } ~Square() { std::cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3