/* Author: Arun Somasundaram, CSE Dept, OSU. Simple Program to test templates.... 1. Function Templates / Specialization 2. Class Templates 3. typedef usage */ #include using namespace std; //----------------------------------------------------// //Template with one parameter template void print(SomeType val){ cout << "Value is " << val << endl; } //Template with two parameters template void printTwo(T1 x, T2 y){ cout << "x, y : " << x << ", " << y << endl; } //------------------------------------------------------------// template bool isLess(T a, T b) { return (a < b); } //Template Function Specialization for comparing char * //When the function arguments are const char * then this is called template <> bool isLess(const char *a, const char *b) { return (strcmp(a, b) < 0); } //-----------------------------------------------------------// //Template for a simple List class template class List { private: int size; T elem[maxSize]; public: List(){size = 0;} List(const List&); ~List(){} void addElement(T val); void deleteLastElement(); int getMaxSize(){return maxSize;} int getSize(){return size;} T getElement(int index) { //Assuming index is within bounds. return elem[index]; } }; template void List::addElement(T val){ if (size == maxSize) { cout << "Max size " << maxSize << " reached." << endl; } else{ elem[size] = val; size++; } } template void List::deleteLastElement(){ if (size == 0) cout << "No elements to delete" << endl; else size--; } //-------------------------------------------------------------// //typedefs are useful for shortening the long names of classes generated from templates typedef List List256Float; //------------------------------------------------------------// int main(){ int i = 1; char c = 'a'; float f = 2.5; cout << "Print function template usage." << endl; cout << "-----------------------------" << endl; print(i); //Template argument deduced from function argument print(i); //Template argument int explicitly specified cout << endl; print(c); print(c); cout << endl; print(f); print(f); cout << endl; cout << "PrintTwo function template usage." << endl; cout << "--------------------------------" << endl; printTwo(i, c); printTwo(i, c); cout << endl; cout << "Simple List Class Template usage." << endl; cout << "---------------------------------" << endl; //List of integers List lI; //Argument list explicitly specified. //Class Template Parameters are not deduced. //Integer template argument has to be a // constant (Here 2 for example). lI.addElement(100); lI.addElement(101); cout << lI.getElement(0) << ", " << lI.getElement(1) << endl;; //List of characters List lC; lC.addElement('a'); lC.addElement('b'); lC.addElement('c'); cout << lC.getElement(0) << ", " << lC.getElement(1) << ", " << lC.getElement(2) << endl; //List of floats List256Float l256f; //was defined using typedef l256f.addElement(1.5); l256f.addElement(2.5); cout << "MaxSize " << l256f.getMaxSize() << endl; cout << l256f.getElement(0) << ", " << l256f.getElement(1) << endl << endl; cout << "Template Function Specialization for isLess" << endl; cout << "-------------------------------------------" << endl; float f1 = 2.0, f2 = 3.0; bool lessBool = isLess(f1, f2); cout << f1 << " < " << f2 << " : " << lessBool << " (0 - False, 1 - True)" << endl; const char *s2 = "Banana", *s1 = "Apples"; lessBool = isLess(s1, s2); //specialization used cout << s1 << " < " << s2 << " : " << lessBool << " (0 - False, 1 - True)" << endl; return 0; }