13.13(Shape 类层次结构)实现习题12.7(基于图12.3中的类层次结构)设计的Shape类 层次结构。每个TwoDimensionalShape类都包含成员函数getArea,用于计算二维形状的面积。每个ThreeDimensionalShape类都包含成员函数getArea和 getVolume,分别用于算三维形状的表面积和体积。编写一个程序,使用一个Shape指针的vector对象,它的元素指向类层次中每个具体类的对象。程序应打印输出该vector对象每个元素所指的对象。同样,在处理该vector对象中所有形状的循环中,程序应判断每个形状到底是TwoDimensionalShape 类的还是ThreeDimensionalShape 类的。如果属于TwoDimensionalShape类,就显示其面积,如果属于ThreeDimensionalShape类,就显示其表面积和体积。
时间: 2023-06-24 10:08:20 浏览: 163
以下是实现Shape类层次结构的代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Shape {
public:
virtual void print() const = 0; // 纯虚函数,用于输出形状信息
};
class TwoDimensionalShape : public Shape {
public:
virtual double getArea() const = 0; // 纯虚函数,用于计算二维形状的面积
};
class ThreeDimensionalShape : public Shape {
public:
virtual double getArea() const = 0; // 纯虚函数,用于计算三维形状的表面积
virtual double getVolume() const = 0; // 纯虚函数,用于计算三维形状的体积
};
class Circle : public TwoDimensionalShape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double getArea() const {
return 3.14159 * radius * radius;
}
void print() const {
cout << "Circle with radius " << radius << endl;
}
};
class Square : public TwoDimensionalShape {
private:
double side;
public:
Square(double s) : side(s) {}
double getArea() const {
return side * side;
}
void print() const {
cout << "Square with side " << side << endl;
}
};
class Sphere : public ThreeDimensionalShape {
private:
double radius;
public:
Sphere(double r) : radius(r) {}
double getArea() const {
return 4 * 3.14159 * radius * radius;
}
double getVolume() const {
return 4.0 / 3.0 * 3.14159 * radius * radius * radius;
}
void print() const {
cout << "Sphere with radius " << radius << endl;
}
};
class Cube : public ThreeDimensionalShape {
private:
double side;
public:
Cube(double s) : side(s) {}
double getArea() const {
return 6 * side * side;
}
double getVolume() const {
return side * side * side;
}
void print() const {
cout << "Cube with side " << side << endl;
}
};
int main() {
vector<Shape*> shapes;
shapes.push_back(new Circle(2.0));
shapes.push_back(new Square(3.0));
shapes.push_back(new Sphere(2.0));
shapes.push_back(new Cube(3.0));
for (auto s : shapes) {
s->print();
if (dynamic_cast<TwoDimensionalShape*>(s)) {
cout << "Area: " << dynamic_cast<TwoDimensionalShape*>(s)->getArea() << endl;
} else if (dynamic_cast<ThreeDimensionalShape*>(s)) {
auto three_d_shape = dynamic_cast<ThreeDimensionalShape*>(s);
cout << "Surface Area: " << three_d_shape->getArea() << endl;
cout << "Volume: " << three_d_shape->getVolume() << endl;
}
cout << endl;
}
for (auto s : shapes) {
delete s;
}
return 0;
}
```
该程序定义了Shape类作为基类,其中有一个纯虚函数print,用于输出形状信息。TwoDimensionalShape和ThreeDimensionalShape分别继承自Shape类,并且都包含一个纯虚函数getArea,用于计算二维形状的面积和三维形状的表面积。ThreeDimensionalShape还包含一个纯虚函数getVolume,用于计算三维形状的体积。程序还定义了四个具体类Circle、Square、Sphere和Cube,分别继承自TwoDimensionalShape和ThreeDimensionalShape,并实现了各自的getArea和getVolume函数。
在main函数中,首先创建一个Shape指针的vector对象shapes,并向其中添加四个具体类的对象。然后,通过一个循环遍历该vector对象中的所有元素,并依次调用每个元素的print函数,打印出该元素所指的对象信息。接着,程序使用dynamic_cast运算符判断每个元素到底是TwoDimensionalShape类的还是ThreeDimensionalShape类的,并分别输出其面积或表面积和体积。最后,程序删除所有动态分配的对象,释放内存。
阅读全文