下列语句中,将两数intsum(intx,inty)正确重载的是()。 A. float sum(int x, int y); B. int sum(int a, int b); C. float sum(float x, float y); D. double sum(int y, int x);
时间: 2023-11-22 13:18:15 浏览: 59
正确的选项是B. int sum(int a, int b);。函数的重载要求函数名相同,但参数类型、参数个数或参数顺序不同。由于intsum(intx,inty)已经定义,所以只需要保证参数类型、参数个数或参数顺序不同即可。选项B中的int sum(int a, int b)与intsum(intx,inty)函数名相同,参数类型、参数个数都不同,因此是正确的重载。而选项A中的float sum(int x, int y)参数类型不同,但返回值类型不同,不符合重载的条件;选项C中的float sum(float x, float y)参数类型不同,不符合重载的条件;选项D中的double sum(int y, int x)虽然参数类型不同,但参数顺序相同,也不符合重载的条件。
相关问题
. 下面类中两函数是重载函数么?如果是,如何决定调用哪个函数? class A { public: char fun(intx, inty) ; char fun(intx, inty) const; };
是重载函数,它们的函数名和参数列表都相同,但是一个是 const 成员函数,一个是非 const 成员函数。在调用时,如果对象是 const 对象,则会调用 const 成员函数;如果对象是非 const 对象,则会调用非 const 成员函数。例如:
```
A a;
const A b;
a.fun(1, 2); // 调用非 const 成员函数
b.fun(1, 2); // 调用 const 成员函数
```
给出答案并每个选项分析,以下正确的函数原型语句是( )。 A doublefun(int x,int y) B doublefun(int x;inty) C double fun(int,int) ; D doublefun(intx,y) ;
正确的函数原型语句是 C:`double fun(int, int);`
分析如下:
A 选项中,参数列表的两个参数之间缺少了逗号,应该为 `double fun(int x, int y)`。
B 选项中,参数列表的两个参数之间使用了中文分号,应该使用英文逗号,正确的写法为 `double fun(int x, int y)`。
C 选项中,所有语法都是正确的,表示一个返回类型为 double 的函数,函数名为 fun,参数列表包含两个整型变量 x 和 y。
D 选项中,参数列表中的两个参数之间缺少了逗号,并且函数名与返回类型之间也缺少了空格,应该为 `double fun(int x, int y);`
相关推荐
![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_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)
![](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)