每个TwoDimensionShape包含getArea函数,计算二维图形的面积。每个ThreeDimensionShape包含getArea和getVolume函数,计算三维图形的表面积和体积。在主函数中,定义shape的指针数组,分别指向层次结构中的各个具体对象。输出每个对象。在循环中,处理所有图形,确定每个图形是TwoDimensionShape还是ThreeDimensionShape,如果图形是TwoDimensionShape,则显示它的面积。如果图形是ThreeDimensionShape,则显示三维图形的表面积和体积。 程序说明: 1、dynamic_cast强制类型转换 dynamic_cast为C++的强制类型转换方法,主要用于基类和派生类直接的类型转换。更多细节请自行google。 2、输出格式: cout << "Square with side length " <<...<< "; center at (" <<...<< ", " <<... << ")"; cout << "Circle with radius " << ... << "; center at (" << ... << ", " << ...<< ")"; cout << "Cube with side length " <<... << "; center at (" << ... << ", " << ... << ")"; cout << "Sphere with radius " << ... << "; center at (" << ... << ", " << ...<< ")";写个代码
时间: 2023-07-19 19:22:53 浏览: 102
以下是题解:
```cpp
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Shape {
public:
virtual ~Shape() {}
};
class TwoDimensionShape : public Shape {
public:
virtual double getArea() const = 0;
};
class ThreeDimensionShape : public Shape {
public:
virtual double getArea() const = 0;
virtual double getVolume() const = 0;
};
class Point {
public:
Point(double x = 0, double y = 0, double z = 0) : _x(x), _y(y), _z(z) {}
double getX() const { return _x; }
double getY() const { return _y; }
double getZ() const { return _z; }
private:
double _x, _y, _z;
};
class Square : public TwoDimensionShape {
public:
Square(double side, double x, double y) : _side(side), _center(x, y) {}
double getArea() const { return _side * _side; }
Point getCenter() const { return _center; }
private:
double _side;
Point _center;
};
class Circle : public TwoDimensionShape {
public:
Circle(double radius, double x, double y) : _radius(radius), _center(x, y) {}
double getArea() const { return M_PI * _radius * _radius; }
Point getCenter() const { return _center; }
private:
double _radius;
Point _center;
};
class Cube : public ThreeDimensionShape {
public:
Cube(double side, double x, double y, double z) : _side(side), _center(x, y, z) {}
double getArea() const { return 6 * _side * _side; }
double getVolume() const { return _side * _side * _side; }
Point getCenter() const { return _center; }
private:
double _side;
Point _center;
};
class Sphere : public ThreeDimensionShape {
public:
Sphere(double radius, double x, double y, double z) : _radius(radius), _center(x, y, z) {}
double getArea() const { return 4 * M_PI * _radius * _radius; }
double getVolume() const { return 4.0 / 3.0 * M_PI * _radius * _radius * _radius; }
Point getCenter() const { return _center; }
private:
double _radius;
Point _center;
};
int main() {
Shape* shapes[] = {
new Square(2.0, 0.0, 0.0),
new Circle(1.5, 1.0, 1.0),
new Cube(3.0, 2.0, 2.0, 2.0),
new Sphere(2.5, 3.0, 3.0, 3.0)
};
int n = sizeof(shapes) / sizeof(shapes[0]);
for (int i = 0; i < n; i++) {
if (auto p = dynamic_cast<TwoDimensionShape*>(shapes[i])) {
cout << "Square with side length " << sqrt(p->getArea())
<< "; center at (" << p->getCenter().getX() << ", " << p->getCenter().getY() << ")" << endl;
} else if (auto p = dynamic_cast<ThreeDimensionShape*>(shapes[i])) {
cout << "Cube with side length " << pow(p->getArea() / 6, 1.0 / 3.0)
<< "; center at (" << p->getCenter().getX() << ", " << p->getCenter().getY() << ", " << p->getCenter().getZ() << ")" << endl;
cout << "Sphere with radius " << pow(p->getVolume() * 3 / (4 * M_PI), 1.0 / 3.0)
<< "; center at (" << p->getCenter().getX() << ", " << p->getCenter().getY() << ", " << p->getCenter().getZ() << ")" << endl;
}
}
for (int i = 0; i < n; i++) {
delete shapes[i];
}
return 0;
}
```
思路:
本题要求定义两个抽象类 TwoDimensionShape 和 ThreeDimensionShape,分别表示二维图形和三维图形,并在它们的基础上定义 Square、Circle、Cube 和 Sphere 四个具体类。
在主函数中,定义一个指针数组 shapes,分别指向层次结构中的各个具体对象。在循环中,处理所有图形,确定每个图形是 TwoDimensionShape 还是 ThreeDimensionShape,如果图形是 TwoDimensionShape,则显示它的面积。如果图形是 ThreeDimensionShape,则显示三维图形的表面积和体积。
细节:
- 在 Square 和 Circle 中,使用 Point 类保存图形的中心坐标。
- 在 Cube 和 Sphere 中,也使用 Point 类保存图形的中心坐标。
- 在判断图形类型时,使用 dynamic_cast 进行类型转换,如果转换成功,则说明该图形是 TwoDimensionShape 或 ThreeDimensionShape 子类,然后调用相应的函数进行输出。
- 在输出 Cube 和 Sphere 时,分别使用公式计算它们的边长和半径。
时间复杂度:$O(n)$(其中 $n$ 为图形数)
空间复杂度:$O(n)$
完整代码:
阅读全文