C++语言编写面向对象程序,实现柱体体积和表面积的计算(等柱体,比如三棱柱,四棱柱,五棱柱等,截面积相同的情况下,体积=底面积*高)。 例如底面半径为 2、高为 4 的圆柱,体积为 50.27,表面积为75.40;以长为 3、宽为 2 的长方形为底面,高为 5 的四棱柱,体积为 30,表面积为 62。 注意: 定义一个描述平面图形的基类 Plane 定义一个描述柱体的基类 Body 从虚基类 Plane 派生出具体类(如长方形类 Rectangle、圆形类 Circle 和三角形类triangle,由Rectangle派生出正方形Square类),根据实际情况,覆盖基类 Plane 的求面积函数 area() 和Body的求体积函数volume()。 4、从具体triangle类、square及Circle和Body类派生出Triangularprism(三棱柱), quadrangular(四棱柱), circularcolumn(圆柱)类 5、已知一组棱柱体,由不同的柱体组成,求该组柱体的体积之和和表面积之和
时间: 2024-02-12 07:03:48 浏览: 168
以下是实现题目要求的C++代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
// 平面图形基类
class Plane {
public:
virtual double area() = 0; // 求面积的纯虚函数
};
// 长方形类,继承自平面图形基类
class Rectangle : public virtual Plane {
protected:
double length;
double width;
public:
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() {
return length * width;
}
};
// 正方形类,继承自长方形类
class Square : public Rectangle {
public:
Square(double l) : Rectangle(l, l) {}
double area() {
return length * length;
}
};
// 圆形类,继承自平面图形基类
class Circle : public virtual Plane {
protected:
double radius;
public:
Circle(double r) {
radius = r;
}
double area() {
return M_PI * radius * radius;
}
};
// 三角形类,继承自平面图形基类
class Triangle : public virtual Plane {
protected:
double base;
double height;
public:
Triangle(double b, double h) {
base = b;
height = h;
}
double area() {
return 0.5 * base * height;
}
};
// 柱体基类
class Body {
public:
virtual double volume() = 0; // 求体积的纯虚函数
virtual double surfaceArea() = 0; // 求表面积的纯虚函数
};
// 三棱柱类,继承自长方形类和三角形类和柱体基类
class TriangularPrism : public Rectangle, public Triangle, public Body {
protected:
double height;
public:
TriangularPrism(double l, double w, double h) : Rectangle(l, w), Triangle(l, h) {
height = h;
}
double volume() {
return area() * height;
}
double surfaceArea() {
double s1 = width * height;
double s2 = length * height;
double s3 = base * length;
double s4 = base * width;
double s5 = base * height;
return 2 * (s1 + s2 + s3 + s4 + s5);
}
};
// 四棱柱类,继承自长方形类和柱体基类
class QuadrangularPrism : public Rectangle, public Body {
protected:
double height;
public:
QuadrangularPrism(double l, double w, double h) : Rectangle(l, w) {
height = h;
}
double volume() {
return area() * height;
}
double surfaceArea() {
return 2 * (length * width + length * height + width * height);
}
};
// 圆柱类,继承自圆形类和柱体基类
class CircularColumn : public Circle, public Body {
protected:
double height;
public:
CircularColumn(double r, double h) : Circle(r) {
height = h;
}
double volume() {
return area() * height;
}
double surfaceArea() {
return 2 * M_PI * radius * height + 2 * area();
}
};
int main() {
// 创建三个不同的柱体
TriangularPrism tprism(3, 2, 5);
QuadrangularPrism qprism(2, 3, 5);
CircularColumn ccolumn(2, 4);
// 输出每个柱体的体积和表面积
cout << "Triangular Prism Volume: " << tprism.volume() << endl;
cout << "Triangular Prism Surface Area: " << tprism.surfaceArea() << endl;
cout << "Quadrangular Prism Volume: " << qprism.volume() << endl;
cout << "Quadrangular Prism Surface Area: " << qprism.surfaceArea() << endl;
cout << "Circular Column Volume: " << ccolumn.volume() << endl;
cout << "Circular Column Surface Area: " << ccolumn.surfaceArea() << endl;
// 计算三个柱体的体积和表面积之和
double totalVolume = tprism.volume() + qprism.volume() + ccolumn.volume();
double totalSurfaceArea = tprism.surfaceArea() + qprism.surfaceArea() + ccolumn.surfaceArea();
// 输出计算结果
cout << "Total Volume: " << totalVolume << endl;
cout << "Total Surface Area: " << totalSurfaceArea << endl;
return 0;
}
```
注:以上代码仅供参考,实际实现中可能需要根据具体情况进行修改。
阅读全文