4、编写用来分别完成求三角形、圆形和矩形三种不同图形的周长的函数,要求求周长的函数使用重载函数来完成, 然后由主函数分别进行调用。
时间: 2023-05-22 13:04:58 浏览: 146
非常感谢您的问题。下面是代码示例:
```c++
#include<iostream>
using namespace std;
double calculatePerimeter(double side1, double side2, double side3) {
return side1 + side2 + side3;
}
double calculatePerimeter(double radius) {
return 2 * 3.14 * radius;
}
double calculatePerimeter(double length, double width) {
return 2 * (length + width);
}
int main() {
double s1 = 3, s2 = 4, s3 = 5;
double radius = 5;
double length = 6, width = 8;
cout << "Triangle perimeter: " << calculatePerimeter(s1, s2, s3) << endl;
cout << "Circle perimeter: " << calculatePerimeter(radius) << endl;
cout << "Rectangle perimeter: " << calculatePerimeter(length, width) << endl;
return 0;
}
```
如果您有更多问题,我很乐意为您解答。
阅读全文