Simple Class Template Array Program Example : 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.
Class Template Syntax
template <class identifier>
class template_class_name {
templete_identifier args;
public:
template_class_name(){
}
templete_identifier fn_name(templete_identifier .. args) {
return ...
}
};
Example Program
// Header Files
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
// Template Declaration
template<class T>
// Generic Template Class for Search
class TClassSearch {
T x[MAX_SIZE];
T element;
public:
TClassSearch()
{
}
void readElements()
{
int i = 0;
for (i = 0; i < MAX_SIZE; 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 < MAX_SIZE; i++) {
cout << "\t" << x[i];
}
/* for : Check elements one by one - Linear */
for (i = 0; i < MAX_SIZE; 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 == MAX_SIZE)
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 " << MAX_SIZE << " Elements for Searching Int : " << endl;
iMax.readElements();
iMax.getSearch();
cout << "\nEnter " << MAX_SIZE << " Elements for Searching float : " << endl;
fMax.readElements();
fMax.getSearch();
getch();
return 0;
}
Sample Output
Simple Class Template Array Program Example : Search Number
Enter 5 Elements for Searching Int :
56
78
10
34
67
Enter Element to Search : 67
Your Data : 56 78 10 34 67
Class Template Search : Element : 67 : Found : Position : 5.
Enter 5 Elements for Searching float :
90.89
89.008
67.45
100.1
90.41
Enter Element to Search : 90.89
Your Data : 90.89 89.008 67.45 100.1 90.41
Class Template Search : Element : 90.89 : Found : Position : 1.
Templates In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Stack Program in C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Do While Loop Example Program In C++
- Simple Program for Function Template Using C++ Programming