JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

MEMBER FUNCTION AND TEMPLATE in c++ language in hindi , program example source code

program example source code , MEMBER FUNCTION AND TEMPLATE in c++ language in hindi :-
इससे पहले के article function template और class member function को discuss किया था अब इस article मे class member function template को discuss करेगे |
जब किसी template को class method से define किया जाता है | तब इसे define करने के लिए दो method होते है | पहले method मे class scope के अन्दर जिसे हम पहले discuss आकर चुके  | दुसरे method मे template के डेफिनिशन को class scope के बहार define किया जाता है | इसके लिए स्कोप resolution operator का use किया जाता है | जब यूजर किसी template को केवल class के लिए create करता है तब उसे member function template को use नहीं करना चाहिए | अतः इस method का use उस case मे किया जाता है तब किसिस template को दो या दो से अधिक class या file मे use करना होता है |
इसका syntax नोरमल function की तरह होता है | इस समजने  के लिए निन्म उदाहरन को consider करेगे :-
method 1
template <class any > class display
{
public:
void function(any variable );
}
void display <int> :: function (int)
{
display statemenet ;
}
void display <char> :: function (char)
{
display statemenet ;
}
void main()
{
int a=9 ;
display s;
s.function(a);
}
इस method मे template class display को create किया जाता है | जिसमे function () function है |
इसके बाद template की definition को define किया जाता है | इसमें दो function template को define किया जाता है एक तो integer के लिए | दूसरा character के लिए |
method 2
template <class any > class display
{
public:
void function(any variable );
}
templat <class any > void display<any> :: function ( any )
{
display statemenet ;
}
void main()
{
int a=9 ;
display s;
s.function(a);
}
इस method मे class template display को declare किया जाता है |
इसके बाद केवल एक template की define किया जाता है जिसे किसी भी data type के लिए use किया जा सकता है |
method 3
template <class any > class display
{
public:
void function(any variable )
{
display statement ;
}
};
void main()
{
int a=9 ;
display s;
s.function(a);
}
इस method मे template को class display के अन्दर ही define किया गया है |
अब function member template के उदाहरन को discuss करगे :-
उदाहरन 1
/* swaping function using class member function template */
इस उदाहरन मे swap function मे template को use किया जाता  है लेकिन इसे class के बहार declare किया जाता है जिससे इसे किसिस दुसरे class या file मे access किया जासकता है | और ये private variable और function को भी access भी कर सके |
इस उदाहरन मे main() function दो तरह के data type के लिए swap() function को use किया जाता है |
integer के लिए template variable का type integer हो जाता है |
और float values के लिए  template variable का type float हो जाता है|
‘ओत जब template को declare किया जाता है तब method 1 और method 3 दोनों मे से कोई एक method को use किया जा सकता है |
नीचे दिए गये उदाहरन मे method 1 मे ,
class मे केवल  memeber function के header को define किया गया है |
इसके बाद इसे class scope के बहार scope resolution के साथ define किया गया है | इसका syntax नोरमल function की तरह होता है लेकिन इन्समे template को use किया गया है |
method 2 मे member function को class के scope मे ही declare किया गया है |
#include<iostream.h>
#include<conio.h>
template <class any>
void swap ( any &a , any &b);
void main()
{
using namespce std;
int a = 110 ;
int b = 120;
cout<<“Before Swap operation “;
cout<<“a :”<<a<<” and b :”<<b;
cout<<“After swap operation using integer “;
swap(a,b);
cout<< ” after swap operation using integer  :”
cout<<“a :”<<a<<” and b :”<<b;
double a = 12.40 ;
double b = 10 .56 ;
cout<<“Before Swap operation  “;
cout<<“a :”<<a<<” and b :”<<b;
cout<<“After swap operation using double “;
swap(a,b);
cout<< “Now :”
cout<<“a :”<<a<<” and b :”<<b;
getch();
}
method 1 :
template< class any >
class operation
{
public :
void swap(any a , any b );
}
templat <class any > void operation <any> :: swap ( any a , any b )
{
any temp ;
temp = a;
a= b ;
b = temp ;
}
method 2 :
template< class any >
void swap ( any a , any b )
{
any temp ;
temp = a;
a=b;
b =  temp;
}
उदाहरन 2
write a program to find maximum number using member function template.
Explanation
सबसे पहले template को declare किया जाता है | इसके बाद class function को define किया जाता है | जिसमे  template type के  variable any को define किया जाता है | उसके बाद  large() function template के prototype को define किया जाता है
इसके बाद class function को क्लोज किया जाता है |
इसके बाद class member function template () को declare किया जाता है  जिसमे any type के दो variable को pass किया जाता है | इस function मे ,
अगर n1 इस large than n2  तब n1 return होता है | और अगर n1 की value num 2 से छोटी होता है तब n2  return होता है |
main() function मे
सबसे पहले integer type के  दो variable n1 और n2  को declare किया जाता है जिसमे यूजर द्वारा input की गयी values को assign किया जाता है |
उसके बाद function class के object को define किया जाता है | फिर इस object से function large() को call किया जाता है| इस function मे integer value को pass किया जाता है इस case मे any का type integer होता है |
इसके बाद दो float value को input करा लेते है  फिर object से large() function को call किया जाता है |  इस function मे float value को pass किया जाता है इस case मे any का type integer होता है | और इस function मे return type भो float होता है |
source code
#inclue<iostrem>
template <class any >
class function
{
private :
any a ,b ;
public large (any a , any b );
}
template <class any > void function  <any> :: large ( any a , any b )
{
if(a>b)
{
return b ;
}
else
{
return a;
}
}
void main()
{
int n1 , n2 ;
float f1 , f2 ;
char c1 , c2 ;
function f ;
cout<<“Input integer value “<<endl;
cin>>n1 >> n2 ;
cout<<“Input float value “<<endl;
cin>>f1>> f2;
cout<<“Input  charecter value “<<endl;
cin>>c1>>c2;
cout<<f.large(n1 , n2  )<<“is largest integer value”<<endl;
cout<<f.large(f1 , f2 )<<“is largest float value”<<endl;
cout<<f.large(c1 , c2 )<<“is largest charecter value”<<endl;
getch();
}
इसका आउटपुट होगा :
Input integer value 24 23
Input float value 15.67 34.47
Input  charecter value p r
24 is largest integer value
34.47 is largest float value
r is largest charecter value
इस article मे member function class को discuss किया है अब आगे के article मे c++ constructor और destructor को भी discuss करेगे |
Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

13 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

13 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 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
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now