以下是根据您提供的问题描述所编写的代码:#include <iostream>#include <cmath>#include <vector>using namespace std;class Shape {public: virtual double getArea() = 0; virtual double getPerimeter() = 0;};class Rectangle : public Shape {private: double width; double height;public: Rectangle(double w, double h) : width(w), height(h) {} double getArea() { return width * height; } double getPerimeter() { return 2 * (width + height); }};class Circle : public Shape {private: double radius;public: Circle(double r) : radius(r) {} double getArea() { return M_PI * pow(radius, 2); } double getPerimeter() { return 2 * M_PI * radius; }};class Cylinder : public Circle {private: double height;public: Cylinder(double r, double h) : Circle(r), height(h) {} double getArea() { return 2 * M_PI * pow(radius, 2) + 2 * M_PI * radius * height; } double getVolume() { return M_PI * pow(radius, 2) * height; }};class Cone : public Circle {private: double height;public: Cone(double r, double h) : Circle(r), height(h) {} double getArea() { return M_PI * pow(radius, 2) + M_PI * radius * sqrt(pow(radius, 2) + pow(height, 2)); } double getVolume() { return M_PI * pow(radius, 2) * height / 3; }};class Sphere : public Circle {public: Sphere(double r) : Circle(r) {} double getArea() { return 4 * M_PI * pow(radius, 2); } double getVolume() { return 4 * M_PI * pow(radius, 3) / 3; }};class StaticCalculator {public: static double calculateTotalArea(vector<Shape*> shapes) { double totalArea = 0; for (Shape* shape : shapes) { totalArea += shape->getArea(); } return totalArea; } static double calculateTotalPerimeter(vector<Shape*> shapes) { double totalPerimeter = 0; for (Shape* shape : shapes) { totalPerimeter += shape->getPerimeter(); } return totalPerimeter; } static double calculateTotalVolume(vector<Shape*> shapes) { double totalVolume = 0; for (Shape* shape : shapes) { if (dynamic_cast<Cylinder*>(shape)) { totalVolume += dynamic_cast<Cylinder*>(shape)->getVolume(); } else if (dynamic_cast<Cone*>(shape)) { totalVolume += dynamic_cast<Cone*>(shape)->getVolume(); } else if (dynamic_cast<Sphere*>(shape)) { totalVolume += dynamic_cast<Sphere*>(shape)->getVolume(); } } return totalVolume; }};int main() { Shape* rectangle = new Rectangle(4, 5); Shape* circle = new Circle(3); Shape* sphere = new Sphere(2); Shape* cylinder = new Cylinder(2, 5); Shape* cone = new Cone(3, 4); vector<Shape*> shapes; shapes.push_back(rectangle); shapes.push_back(circle); shapes.push_back(sphere); shapes.push_back(cylinder); shapes.push_back(cone); cout << "Total area: " << StaticCalculator::calculateTotalArea(shapes) << endl; cout << "Total perimeter: " << StaticCalculator::calculateTotalPerimeter(shapes) << endl; cout << "Total volume: " << StaticCalculator::calculateTotalVolume(shapes) << endl; delete rectangle; delete circle; delete sphere; delete cylinder; delete cone; return 0;}
代码中定义了抽象基类`Shape`,以及派生类`Rectangle`、`Circle`、`Cylinder`、`Cone`和`Sphere`。每个类都重写了基类中的面积和周长(对于立体图形还有体积)的计算函数。例如,`Rectangle`类重写了`getArea`和`getPerimeter`函数,分别计算矩形的面积和周长。`Cylinder`类和`Cone`类实现了立体图形的计算函数,即重写了基类`Circle`的`getArea`函数,同时还增加了一个新的函数`getVolume`,用于计算体积。
在`StaticCalculator`类中,定义了静态成员函数`calculateTotalArea`、`calculateTotalPerimeter`和`calculateTotalVolume`,用于计算图形数组的总面积、总周长和总体积。这些静态函数遍历图形数组,通过多态性调用每个图形对象的相应函数,并将结果累加到总值中。
在主函数中,创建了一些不同类型的图形对象,并将它们加入到一个图形数组中。然后使用`StaticCalculator`类的静态函数计算并输出图形数组的总面积、总周长和总体积。
请注意,为了避免内存泄漏,需要在不再使用对象时手动释放内存,即调用`delete`操作符来销毁动态分配的对象。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |