From a chapter on function overloading.
Exercise 9.2.1. Write two versions of a generic get_number() function so that
get_number can be used to get either an integer or floating-point number, as
desired. As with the get_int and get_dbl examples in this book, the function
should take a numeric argument that specifies the default value. Use the argu-
ment to determine what kind of data to return.
For example, given this call
get_number(0)
the function should return an integer value, whereas this call
get_number(0.0)
should return a value of type double. Remember that C++ notation recognizes
any constant expression with a decimal point as a floating-point expression with
double type.
1 2 3 4 5 6 7 8 9
|
int get_number(int n = 0) {
cin >> n;
return n;
}
float get_number(float n = 0.0) {
cin >> n;
return n;
}
|