鱼C论坛

 找回密码
 立即注册
查看: 1476|回复: 1

[已解决]重写派生类矩形、圆形、圆的面积函数和周长函数

[复制链接]
发表于 2023-12-21 21:20:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1)重写派生类矩形、圆形、圆的面积函数和周长函数;

2)重写派生类球体、圆柱体、圆锥体的面积函数和体积函数。

3)实现一个静态类,该静态类用于分别计算图形数组的面积、周长和体积之和。



主函数中的输出语句改为如下语句:

cout << "Sum of Area: " << sum_of_area << "\nSum of Perimeter: " << sum_of_perimeter << "\nSum of Volume: " << sum_of_volume << endl;



这题C++代码怎么写
最佳答案
2023-12-21 21:20:45
以下是根据您提供的问题描述所编写的代码:
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. using namespace std;
  5. class Shape {
  6. public:
  7.     virtual double getArea() = 0;
  8.     virtual double getPerimeter() = 0;
  9. };
  10. class Rectangle : public Shape {
  11. private:
  12.     double width;
  13.     double height;
  14. public:
  15.     Rectangle(double w, double h) : width(w), height(h) {}
  16.     double getArea() {
  17.         return width * height;
  18.     }
  19.     double getPerimeter() {
  20.         return 2 * (width + height);
  21.     }
  22. };
  23. class Circle : public Shape {
  24. private:
  25.     double radius;
  26. public:
  27.     Circle(double r) : radius(r) {}
  28.     double getArea() {
  29.         return M_PI * pow(radius, 2);
  30.     }
  31.     double getPerimeter() {
  32.         return 2 * M_PI * radius;
  33.     }
  34. };
  35. class Cylinder : public Circle {
  36. private:
  37.     double height;
  38. public:
  39.     Cylinder(double r, double h) : Circle(r), height(h) {}
  40.     double getArea() {
  41.         return 2 * M_PI * pow(radius, 2) + 2 * M_PI * radius * height;
  42.     }
  43.     double getVolume() {
  44.         return M_PI * pow(radius, 2) * height;
  45.     }
  46. };
  47. class Cone : public Circle {
  48. private:
  49.     double height;
  50. public:
  51.     Cone(double r, double h) : Circle(r), height(h) {}
  52.     double getArea() {
  53.         return M_PI * pow(radius, 2) + M_PI * radius * sqrt(pow(radius, 2) + pow(height, 2));
  54.     }
  55.     double getVolume() {
  56.         return M_PI * pow(radius, 2) * height / 3;
  57.     }
  58. };
  59. class Sphere : public Circle {
  60. public:
  61.     Sphere(double r) : Circle(r) {}
  62.     double getArea() {
  63.         return 4 * M_PI * pow(radius, 2);
  64.     }
  65.     double getVolume() {
  66.         return 4 * M_PI * pow(radius, 3) / 3;
  67.     }
  68. };
  69. class StaticCalculator {
  70. public:
  71.     static double calculateTotalArea(vector<Shape*> shapes) {
  72.         double totalArea = 0;
  73.         for (Shape* shape : shapes) {
  74.             totalArea += shape->getArea();
  75.         }
  76.         return totalArea;
  77.     }
  78.     static double calculateTotalPerimeter(vector<Shape*> shapes) {
  79.         double totalPerimeter = 0;
  80.         for (Shape* shape : shapes) {
  81.             totalPerimeter += shape->getPerimeter();
  82.         }
  83.         return totalPerimeter;
  84.     }
  85.     static double calculateTotalVolume(vector<Shape*> shapes) {
  86.         double totalVolume = 0;
  87.         for (Shape* shape : shapes) {
  88.             if (dynamic_cast<Cylinder*>(shape)) {
  89.                 totalVolume += dynamic_cast<Cylinder*>(shape)->getVolume();
  90.             } else if (dynamic_cast<Cone*>(shape)) {
  91.                 totalVolume += dynamic_cast<Cone*>(shape)->getVolume();
  92.             } else if (dynamic_cast<Sphere*>(shape)) {
  93.                 totalVolume += dynamic_cast<Sphere*>(shape)->getVolume();
  94.             }
  95.         }
  96.         return totalVolume;
  97.     }
  98. };
  99. int main() {
  100.     Shape* rectangle = new Rectangle(4, 5);
  101.     Shape* circle = new Circle(3);
  102.     Shape* sphere = new Sphere(2);
  103.     Shape* cylinder = new Cylinder(2, 5);
  104.     Shape* cone = new Cone(3, 4);
  105.     vector<Shape*> shapes;
  106.     shapes.push_back(rectangle);
  107.     shapes.push_back(circle);
  108.     shapes.push_back(sphere);
  109.     shapes.push_back(cylinder);
  110.     shapes.push_back(cone);
  111.     cout << "Total area: " << StaticCalculator::calculateTotalArea(shapes) << endl;
  112.     cout << "Total perimeter: " << StaticCalculator::calculateTotalPerimeter(shapes) << endl;
  113.     cout << "Total volume: " << StaticCalculator::calculateTotalVolume(shapes) << endl;
  114.     delete rectangle;
  115.     delete circle;
  116.     delete sphere;
  117.     delete cylinder;
  118.     delete cone;
  119.     return 0;
  120. }
复制代码

代码中定义了抽象基类`Shape`,以及派生类`Rectangle`、`Circle`、`Cylinder`、`Cone`和`Sphere`。每个类都重写了基类中的面积和周长(对于立体图形还有体积)的计算函数。例如,`Rectangle`类重写了`getArea`和`getPerimeter`函数,分别计算矩形的面积和周长。`Cylinder`类和`Cone`类实现了立体图形的计算函数,即重写了基类`Circle`的`getArea`函数,同时还增加了一个新的函数`getVolume`,用于计算体积。

在`StaticCalculator`类中,定义了静态成员函数`calculateTotalArea`、`calculateTotalPerimeter`和`calculateTotalVolume`,用于计算图形数组的总面积、总周长和总体积。这些静态函数遍历图形数组,通过多态性调用每个图形对象的相应函数,并将结果累加到总值中。

在主函数中,创建了一些不同类型的图形对象,并将它们加入到一个图形数组中。然后使用`StaticCalculator`类的静态函数计算并输出图形数组的总面积、总周长和总体积。

请注意,为了避免内存泄漏,需要在不再使用对象时手动释放内存,即调用`delete`操作符来销毁动态分配的对象。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-21 21:20:45 | 显示全部楼层    本楼为最佳答案   
以下是根据您提供的问题描述所编写的代码:
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. using namespace std;
  5. class Shape {
  6. public:
  7.     virtual double getArea() = 0;
  8.     virtual double getPerimeter() = 0;
  9. };
  10. class Rectangle : public Shape {
  11. private:
  12.     double width;
  13.     double height;
  14. public:
  15.     Rectangle(double w, double h) : width(w), height(h) {}
  16.     double getArea() {
  17.         return width * height;
  18.     }
  19.     double getPerimeter() {
  20.         return 2 * (width + height);
  21.     }
  22. };
  23. class Circle : public Shape {
  24. private:
  25.     double radius;
  26. public:
  27.     Circle(double r) : radius(r) {}
  28.     double getArea() {
  29.         return M_PI * pow(radius, 2);
  30.     }
  31.     double getPerimeter() {
  32.         return 2 * M_PI * radius;
  33.     }
  34. };
  35. class Cylinder : public Circle {
  36. private:
  37.     double height;
  38. public:
  39.     Cylinder(double r, double h) : Circle(r), height(h) {}
  40.     double getArea() {
  41.         return 2 * M_PI * pow(radius, 2) + 2 * M_PI * radius * height;
  42.     }
  43.     double getVolume() {
  44.         return M_PI * pow(radius, 2) * height;
  45.     }
  46. };
  47. class Cone : public Circle {
  48. private:
  49.     double height;
  50. public:
  51.     Cone(double r, double h) : Circle(r), height(h) {}
  52.     double getArea() {
  53.         return M_PI * pow(radius, 2) + M_PI * radius * sqrt(pow(radius, 2) + pow(height, 2));
  54.     }
  55.     double getVolume() {
  56.         return M_PI * pow(radius, 2) * height / 3;
  57.     }
  58. };
  59. class Sphere : public Circle {
  60. public:
  61.     Sphere(double r) : Circle(r) {}
  62.     double getArea() {
  63.         return 4 * M_PI * pow(radius, 2);
  64.     }
  65.     double getVolume() {
  66.         return 4 * M_PI * pow(radius, 3) / 3;
  67.     }
  68. };
  69. class StaticCalculator {
  70. public:
  71.     static double calculateTotalArea(vector<Shape*> shapes) {
  72.         double totalArea = 0;
  73.         for (Shape* shape : shapes) {
  74.             totalArea += shape->getArea();
  75.         }
  76.         return totalArea;
  77.     }
  78.     static double calculateTotalPerimeter(vector<Shape*> shapes) {
  79.         double totalPerimeter = 0;
  80.         for (Shape* shape : shapes) {
  81.             totalPerimeter += shape->getPerimeter();
  82.         }
  83.         return totalPerimeter;
  84.     }
  85.     static double calculateTotalVolume(vector<Shape*> shapes) {
  86.         double totalVolume = 0;
  87.         for (Shape* shape : shapes) {
  88.             if (dynamic_cast<Cylinder*>(shape)) {
  89.                 totalVolume += dynamic_cast<Cylinder*>(shape)->getVolume();
  90.             } else if (dynamic_cast<Cone*>(shape)) {
  91.                 totalVolume += dynamic_cast<Cone*>(shape)->getVolume();
  92.             } else if (dynamic_cast<Sphere*>(shape)) {
  93.                 totalVolume += dynamic_cast<Sphere*>(shape)->getVolume();
  94.             }
  95.         }
  96.         return totalVolume;
  97.     }
  98. };
  99. int main() {
  100.     Shape* rectangle = new Rectangle(4, 5);
  101.     Shape* circle = new Circle(3);
  102.     Shape* sphere = new Sphere(2);
  103.     Shape* cylinder = new Cylinder(2, 5);
  104.     Shape* cone = new Cone(3, 4);
  105.     vector<Shape*> shapes;
  106.     shapes.push_back(rectangle);
  107.     shapes.push_back(circle);
  108.     shapes.push_back(sphere);
  109.     shapes.push_back(cylinder);
  110.     shapes.push_back(cone);
  111.     cout << "Total area: " << StaticCalculator::calculateTotalArea(shapes) << endl;
  112.     cout << "Total perimeter: " << StaticCalculator::calculateTotalPerimeter(shapes) << endl;
  113.     cout << "Total volume: " << StaticCalculator::calculateTotalVolume(shapes) << endl;
  114.     delete rectangle;
  115.     delete circle;
  116.     delete sphere;
  117.     delete cylinder;
  118.     delete cone;
  119.     return 0;
  120. }
复制代码

代码中定义了抽象基类`Shape`,以及派生类`Rectangle`、`Circle`、`Cylinder`、`Cone`和`Sphere`。每个类都重写了基类中的面积和周长(对于立体图形还有体积)的计算函数。例如,`Rectangle`类重写了`getArea`和`getPerimeter`函数,分别计算矩形的面积和周长。`Cylinder`类和`Cone`类实现了立体图形的计算函数,即重写了基类`Circle`的`getArea`函数,同时还增加了一个新的函数`getVolume`,用于计算体积。

在`StaticCalculator`类中,定义了静态成员函数`calculateTotalArea`、`calculateTotalPerimeter`和`calculateTotalVolume`,用于计算图形数组的总面积、总周长和总体积。这些静态函数遍历图形数组,通过多态性调用每个图形对象的相应函数,并将结果累加到总值中。

在主函数中,创建了一些不同类型的图形对象,并将它们加入到一个图形数组中。然后使用`StaticCalculator`类的静态函数计算并输出图形数组的总面积、总周长和总体积。

请注意,为了避免内存泄漏,需要在不再使用对象时手动释放内存,即调用`delete`操作符来销毁动态分配的对象。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 23:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表