定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle( 长方形)、Trapezoid (梯形)和Triangle (三角形),用虚函数分别计算各种图形的面积,输出它们的面积和。要求用基类指针数组,每一个数组元素指向一个派生类的对象。PI=3.14159f,单精度浮点数计算。用C++
时间: 2023-07-16 10:16:55 浏览: 71
以下是实现代码:
```cpp
#include <iostream>
using namespace std;
const float PI = 3.14159f;
class Shape {
public:
virtual float area() = 0; // 纯虚函数,用于计算面积
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) : radius(r) {}
virtual float area() {
return PI * radius * radius;
}
};
class Square : public Shape {
private:
float side;
public:
Square(float s) : side(s) {}
virtual float area() {
return side * side;
}
};
class Rectangle : public Shape {
private:
float length, width;
public:
Rectangle(float l, float w) : length(l), width(w) {}
virtual float area() {
return length * width;
}
};
class Trapezoid : public Shape {
private:
float top, bottom, height;
public:
Trapezoid(float t, float b, float h) : top(t), bottom(b), height(h) {}
virtual float area() {
return (top + bottom) * height / 2.0f;
}
};
class Triangle : public Shape {
private:
float base, height;
public:
Triangle(float b, float h) : base(b), height(h) {}
virtual float area() {
return base * height / 2.0f;
}
};
int main() {
Shape* shapes[5]; // 基类指针数组
shapes[0] = new Circle(2.0f);
shapes[1] = new Square(3.0f);
shapes[2] = new Rectangle(2.0f, 4.0f);
shapes[3] = new Trapezoid(2.0f, 4.0f, 3.0f);
shapes[4] = new Triangle(3.0f, 4.0f);
float totalArea = 0.0f;
for (int i = 0; i < 5; i++) {
float area = shapes[i]->area();
totalArea += area;
cout << "The area of shape " << i+1 << " is: " << area << endl;
}
cout << "The total area of all shapes is: " << totalArea << endl;
for (int i = 0; i < 5; i++) {
delete shapes[i]; // 释放内存
}
return 0;
}
```
运行结果如下:
```
The area of shape 1 is: 12.5664
The area of shape 2 is: 9
The area of shape 3 is: 8
The area of shape 4 is: 9
The area of shape 5 is: 6
The total area of all shapes is: 44.5664
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)