********************************************** * Computer Science 228 Spring 1997 * * TOPICS: Templates * * PROBLEMS: Listed below by chapter groups * ********************************************** Practice Problems (Dietel Ch 12) -------------------------------- 12.1 Answer in text. 12.8 Any number of solutions... 12.16 This statement declares an Array object to store Employee objects and passes 100 to the constructor function (probably as the size of the Array) 12.17 This statement also declares an Array object to store Employee objects. However, it uses the default constructor for the Array class. 12.18 This statement is the heading of a constructor for the Array class that takes an int as a parameter. It's easier to see this if written like: template Array::Array( int s ) Practice Problems (Weiss Ch 3) ----------------------------- 3.5 //Template Min routine //Etype: Must have the operator< owerloaded for it template Etype Min(Etype Lhs, Etype Rhs) { if (Lhs < Rhs) return Lhs; return Rhs; } //Template Max routine //Etype: Must have the operator> owerloaded for it template Etype Max(Etype Lhs, Etype Rhs) { if (Lhs > Rhs) return Lhs; return Rhs; } // Weiss 3.6 #include template Etype Min(Etype a[], int size) { Etype min; int i; min = a[0]; for(i = 1; i < size; i++) if( min > a[i]) min = a[i]; return min; } template Etype Max(Etype a[], int size) { Etype max; int i; max = a[0]; for(i = 1; i < size; i++) if( max < a[i]) max = a[i]; return max; } main() { int a[10] = {6, 200, -8, 75, 20000, -67523, 0, 77, -1, 888}; float b[10] = {6.0, 200.1, -8.2, 75.3, 20000.4, -67523.5, 0.6, 77.7, -1.8, 888.9}; cout << "Min in integer array: " << Min(a, 10) << endl; cout << "Max in integer array: " << Max(a, 10) << endl; cout << "Min in float array: " << Min(b, 10) << endl; cout << "Max in float array: " << Max(b, 10) << endl; } /* Output from main(): Min in integer array: -67523 Max in integer array: 20000 Min in float array: -67523.5 Max in float array: 20000.4 */