JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

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

Class 6

Hindi social science science maths English

Class 7

Hindi social science science maths English

Class 8

Hindi social science science maths English

Class 9

Hindi social science science Maths English

Class 10

Hindi Social science science Maths English

Class 11

Hindi sociology physics physical education maths english economics geography History

chemistry business studies biology accountancy political science

Class 12

Hindi physics physical education maths english economics

chemistry business studies biology accountancy Political science History sociology

Home science Geography

English medium Notes

Class 6

Hindi social science science maths English

Class 7

Hindi social science science maths English

Class 8

Hindi social science science maths English

Class 9

Hindi social science science Maths English

Class 10

Hindi Social science science Maths English

Class 11

Hindi physics physical education maths entrepreneurship english economics

chemistry business studies biology accountancy

Class 12

Hindi physics physical education maths entrepreneurship english economics

chemistry business studies biology accountancy

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

four potential in hindi 4-potential electrodynamics चतुर्विम विभव किसे कहते हैं

चतुर्विम विभव (Four-Potential) हम जानते हैं कि एक निर्देश तंत्र में विद्युत क्षेत्र इसके सापेक्ष…

1 day ago

Relativistic Electrodynamics in hindi आपेक्षिकीय विद्युतगतिकी नोट्स क्या है परिभाषा

आपेक्षिकीय विद्युतगतिकी नोट्स क्या है परिभाषा Relativistic Electrodynamics in hindi ? अध्याय : आपेक्षिकीय विद्युतगतिकी…

3 days ago

pair production in hindi formula definition युग्म उत्पादन किसे कहते हैं परिभाषा सूत्र क्या है लिखिए

युग्म उत्पादन किसे कहते हैं परिभाषा सूत्र क्या है लिखिए pair production in hindi formula…

5 days ago

THRESHOLD REACTION ENERGY in hindi देहली अभिक्रिया ऊर्जा किसे कहते हैं सूत्र क्या है परिभाषा

देहली अभिक्रिया ऊर्जा किसे कहते हैं सूत्र क्या है परिभाषा THRESHOLD REACTION ENERGY in hindi…

5 days ago

elastic collision of two particles in hindi definition formula दो कणों की अप्रत्यास्थ टक्कर क्या है

दो कणों की अप्रत्यास्थ टक्कर क्या है elastic collision of two particles in hindi definition…

5 days ago

FOURIER SERIES OF SAWTOOTH WAVE in hindi आरादंती तरंग की फूरिये श्रेणी क्या है चित्र सहित

आरादंती तरंग की फूरिये श्रेणी क्या है चित्र सहित FOURIER SERIES OF SAWTOOTH WAVE in…

1 week 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