Explain function templates with an example.
Function
Templates
•
To perform identical operations for each type of data compactly and
conveniently, the function templates are used. One can write a single function
template definition. Based on the argument types provided in calls to the
function, the compiler automatically instantiates separate object code
functions to handle each type of call appropriately. Thus the same functional
code with different data type elements can be handled. For instance using add
function one can perform addition of two integer values or two double values or
two float values.
•
The syntax of function template is as follows ‒
template
<class name_of_data_type>
name_of_data_type
function_name (name_of_data_type id1,... name_of_data_type id2)
template<class
T>
T
min(T a, T b)
• Here template
is a keyword used to represent the template. Then inside the angular
bracket keyword class is followed by the data type name T. The compiler will
replace T by the appropriate data types.
•
Function templates are implemented like regular functions, except they are
prefixed with the keyword template. Here is a sample with a function template.
#include <iostream>
using namespace std;
//min returns the minimum of the two elements
template<class T>
T min(T a, T b)
{
if (a<b)
return a;
else
return b;
}
int main()
{
cout <<
"min(10, 20) = " <<
min(10, 20) << endl;
cout <<
"min('p', 't') =" <<
min('p', 't') << endl;
cout <<
"min(10.3, 67.2) = " <<min(10.3,67.2)<<endl;
return 0;
}
Output
min(10, 20) = 10
min('p', 't') = p
min(10.3, 67.2) = 10.3
•
Use of function templates is very easy: We can use them like regular functions.
When the compiler sees an instantiation of the function template, for example:
the call min(10, 20) in function main, the compiler generates a function
min(int, int). Similarly the compiler generates definitions for min(char, char)
and min(float, float) on encountering the corresponding values.
Example :1
What is function template? Write a template
function to sort arrays of float and int using bubble sort.
Solution:
Function template:
Function template defines the general set of operations that will be applied on
various types of data. This data can be passed to this function as a argument.
/**************************************************************************
Program to sort the arrays of float and int using bubble sort
with the help of function template
***************************************************************************/
#include<iostream>
#define size 10
using namespace std;
template<class T>
void bubble(T A[size],int n)
{
T temp;
for(int i=0;i<=n‒2;i++)
{
for(int j=0;j<=n‒2‒i;j++)
{
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
cout<<"\n The sorted List is ...\n";
for(i=0;i<n;i++)
out
cout<<" "<<A[i];
}
void main()
{
int A1[size];
float A2[size];
cout<<"\n\t\t Bubble Sort\n";
int i,n;
cout<<"\n\t‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒";
cout<<"\n\t Handling Integer Elements";
cout<<"\n\t‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒";
cout<<"\n How many elements are there?";
cin>>n;
cout<<"\n Enter the Integer elements\n";
for(i=0;i<n;i++)
cin>>A1[i];
bubble(A1,n);
cout<<"\n";
cout<<"\n\t‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒";
cout<<"\n\t Handling Float Elements";
cout<<"\n\t‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒";
cout<<"\n How many elements are there?";
cin>>n;
cout<<"\n Enter the float elements\n";
for(i=0;i<n;i++)
cin>> A2[i];
bubble(A2,n);
Output
Bubble Sort
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
Handling Integer
Elements
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
How many elements are
there?5
Enter the Integer
elements
33 22 55 44 11
The sorted List is ...
11 22 33 44 55
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
Handling Float
Elements
‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒‒
How many elements are
there?5
Enter the float
elements
44.4 22.2 11.1 55.5
33.3
The sorted List is
11.1 22.2 33.3 44.4
55.5
Example :
2
Write C++
program to sort the list of integers, floating point numbers and characters by
Quick sort mechanism using function template.
Solution :
#include<iostream>
using namespace std;
#define SIZE 5
template <class T>
void quicksort(T [], int, int);
/*
This function is to sort the elements in a sublist
*/
template<class T>
void quicksort(T arr[], int p, int q)
{
int j;
if (p<q)
{
j =
partition(arr,p, q + 1);//setting pivot element
quicksort(arr,p,
j‒1);//splitting of list
quicksort(arr,j
+ 1, q);//splitting of list
}
}
*/
This function is to partition a list and decide the pivot
element
*/
template<class T>
T partition(T arr[],int m, int p)
{
T pivot = arr[m];
int i=m, j = p;
do
{
do
{
i++;
} while (arr[i]<pivot);
do
{
j‒‒;
} while (arr[j]>pivot);
if (i<j)
swap(i, j);
} while (i<j);
arr[m] = arr[j];
arr[j] = pivot;
return j;
}
template<class T>
void swap(int i, int i)
{
T p;
p = arr[i];
arr[i] = arr[j];
arr[j] = p;
}
void main()
{
cout << "\n\t\t Quick Sort Method \n";
int arr[] = { 5,2,3,1,4 };
quicksort(arr,0, SIZE‒1);
cout << "\n The sorted list of integers is\n";
for (int i = 0; i < SIZE; i++)
cout
<<<< arr[i];
char arr1[] = { 'E','B','C', 'A','D'};
quicksort(arr1, 0, SIZE ‒ 1);
cout << "\n The sorted list of characters is\n";
for (int i = 0; i < SIZE; i++)
cout << "
" << arr1[i];
float arr2[] = {5.5,2.2,3.3,1.1,4.4 };
quicksort(arr2, 0, SIZE ‒ 1);
cout << "\n The sorted list of floats is\n";
for (int i = 0; i < SIZE; i++)
cout << "
" << arr2[i];
}
Output
Quick Sort Method
The sorted list of
integers is
12345
The sorted list of
characters is
A B C D E
The sorted list of
floats is
1.1 2.2 3.3 4.4 5.5
1. Explain function
templates with an example.
Object Oriented Programming: Chapter 5: Templates and Exception Handling : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Function Templates
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation