c++利用函数重载,计算三个int的平方和及三个double的平方和
时间: 2024-05-29 14:09:04 浏览: 11
好的,这是一个计算平方和的问题,我可以直接回答。对于三个int的平方和,可以写出如下函数重载:
int calcSquareSum(int a, int b, int c) {
return a * a + b * b + c * c;
}
而对于三个double的平方和,可以写出如下函数重载:
double calcSquareSum(double a, double b, double c) {
return a * a + b * b + c * c;
}
通过这两个函数,我们可以分别计算三个int和三个double的平方和。
相关问题
c++重载函数平方数计算
你好,下面是一个C++的重载函数平方数计算的示例代码:
```c++
#include <iostream>
using namespace std;
// 重载函数,计算整数的平方
int square(int num) {
return num * num;
}
// 重载函数,计算浮点数的平方
double square(double num) {
return num * num;
}
int main() {
int intNum = 5;
double doubleNum = 2.5;
cout << "整数 " << intNum << " 的平方为:" << square(intNum) << endl;
cout << "浮点数 " << doubleNum << " 的平方为:" << square(doubleNum) << endl;
return 0;
}
```
输出结果为:
```
整数 5 的平方为:25
浮点数 2.5 的平方为:6.25
```
在上面的示例中,我们定义了两个重载函数 `square`,分别接收一个整数和一个浮点数,计算它们的平方并返回结果。在主函数中,我们分别传入一个整数和一个浮点数,并分别调用对应的 `square` 函数。最后输出结果。
编写两个名为sum of square的重载函数分别求两整数的平方和和两实数的平方
和。
可以参考以下代码:
```c++
#include <iostream>
using namespace std;
int sum_of_square(int a, int b) {
return a * a + b * b;
}
double sum_of_square(double a, double b) {
return a * a + b * b;
}
int main() {
int x = 3, y = 4;
double p = 1.5, q = 2.5;
cout << "sum of square of " << x << " and " << y << " is " << sum_of_square(x, y) << endl;
cout << "sum of square of " << p << " and " << q << " is " << sum_of_square(p, q) << endl;
return 0;
}
```
输出结果为:
```
sum of square of 3 and 4 is 25
sum of square of 1.5 and 2.5 is 8.5
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)