हिंदी माध्यम नोट्स
Function Overload example in c++ language , what is Function overload source code with explanation
c++ language मे function polymorphism सबसे महतवपूर्ण capablity होती है | इसमें function मे pass किये गये arguuments की सख्या function definition मे declare किये गये arguments से कम या ज्यादा होता है | इस condition को function polymorphism कहते है | यस जब दो या दो से अधिक function का नाम समाए होता है तब इसे function polymorphism कहते है |
Function template
c++ language मे function template के concept को introduce किया है | function template एक generic function description है | generic function description मे function के type को define किया जाता है | जसिसे int या double |
किसी function template मे argument को pass किया जाता है तब complier एक generic function description को generate किया जाता है |इस programming को generic programming कहते है | अगर function मे argument के type को define किया जाता है जब इसे paramerized types कहते है |
Generic Programming
नीचे दी गये प्रोग्राम मे swap function को consider किया गया है | जिसमे दो integer value को swap किया जाता है | इसके अलावा swap function को double type के साथ भी discuss किया जाता है | लेकिन अगर char को swap किया जाता है तब इस function को भी use किया जाता है | लेकिन एक function को अलगअलग दत्त type को use किया जाता है | तब इस function से error होता है | इस error को change करने के लिए आप by hand भी change किया जाता है | तो ये method काफी time consume होता है | इस method के अलावा global search and replace method को use किया जाता है | उद्धरण के लिए
int a;
short int;
double case मे ,
double a ;
short double;
c++ language मे इस convert करने केलिए function template को use ककिया जाता है | जैसे
template <class any >
void swap ( any a , any b )
{
any temp ;
temp = a;
a=b;
b = temp;
}
इस method मे function template को set किया है | इस function template का नाम any है | template और class का declare करना सामान होता है | लेकिन इसमें keyword से differentia किया जता है | उसके बाद function template को {} मे define किया जाता है | type name template को define नहीं किया जाता है | इसके लिए कई प्रोग्रामर ‘T’ को ज्यादा use करते है | इसके अलावा function template मे function को define नहीं किया जाता है | इस function template से function template , complier को define करते है किस तरह से function को define किया जाता है |
जैसे अगर किसी दो integer को swap function को define किया जाता है इस function मे int को any से replace किया जाता है | और जैसे अगर किसी दो double को swap function को define किया जाता है इस function मे double को any से replace किया जाता है |
c++ language मे अभी अभी type name को class की जगह use किया जाता है | इसका syntax निन्म होता है :-
template < typename any >
void swap ( any a , any b )
{
any temp ;
temp = a;
a=b;
b = temp;
}
function template से complier को check किया जाता है | सबसे पहले function को call किया जाता है | तब सबसे पहले function मे pass argument को र्त्पे को पता लगाया जाता है इसके बाद उस type के function को generate किया जाता है |
Source Code
#include<iostream.h>
#include<conio.h>
template <class any>
void swap ( any &a , any &b);
void main()
{
using namespce std;
int a = 10 ;
int b = 120;
cout<<“Before Swap “;
cout<<“a :”<<a<<“b :”<<b;
cout<<“After swap using integer “;
swap(a,b);
cout<< “Now :”
cout<<“a :”<<a<<“b :”<<b;
double a = 10.40 ;
double b = 12.56 ;
cout<<“Before Swap “;
cout<<“a :”<<a<<“b :”<<b;
cout<<“After swap using double “;
swap(a,b);
cout<< “Now :”
cout<<“a :”<<a<<“b :”<<b;
getch();
}
template< class any >
void swap ( any a , any b )
{
any temp ;
temp = a;
a=b;
b = temp;
}
जब पहली swap () function को call किया जाता है तब निन्म function को generate किया जाता है जिसमे any को integer से replace किया जाता है | :-
void swap ( int a , int b )
{
int temp ;
temp = a;
a=b;
b = temp;
}
जब दुसरे swap () function को call किया जाता है तब निन्म function को generate किया जाता है जिसमे any को double से replace किया जाता है | :-
void swap ( double a , double b )
{
double temp ;
temp = a;
a=b;
b = temp;
}
इसका आउटपुट होगा :
Before Swap
a : 10 b : 20
After swap using integer
a : 20 b : 10
Before Swap
a : 10.40 b : 20.56
After swap using integer
a : 20.56 b : 10.40
इस article मे function template को discuss किया है अब आगे के article मे function template overload को discuss करेगे |
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…