हिंदी माध्यम नोट्स
Categories: c++ language in hindi
FUNCTION TEMPLATE ( EXAMPLE ) in c++ language with program examples source code explanation
source code explanation , FUNCTION TEMPLATE ( EXAMPLE ) in c++ language with program examples :-
इससे पहले के article मे , c++ language के powerful concept template के उदाहरणों को discuss किया है अब इस article मे template को बाकि डट type matrix , string और array के साथ उदाहरनो को discuss करेगे |
template with matrix
इस उदाहरन matrix को addition,subtraction और multiplication को calculate किया जाता है |
Explanation
सबसे पहले template को define किया जाता है
class matrix को declare किया जाता है |
इसमें construtor matrix को बनाया जाता है | जिसमे 2 * 2 matrix के सभी चार elements को pass किया जाता है |
इसका बाद inputmatrix() से matrix मे value को input किया जाता है |
इसके बाद addmatrix() function को declare किया जाता है |
और multiply() function और sub() function भी declare किया जाता है |
display () function को भी declare किया जाता है |
इसके बाद class template को define किया जाता है | इसमें यूजर द्वारा input किये गये value को matrix मे assign किया जाता है |
इसके लिए constructor को use किया जाता है | इसमें मेट्रिक्स को loop के बिने ही assign किया जाता है |
इसके बाद matrix के element को दुसरे मेट्रिक्स मे assign किया जाता है |
इस function matrix() के लिए दो function template को use किया जाता है | इसके लिए पहले
template मे matrix मे elements को assign किया जाता है |
और दुसरे matrix मे element को ‘0’ से assign किया जाता है |
इसके बाद add() function को डेफिन किया जाता है | जिसमे दो matrix को pass किया जाता है | matrix m और x को define किया जाता है |
matrix type के sum variable को define किया जाता है |जिसमे matrix को add करने प्राप्त आउटपुट को assign किया जाता है |
इसके बाद sub() function को डेफिन किया जाता है | जिसमे दो matrix को pass किया जाता है | matrix m और x को define किया जाता है |
matrix type के sub variable को define किया जाता है |जिसमे matrix को subtraction करने प्राप्त आउटपुट को assign किया जाता है |
इसके बाद mul() function को डेफिन किया जाता है | जिसमे दो matrix को pass किया जाता है | matrix m और x को define किया जाता है |
matrix type के mul variable को define किया जाता है | जिसमे matrix को multiply करने प्राप्त आउटपुट को assign किया जाता है |
using namespce std;
template<class any>
class matrix
{
puiblic :
matrix(any_a, any b , any c, any d);
matrix (any m[2][2]);
matrix();
int add (matrix);
int sub(matrix);
int mul(matrix);
void display();
any m[2][2];
}
template<class any >
matrix<any>:: matrix(any_a, any b , any c, any d)
{
m[0][0]=a;
m[0][1]=b;
m[1][1]=c;
m[1][0]=d;
}
template<class any >
matrix<any>:: matrix(any_m)
{
m[0][0]=_m[0][0];
m[0][1]_m[0][1];
m[1][1]=_m[1][1];
m[1][0]=_m[1][0];
}
template<class any >
matrix<any>:: matrix( )
{
m[0][0]= 0;
m[0][1]= 0;
m[1][1]= 0;
m[1][0]= 0;
}
template<class any >
matrix<any>:: add( matrix x )
{
matrix<any> sum ;
sum.m[0][0]= m[0][0]+x.m[0][0];
sum.m[0][1]= m[0][1]+x.m[0][1];
sum.m[1][0]= m[1][0]+x.m[1][0];
sum.m[1][1]= m[1][1]+x.m[1][1];
retusn sum;
}
template<class any >
matrix<any>:: sub( matrix x )
{
matrix<any> sub;
sub.m[0][0]= m[0][0]-x.m[0][0];
sub.m[0][1]= m[0][1]-x.m[0][1];
sub.m[1][0]= m[1][0]-x.m[1][0];
sub.m[1][1]= m[1][1]-x.m[1][1];
retusn sub;
}
template<class any >
matrix<any>:: mul( matrix x )
{
matrix<any> mul;
mul.m[0][0]= m[0][0]*x.m[0][0];
mul.m[0][1]= m[0][1]*x.m[0][1];
mul.m[1][0]= m[1][0]*x.m[1][0];
mul.m[1][1]= m[1][1]*x.m[1][1];
return mul ;
}
void main()
{
matrix <any> x(1,2,3,4);
matrix<any> y(2,3,45,3);
matrix<any> a = x.add(y);
matrix<any> a = x.sub(y);
matrix<any> a = x.mul(y);
}
उदाहरन -2
write a program to find square of number using template.
Explantion
सबसे पहले tempalte को declare किया जाता है | जिसमे template type के variable any को define किया जाता है | उसके बाद square() function template को declare किया जाता है जिसमे तो template type के एक variables को pass किया जाता है |
इस function मे square operation number*number को calculate किया जाता है |
main() function मे
सबसे पहले integer type के एक variable number को declare किया जाता है जिसमे यूजर द्वारा input की गयी values को assign किया जाता है |
उसके बाद function square() को call किया जाता है| इस call मे function का return type integer होता है और इसमें pass होने वाले variable के type भी integer होता है |
इसके बाद एक float value को input करा लेते है फिर square() function को call किया जाता है | इस call मे function का return type float होता है और इसमें pass होने वाले variable के type भी float होता है |
source code
#inclue<iostrem>
template <class any >
any square( any a )
{
return a*a;
}
void main()
{
int num;
float f;
cout<<“Input integer value “<<endl;
cin>>num;
cout<<“Input float value “<<endl;
cin>>f;
cout<<sqaure(num)<<“is square value of integer value”<<num <<endl;
cout<<sqaure(f)<<“is square value of float value”<<f<<endl;
getch();
}
इसका आउटपुट होगा :
Input integer value 12
Input float value 12.6
144 is square value of integer value 12
156.32 is square value of float value 12.6
उदाहरन -3
write a program to find cube of number using template.
Explantion
सबसे पहले tempalte को declare किया जाता है | जिसमे template type के variable any को define किया जाता है | उसके बाद cube() function template को declare किया जाता है जिसमे तो template type के एक variables को pass किया जाता है |
इस function मे cube operation number*number* number को calculate किया जाता है |
main() function मे
सबसे पहले integer type के एक variable number को declare किया जाता है जिसमे यूजर द्वारा input की गयी values को assign किया जाता है |
उसके बाद function cube() को call किया जाता है| इस call मे function का return type integer होता है और इसमें pass होने वाले variable के type भी integer होता है |
इसके बाद एक float value को input करा लेते है फिर cube() function को call किया जाता है | इस call मे function का return type float होता है और इसमें pass होने वाले variable के type भी float होता है |
source code
#inclue<iostrem>
template <class any >
any cube( any a )
{
return a*a*a;
}
void main()
{
int num;
float f;
cout<<“Input integer value “<<endl;
cin>>num;
cout<<“Input float value “<<endl;
cin>>f;
cout<<sqaure(num)<<” is cube of “<<num <<endl;
cout<<sqaure(f)<<” is cube of “<<f<<endl;
getch();
}
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago