用C++定义一个描述平面图形的基类 Plane 定义一个描述柱体的基类 Body 从虚基类 Plane 派生出具体类(如长方形类 Rectangle、圆形类 Circle 和三角形类triangle,由Rectangle派生出正方形Square类),根据实际情况,覆盖基类 Plane 的求面积函数 area() 和Body的求体积函数volume()。从具体triangle类、square及Circle和Body类派生出Triangularprism(三棱柱), quadrangular(四棱柱), circularcolumn(圆柱)类且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-03-27 08:41:11 浏览: 79
好的,以下是代码:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
class Plane {
public:
virtual double area() = 0;
};
class Rectangle : virtual public Plane {
public:
Rectangle(double w, double h) : width(w), height(h) {}
virtual double area() {
return width * height;
}
protected:
double width;
double height;
};
class Circle : virtual public Plane {
public:
Circle(double r) : radius(r) {}
virtual double area() {
return M_PI * radius * radius;
}
protected:
double radius;
};
class Triangle : virtual public Plane {
public:
Triangle(double a, double b, double c) : side1(a), side2(b), side3(c) {}
virtual double area() {
double s = (side1 + side2 + side3) / 2;
return sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
protected:
double side1;
double side2;
double side3;
};
class Square : public Rectangle {
public:
Square(double l) : Rectangle(l, l) {}
};
class Body {
public:
virtual double volume() = 0;
virtual double superficialarea() = 0;
};
class Circularcolumn : public Circle, public Body {
public:
Circularcolumn(int n, double h, double r) : Circle(r), height(h), shape(n) {}
virtual double volume() {
return Circle::area() * height;
}
virtual double superficialarea() {
return 2 * Circle::area() + shape * Circle::radius * height;
}
protected:
double height;
int shape;
};
class Triangularprism : public Triangle, public Body {
public:
Triangularprism(int n, double h, double a, double b, double c) : Triangle(a, b, c), height(h), shape(n) {}
virtual double volume() {
return Triangle::area() * height;
}
virtual double superficialarea() {
double l = sqrt(height * height + (side3 - side1) * (side3 - side1) / 4);
return Triangle::area() * 2 + (side1 + side2 + side3) * l + shape * height * side3;
}
protected:
double height;
int shape;
};
class Quadrangular : public Rectangle, public Body {
public:
Quadrangular(int n, double h, double l) : Rectangle(l, l), height(h), shape(n) {}
virtual double volume() {
return Rectangle::area() * height;
}
virtual double superficialarea() {
return Rectangle::area() * 2 + shape * height * (width + height) * 2;
}
protected:
double height;
int shape;
};
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 superficialsum = 0;
double volumesum = 0;
for (int i = 0; i < 3; i++) {
volumesum += body[i]->volume();
superficialsum += body[i]->superficialarea();
}
cout << "all volume=" << volumesum << endl;
cout << "all superficialarea=" << superficialsum << endl;
return 0;
}
```
这段代码定义了一个平面图形基类 `Plane`,派生出三个具体类 `Rectangle`、`Circle` 和 `Triangle`,其中 `Rectangle` 和 `Circle` 分别代表长方形和圆形,`Triangle` 代表三角形。这三个类都重载了基类的求面积函数。
然后定义了一个柱体基类 `Body`,派生出三个具体类 `Circularcolumn`、`Triangularprism` 和 `Quadrangular`,分别代表圆柱体、三棱柱和正四棱柱。这三个类都重载了基类的求体积函数和表面积函数。
在 `main` 函数中,首先按照题目要求输入三个图形的参数,然后将它们的指针存储在一个 `Body` 类型的数组中。最后循环遍历数组,求出总体积和总表面积,并输出。
阅读全文