// --------------------------------------------------------------------------- // MySums // Motivation Assignment for why Templates may be useful // --------------------------------------------------------------------------- #include using namespace std; // --------------------------------------------------------------------------- // Templated Sum Function // Sums a list of numbers of specified length // --------------------------------------------------------------------------- template T SumThings(T* thingList, int size) { T sum = 0; for (int i=0; i < size; i++) { sum += thingList[i]; } return sum; } // --------------------------------------------------------------------------- // main function - it all begins here // --------------------------------------------------------------------------- int main() { int myInts[5] = { 4, 45, 65, 34, 12 }; float myFloats[5] = { 3.4, 0.5, 6.9, 43.2, -14.7 }; double myDoubles[5] = { 3.4, 10.5, 6.9, 43.2, -14.7 }; cout << SumThings(myInts, 5) << " " << SumThings(myFloats, 5) << " " << SumThings(myDoubles, 5) << endl; return 0; } // --------------------------------------------------------------------------- // file ends