📄 prog9_01b.cpp
字号:
// Program 9.1b Overloading a function - removing ambiguity with a cast
#include <iostream>
using std::cout;
using std::endl;
// Prototypes for two different functions
double larger(double a, double b);
long larger(long a, long b);
int main() {
double a_double = 1.5, b_double = 2.5;
float a_float = 3.5f, b_float = 4.5f;
long a_long = 15L, b_long = 25L;
int a_int = 15, b_int = 25;
cout << "The larger of int values "
<< a_int << " and " << b_int <<" is "
<< larger(static_cast<long>(a_int), static_cast<long>(b_int))
<< endl;
cout << endl;
cout << "The larger of double values "
<< a_double << " and " << b_double <<" is "
<< larger(a_double, b_double) << endl;
cout << "The larger of float values "
<< a_float << " and " << b_float <<" is "
<< larger(a_float, b_float) << endl;
cout << "The larger of long values "
<< a_long << " and " << b_long <<" is "
<< larger(a_long, b_long) << endl;
return 0;
}
// Function to return the larger of two floating point values
double larger(double a, double b) {
cout << "double larger() called" << endl;
return a>b ? a : b;
}
// Function to return the larger of two integer values
long larger(long a, long b) {
cout << "long larger() called" << endl;
return a>b ? a : b;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -