c++定义一个描述平面图形的基类 Plane 定义一个描述柱体的基类 Body 从虚基类 Plane 派生出具体类(如长方形类 Rectangle、圆形类 Circle 和三角形类triangle,由Rectangle派生出正方形Square类),根据实际情况,覆盖基类 Plane 的求面积函数 area() 和Body的求体积函数volume()。 4、从具体triangle类、square及Circle和Body类派生出Triangularprism(三棱柱), quadrangular(四棱柱), circularcolumn(圆柱)类 5、已知一组棱柱体,由不同的柱体组成,求该组柱体的体积之和和表面积之和 并补充下列代码 #include <iostream> using namespace std; #include<string> #include"time.h" #include"math.h" #define PI 3.14 int main() { int n; double height,r,t1,t2,t3,l; cin>>n>>height>>r;//输入n=0,表示圆柱体 Circularcolumn c1(n,height,r); cin>>n>>height>>t1>>t2>>t3;//输入n=3,表示三棱柱 Triangularprism t(n,height,t1,t2,t3); cin>>n>>height>>l;//输入n=4表示正四棱柱 Quadrangular qu(n,height,l); Body *body[3]; body[0]=&c1; body[1]=&t; body[2]=&qu; double superficalsum=0; double volumesum=0; for(int i=0;i<3;i++) { volumesum+=body[i]->volume();//volume()获取该体的体积 superficalsum+=body[i]->superficialarea();//获取该体的表面积 } cout<<"all volume="<<volumesum<<endl; cout<<"all superfilarea="<<superficalsum<<endl; }
时间: 2024-01-26 14:04:43 浏览: 123
这是一道较长的题目,不过按照题目要求,我们需要定义 Plane 和 Body 两个基类,然后派生出若干个具体类,最终计算一组由不同柱体组成的体积之和和表面积之和。下面是代码实现:
```
#include <iostream>
#include <string>
#include <cmath>
#define PI 3.14
using namespace std;
class Plane {
public:
virtual double area() = 0; // 求面积函数
};
class Body {
public:
virtual double volume() = 0; // 求体积函数
virtual double superficialarea() = 0; // 求表面积函数
};
class Rectangle : virtual public Plane { // 长方形类
protected:
double length, width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double area() {
return length * width;
}
};
class Circle : virtual public Plane { // 圆形类
protected:
double radius;
public:
Circle(double r) : radius(r) {}
double area() {
return PI * radius * radius;
}
};
class Triangle : virtual public Plane { // 三角形类
protected:
double a, b, c; // 三边长
public:
Triangle(double aa, double bb, double cc) : a(aa), b(bb), c(cc) {}
double area() {
double p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
};
class Square : public Rectangle { // 正方形类
public:
Square(double l) : Rectangle(l, l) {}
};
class Triangularprism : public Triangle, public Body { // 三棱柱类
protected:
double height;
public:
Triangularprism(int n, double h, double a, double b, double c) : Triangle(a, b, c), height(h) {}
double volume() {
return Triangle::area() * height;
}
double superficialarea() {
return Triangle::area() * 2 + a * height + b * height + c * height;
}
};
class Quadrangular : public Rectangle, public Body { // 正四棱柱类
protected:
double height;
public:
Quadrangular(int n, double h, double l) : Rectangle(l, l), height(h) {}
double volume() {
return Rectangle::area() * height;
}
double superficialarea() {
return Rectangle::area() * 2 + length * height * 2 + width * height * 2;
}
};
class Circularcolumn : public Circle, public Body { // 圆柱类
protected:
double height;
public:
Circularcolumn(int n, double h, double r) : Circle(r), height(h) {}
double volume() {
return Circle::area() * height;
}
double superficialarea() {
return Circle::area() * 2 + 2 * PI * Circle::radius * height;
}
};
int main() {
int n;
double height, r, t1, t2, t3, l;
cin >> n >> height >> r; // 输入圆柱体参数
Circularcolumn c1(n, height, r);
cin >> n >> height >> t1 >> t2 >> t3; // 输入三棱柱参数
Triangularprism t(n, height, t1, t2, t3);
cin >> n >> height >> l; // 输入正四棱柱参数
Quadrangular qu(n, height, l);
Body *body[3];
body[0] = &c1;
body[1] = &t;
body[2] = &qu;
double superficalsum = 0, volumesum = 0;
for (int i = 0; i < 3; i++) {
volumesum += body[i]->volume(); // 计算体积之和
superficalsum += body[i]->superficialarea(); // 计算表面积之和
}
cout << "all volume=" << volumesum << endl;
cout << "all superficalarea=" << superficalsum << endl;
return 0;
}
```
最后的输出结果就是该组柱体的体积之和和表面积之和。
阅读全文