C++ Programming Code Examples C++ > Arrays and Matrices Code Examples Simple Class Template Array Program: Search Number Simple Class Template Array Program: Search Number Generic Programming Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types Class Template Definition A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.The C++ Standard Library contains many class templates, in particular, the containers adapted from the Standard Template Library, such as vector. // Header Files #include <iostream> #include<conio.h> #include<stdlib.h> #define maxsize 5 using namespace std; // Template Declaration template<class T> // Generic Template Class for Search class TClassSearch { T x[maxsize]; T element; public: TClassSearch() { } void readElements() { int i = 0; for (i = 0; i < maxsize; i++) cin >> x[i]; //x*=t_x; element=t_element; cout << "\nEnter Element to Search : "; cin>>element; } T getSearch() { int i; cout << "\nYour Data :"; for (i = 0; i < maxsize; i++) { cout << "\t" << x[i]; } /* for : Check elements one by one - Linear */ for (i = 0; i < maxsize; i++) { /* If for Check element found or not */ if (x[i] == element) { cout << "\Class Template Search : Element : " << element << " : Found : Position : " << i + 1 << ".\n"; break; } } if (i == maxsize) cout << "\nSearch Element : " << element << " : Not Found \n"; } }; int main() { TClassSearch <int> iMax = TClassSearch<int>(); TClassSearch <float> fMax = TClassSearch<float>(); cout << "Simple Class Template Array Program Example : Search Number\n"; cout << "\nEnter " << maxsize << " Elements for Searching Int : " << endl; iMax.readElements(); iMax.getSearch(); cout << "\nEnter " << maxsize << " Elements for Searching float : " << endl; fMax.readElements(); fMax.getSearch(); getch(); return 0; }