Template function: download templatesort.cpp from here
File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction
VISIT SITE LIFEQUEST.COM
Hi friends,
This blog is related with my previous blog of template class for better understanding have a look at this link if you have not see it yet.
Template class in C++
template function are same as template classes, here i have created a template function for sorting numeric numbers of any type using same template function.
Look at template function declaration of sorting array:
SORT Function:
template <class sample>
void sort(sample a[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
if(a[i]>a[j])
swap(a[i],a[j]);
}
}
}
Swap function:
template <class x>
void swap(x &p,x &q)
{
x temp;
temp=p;
p=q;
q=temp;
}
Don't confuse with class keyword in both classes it still treated as function only.
Main function:
void main()
{
clrscr();
int m[3];
float n[3];
clrscr();
for(int i=0;i<3;i++)
{
cout<<endl<<"Enter integer number:";
cin>>m[i];
}
cout<<endl<<"Original integer list:"<<endl;
for(i=0;i<3;i++)
cout<<m[i]<<endl;
sort(m,3);
cout<<endl<<"Sorted intger list:"<<endl;
for(i=0;i<3;i++)
cout<<m[i]<<endl;
for( i=0;i<3;i++)
{
cout<<endl<<"Enter float number:";
cin>>n[i];
}
cout<<endl<<"Original float list:"<<endl;
for(i=0;i<3;i++)
cout<<n[i]<<endl;
sort(n,3);
cout<<endl<<"Sorted float list:"<<endl;
for(i=0;i<3;i++)
cout<<n[i]<<endl;
getch();
}
Here i have created two array one of integer and another of float, sort both array using sort function.
Download program from here:
Template function: download templatesort.cpp from here
OUtput Snap:
So that's it!
Once again this blog is related with my previous blog of template class for better understanding have a look at this link if you have not see it yet.
Template class in C++
Template class in C++
Other such programs:
File operation in c++
Stack Operation in C++
Queue simulation in C++
Use of static keyword in c++
DOWNLOAD BINARY ADDITION AND COPY CONSTRUCTOR.CPP FILE FROM HERE
Binary Search in C++
Operator overloading concept in c++
New and delete operator in c++
Call by value and call-by-refrence in c++
c++ macro:Invoked inline during program execution
Create User Defined Function In C++: Header Files
C++ Polymorphisamusingvirtualfunction
VISIT SITE LIFEQUEST.COM