हिंदी माध्यम नोट्स
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
सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है
सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…
21 hours ago
मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the
marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…
21 hours ago
राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi
sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…
3 days ago
गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi
gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…
3 days ago
Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन
वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…
3 months ago
polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten
get all types and chapters polity notes pdf in hindi for upsc , SSC ,…
3 months ago