计算几种不同的三维图形的表面积和体积。这里定义一个抽象类CShape,将其成员函数显示基本图形信息函数DispAttr()、计算表面积函数SArea()和计算体积函数Volume()定义为纯虚函数。根据此抽象类派生出球体、正方体、长方体和圆柱体,并分别重新实现基类中的纯虚函数来显示输出计算结果。 3、实验步骤: 1)声明形状类CShape为抽象列,并将其成员函数DispAttr()、SArea()、Volume()定义为纯虚函数; 2)声明四个派生类:球体类、正方体类、长方体类和圆柱体类,并重新实现基类中的纯虚函数; 3)在main函数中定义基类的指针数组,分别用来指向四个不同的派生类,并初始化各派生类的对象; 4)在main函数中计算所有三维图形的总的表面积和体积,并计算各三维图形占总表面积、总体积的百分比。
时间: 2024-03-12 17:50:16 浏览: 20
好的,我来详细解释一下实验步骤:
1. 声明形状类CShape为抽象类,并将其成员函数DispAttr()、SArea()、Volume()定义为纯虚函数。
```c++
class CShape {
public:
virtual void DispAttr() = 0; // 显示基本图形信息函数
virtual double SArea() = 0; // 计算表面积函数
virtual double Volume() = 0; // 计算体积函数
};
```
2. 声明四个派生类:球体类、正方体类、长方体类和圆柱体类,并重新实现基类中的纯虚函数。
```c++
class CCircle : public CShape {
private:
double r; // 半径
public:
CCircle(double r) : r(r) {}
void DispAttr() { cout << "球体半径:" << r << endl; }
double SArea() { return 4 * PI * r * r; }
double Volume() { return 4.0 / 3 * PI * r * r * r; }
};
class CRectangle : public CShape {
private:
double l, w, h; // 长、宽、高
public:
CRectangle(double l, double w, double h) : l(l), w(w), h(h) {}
void DispAttr() { cout << "长方体长:" << l << ",宽:" << w << ",高:" << h << endl; }
double SArea() { return 2 * (l * w + w * h + l * h); }
double Volume() { return l * w * h; }
};
class CSquare : public CRectangle {
public:
CSquare(double l) : CRectangle(l, l, l) {}
void DispAttr() { cout << "正方体边长:" << l << endl; }
};
class CCylinder : public CShape {
private:
double r, h; // 半径、高
public:
CCylinder(double r, double h) : r(r), h(h) {}
void DispAttr() { cout << "圆柱体半径:" << r << ",高:" << h << endl; }
double SArea() { return 2 * PI * r * h + 2 * PI * r * r; }
double Volume() { return PI * r * r * h; }
};
```
3. 在main函数中定义基类的指针数组,分别用来指向四个不同的派生类,并初始化各派生类的对象。
```c++
int main() {
const int n = 4; // 4种图形
CShape* shapes[n]; // 指针数组,指向各种图形
shapes[0] = new CCircle(2.0);
shapes[1] = new CSquare(3.0);
shapes[2] = new CRectangle(2.0, 3.0, 4.0);
shapes[3] = new CCylinder(2.0, 4.0);
// ...
}
```
4. 在main函数中计算所有三维图形的总的表面积和体积,并计算各三维图形占总表面积、总体积的百分比。
```c++
int main() {
const int n = 4; // 4种图形
CShape* shapes[n]; // 指针数组,指向各种图形
shapes[0] = new CCircle(2.0);
shapes[1] = new CSquare(3.0);
shapes[2] = new CRectangle(2.0, 3.0, 4.0);
shapes[3] = new CCylinder(2.0, 4.0);
double totalSurfaceArea = 0.0, totalVolume = 0.0;
for (int i = 0; i < n; i++) {
shapes[i]->DispAttr();
double surfaceArea = shapes[i]->SArea();
double volume = shapes[i]->Volume();
totalSurfaceArea += surfaceArea;
totalVolume += volume;
cout << "表面积:" << surfaceArea << endl;
cout << "体积:" << volume << endl << endl;
}
cout << "总表面积:" << totalSurfaceArea << endl;
cout << "总体积:" << totalVolume << endl;
for (int i = 0; i < n; i++) {
double surfaceArea = shapes[i]->SArea();
double volume = shapes[i]->Volume();
cout << "图形" << i + 1 << "占总表面积的百分比:" << surfaceArea / totalSurfaceArea * 100 << "%" << endl;
cout << "图形" << i + 1 << "占总体积的百分比:" << volume / totalVolume * 100 << "%" << endl;
}
for (int i = 0; i < n; i++) {
delete shapes[i];
}
return 0;
}
```
这样就完成了计算几种不同的三维图形的表面积和体积的实验。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)