Section 12.2 Check Point Questions7 questions
12.2.1
For the maxValue function in Listing 12.1, can you invoke it with two arguments of different types,
such as maxValue(1, 1.5)?
12.2.2
For the maxValue function in Listing 12.1,
can you invoke it with two arguments of strings,
such as maxValue("ABC", "ABD")? Can you invoke
it with two arguments of circles,
such as maxValue(Circle(2), Circle(3))?
12.2.3
Can template<typename T> be replaced by template<class T>?
12.2.4
Can a type parameter be named using any identifier other than a keyword?
12.2.5
Can a type parameter be of a primitive type or an object type?
12.2.6
What is wrong in the following code?
#include <iostream> #include <string> using namespace std; template<typename T> T maxValue(T value1, T value2) { int result; if (value1 > value2) result = value1; else result = value2; return result; } int main() { cout << "Maximum between 1 and 3 is " << maxValue(1, 3) << endl; cout << "Maximum between 1.5 and 0.3 is " << maxValue(1.5, 0.3) << endl; cout << "Maximum between 'A' and 'N' is " << maxValue('A', 'N') << endl; cout << "Maximum between \"ABC\" and \"ABD\" is " << maxValue("ABC", "ABD") << endl; return 0; }
12.2.7
Suppose you define the maxValue function as follows:
template<typename T1, typename T2> T1 maxValue(T1 value1, T2 value2) { if (value1 > value2) return value1; else return value2; }What would be the return value from invoking maxValue(1, 2.5), maxValue(1.4, 2.5), and maxValue(1.5, 2)?