使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。
时间: 2023-05-10 11:55:09 浏览: 126
程序结构和函数--函数举例-C程序语言复习
可以使用函数重载的方法定义两个重名函数,分别为:
int sum(int a, int b) {
return a + b;
}
float sum(float a, float b) {
return a + b;
}
在主函数中调用这两个函数:
int main() {
int a = 1, b = 2;
float c = 1.5, d = 2.5;
cout << "The sum of " << a << " and " << b << " is " << sum(a, b) << endl;
cout << "The sum of " << c << " and " << d << " is " << sum(c, d) << endl;
return 0;
}
输出结果为:
The sum of 1 and 2 is 3
The sum of 1.5 and 2.5 is 4
阅读全文