Function Template (Example) in c++ language , some basic and advance Function Template Examples

some basic and advance Function Template Examples , Function Template (Example) in c++ language :-
इससे पहले के article मे Templates को discuss किया था अब इस article मे tamplate के कुछ  उदाहरनो को discuss करेगे जिससे आ सभी के template से related concept अच्छी तरह से समज सकते है |
Template दो प्रकार के होते है :-
1.class Template
class template को generic class method से generate किया जाता है | in method मे class को use किया जाता है | इस method मे अलग अलग class को बनाया जाता है जिसमे अलग अलग function और variable को declare किया जाता है | इसका syntax निन्म होता है :
template <class any>
class class_name
{
public :
any variable_name ;
any function_name ()
{
function task ;
}
};
class object को declare करने के लिए निन्म method को use किया जाता है :
class_name <data_type> object_name ;
उदहारण के लिए
class_name <int> object_name ;
class_name <char> object_name ;
उदाहरन -1
write a program of calculator using template.
Explanation
सबसे पहले class calculator को declare किया जाता है | जिसमे  template type के दो variable t1 और t2 को define किया जाता है | उसके बाद  calculator () function template को declare किया जाता है जिसमे तो template type वरिअब्ल्ले को pass किया जाता है |
उसके बाद एक और function display () को declare किया जाता है |
display () function use द्वारा input दी गये value के बीच basic arithmatic operation को perform किया जाता है |
add() से addition perform होता है |
sub() से minus perform होता है |
mul() से multiplication perform होता है |
div() से division perform होता है |
इसके अलावा कला मे char basic arithmatic function add() , sub() , mul() और div() को भी declare किया जाता है |
main() function मे
सबसे पहले class template के object को declare किया जाता है |
अगर integer value कके लिए use करना होता है तब datatype मे int मे declare होता है | और जब float value के लिए use करना होता है तब float को declare किया जाता है |
source code
#inclue<iostrem>
template <class any >
class calculator
{
private :
any num1 , num2;
public:
calculator(any a , any b )
{
num1 = a;
num2 = b ;
}
void display()
{
cout<<“Addition is “<<add()<<endl;
cout<<“Subtraction is “<<sub()<<endl;
cout<<“Multipliaction is “<<mul()<<endl;
cout<<“Division is “<<div()<<endl;
}
any add()
{
return num1 +num2 ;
}
any sub()
{
return num1 – num2 ;
}
any mul()
{
return num1 *  num2 ;
}
any div
{
retuen num1 / num2 ;
}
};
void main()
{
calculator<int> intcal(12,3);
calculator<float> floatcal(12.0,3.5);
cout<<“calculation with integer value :”<<endl;
intca.display() ;
cout<<“calculation with float value :”<<endl;
floatcal.display() ;
getch();
}
इसका आउटपुट होगा :
calculation with integer value :
Addition is 15
Subtraction is 09
Multipliaction is 36
Division is 04
calculation with float value :
Addition is 15.5
Subtraction is 8.5
Multipliaction is 42.0
Division is 3.4287
2.function Template
class template को generic function method से generate किया जाता है | इस method मे नार्मल function  को use किया जाता है | इसका execution process normal function की तरह होता है लेकिन इसमें और normal function मे एक difference होता है | नार्मल function एक समय पर केवल एक type के datas के साथ use किया जा सकता है ल;यकीन function template को एक समय पर अलग अलग data type के data value के साथ use किया जा सकता है | इसका syntax निन्म होता है :
template <class any>
any function_name
{
function task ;
}
इस method मे , any template argument है जिसका use अलग अलग data type के लिए use किया जा सकता है |
class object को declare करने की जरुरत नहीं होती है  |
उदाहरन -2
write a program to find maximum number using template.
Explanation
सबसे पहले template को declare किया जाता है | जिसमे  template type के  variable any को define किया जाता है | उसके बाद  large() function template को declare किया जाता है जिसमे तो template type के दो variables  को pass किया जाता है |
इस function मे दोनों value को compare किया जाता है |
अगर num1 इस large than num2 तब num1 return होता है | और अगर num1 की value num 2 से छोटी होता है तब num2 return होता है |
main() function मे
सबसे पहले integer type के  दो variable num1 और num2 को declare किया जाता है जिसमे यूजर द्वारा input की गयी values को assign किया जाता है |
उसके बाद function large() को call किया जाता है| इस call मे function का return type integer होता है और इसमें pass होने वाले variable के type भी integer होता है |
इसके बाद दो float value को input करा लेते है  फिर large() function को call किया जाता है | इस call मे function का return type float होता है और इसमें pass होने वाले variable के type भी float होता है |
source code
#inclue<iostrem>
template <class any >
any large(any a , any b )
{
if(a>b)
{
return b ;
}
else
{
return a;
}
}
void main()
{
int num1 , num2;
float f1 , f2 ;
char c1 , c2 ;
cout<<“Input integer value “<<endl;
cin>>num1 >> num2;
cout<<“Input float value “<<endl;
cin>>f1>> f2;
cout<<“Input  character value “<<endl;
cin>>c1>>c2;
cout<<large(num1 , num2 )<<“is maximum integer value”<<endl;
cout<<large(f1 , f2 )<<“is maximum float value”<<endl;
cout<<large(c1 , c2 )<<“is maximum character value”<<endl;
getch();
}
इसका आउटपुट होगा :
Input integer value 12 23
Input float value 12.67 34.47
Input  character value a z
23 is maximum integer value
34.47 is maximum float value
z is maximum charecter value