Short note : Template function in hindi
Introduction
Template C++ का एक नया feature है। यह एक new concept है जो generic classes तथा function define करने के साथ साथ generic program के features को भी support करता है। Generic Programming एक ऐसा approach है जिनमें algorithm, parameter के रूप में generic type का use करता है जो कि विभिन्न प्रकार के data types तथा data structure को use करने की facility provide करता है।
Definition of Template function
समान function की facility के लिए एक single function लिखने की पद्धति को Template function कहा जाता है।
Declaration
C++ में Template function को declare करने के लिए normal syntax है-
Template<claasT>
T fuction name( T formal arguments)
{
........................
.......................
return(T);
}
जहाँ template एवं class, C++ में keyword है और T एक parameterized data type है।
Program
#include<iostream.h>
#include<conio.h>
template <classT>
void swap (T & x, T & y)
{
T temp =x;
x=y;
y=temp;
}
void fun(int m,int n, float a, float b)
{
cout<<"m and n before swap : <<m<<" "<<n<<\n;
swap(m,n);
cout<<"m and n after swap :"<<m<<" "<<n<<"\n;
swap(a,b)
cout<<"a and b after swap :"<<a <<" "<<b<<"\n";
}
void main(()
fun(100,200,11.22,33.44);
getch();
}
OUTPUT
m and n before swap : 100 200
m and n after swap : 200 100
a and b before swap : 11.22 33.44
Advantages of Template Function in hindi
- Template function use करने का मुख्य लाभ source code के अनावश्यक repetation को रोकना है।
- Function को define एवं clear करने की अपेक्षा object code अधिक short एवं प्रभावी होना चाहिए।
- यह एक Generic या parameterized data type का use करता है।
Template function in hindi की लेख पसंद आयी हो तो इसे जरूर शेयर करें
Hi please, do not spam in comments