编写一个函数 surfaceArea(),专用于计算各种不同几何图形的面积,利用C++的函数重载技术,实现 以下至少三个版本: 若给定半径,则计算圆的面积 若给定宽和高,则计算矩形的面积 若给定三条边的长度,则计算三角形的面积
时间: 2024-09-25 17:04:17 浏览: 119
在C++中,你可以编写一个名为`surfaceArea()`的函数模板,结合函数重载(Function Overload),以便根据不同输入类型计算不同几何图形的面积。以下是三种情况下的函数实现:
1. **圆的面积** (假设输入的是半径 `radius`):
```cpp
double surfaceArea(double radius) {
return M_PI * pow(radius, 2); // 圆面积公式 πr²
}
```
2. **矩形的面积** (假设输入的是宽度 `width` 和高度 `height`):
```cpp
double surfaceArea(int width, int height) {
return width * height; // 矩形面积公式 长乘以宽
}
```
3. **三角形的面积** (假设输入的是两条边长 `a` 和 `b`,以及它们之间的夹角 `angle`,这里假设角度已转换成弧度,例如使用 `std::atan2` 函数获取):
```cpp
#include <cmath>
double surfaceArea(double a, double b, double angleInRadians) {
double s = (a + b + std::hypot(a, b)) / 2.0; // 半周长
return std::sqrt(s * (s - b) * (s - std::hypot(a, b))); // Heron's formula
}
```
相关问题
定义一个抽象基类(Shape),含有一个数据成员半径(-,并设为保护成员:定义返回 表面积(surfaceArea)和返回体积(volume)的成员函数,这些西数都是纯虚西数。定义基 类的派生 类球(Ba下)、圆柱(Cyinder),都含有返口表面积(surfacearea)和返回体积Cvolume) 的成员西数:圆桂(Cvinder)类还应增加一个私有数据成员高度 Cheight)。编写主文件, 进行测试,求球、圆柱的表面积和体积。使用基类指针,通过基类指针调用具体的派生类对 象中的面积和体积计算函数,实现面积和体积的计算,并输出结果。
以下是题目要求的抽象基类Shape及其派生类的实现:
```c++
#include <iostream>
using namespace std;
class Shape {
protected:
double radius;
public:
Shape(double r = 0) : radius(r) {}
virtual double surfaceArea() = 0;
virtual double volume() = 0;
};
class Ball : public Shape {
public:
Ball(double r) : Shape(r) {}
virtual double surfaceArea() {
return 4 * 3.14 * radius * radius;
}
virtual double volume() {
return 4.0 / 3.0 * 3.14 * radius * radius * radius;
}
};
class Cylinder : public Shape {
private:
double height;
public:
Cylinder(double r, double h) : Shape(r), height(h) {}
virtual double surfaceArea() {
return 2 * 3.14 * radius * radius + 2 * 3.14 * radius * height;
}
virtual double volume() {
return 3.14 * radius * radius * height;
}
};
int main() {
Shape* p;
Ball b(5);
Cylinder c(3, 10);
p = &b;
cout << "Ball surface area: " << p->surfaceArea() << endl;
cout << "Ball volume: " << p->volume() << endl;
p = &c;
cout << "Cylinder surface area: " << p->surfaceArea() << endl;
cout << "Cylinder volume: " << p->volume() << endl;
return 0;
}
```
输出结果为:
```
Ball surface area: 314
Ball volume: 523.333
Cylinder surface area: 207.36
Cylinder volume: 282.6
```
计算几种不同的三维图形的表面积和体积。这里定义一个抽象类CShape,将其成员函数显示基本图形信息函数DispAttr()、计算表面积函数SArea()和计算体积函数Volume()定义为纯虚函数。根据此抽象类派生出球体、正方体、长方体和圆柱体,并分别重新实现基类中的纯虚函数来显示输出计算结果。 3、实验步骤: 1)声明形状类CShape为抽象列,并将其成员函数DispAttr()、SArea()、Volume()定义为纯虚函数; 2)声明四个派生类:球体类、正方体类、长方体类和圆柱体类,并重新实现基类中的纯虚函数; 3)在main函数中定义基类的指针数组,分别用来指向四个不同的派生类,并初始化各派生类的对象; 4)在main函数中计算所有三维图形的总的表面积和体积,并计算各三维图形占总表面积、总体积的百分比。
好的,我来详细解释一下实验步骤:
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;
}
```
这样就完成了计算几种不同的三维图形的表面积和体积的实验。
阅读全文
相关推荐
















