WhatsApp Group Join Now
Telegram Join Join Now

C++ : Template Matching with program example source code , what is Template Matching in c++ language

what is Template Matching in c++ language , C++ : Template Matching with program example source code :-
इससे पहले के article मे , function template overload को discuss किया है | और किसी function के लिए बेस्ट template को find करने के लिए matching method को use किया जाता है | इस article मे matching को discuss करेगे |
matching भी दो प्रकार की होती है :
1. Exact matching
c++ language मे , exact matching के लिए trivial conversion को use किया जाता है | नीचे दिए गये table मे match को discuss किया गया है | उदाहरन के लिए actual parameter int के लिए int ही exact match होता है | लेकिन type conversion मे char & के लिए const char & और char & दोनों ही exact m,आतच हो सकता है |
त्योए matching का मतलब है की call function मे pass argument का type , function prototype मे declare arguments से match होना चाहिए | यस function pointer मे pass argument के type के सामान भी होना चाहिए | function pointer का मतलब है येसा pointer जो की किसी function की डेफिनिशन को hold करता है |
Table :
Actual Parameter
Type
Type
Type &
Type &
Type
Type []
*type
Type {argument-list}
Type (*) {argument list }
Type
Type const
Type
Type volatile
Type *
Const type *
Type *
Volatile type *
नीचे दिए code मे ,
struct book {char name [50]; int price ; };
book b = {“good boy”, 230};
………..;
sell(b);
नीचे दिए गये prototype इस function के लिए exact match है :-
void sell( book );
void sell(const book );
void sell (book & );
void sell(const book & );
in सभी exact match मे से एक बेस्ट match को find करने का कार्य complier करता है | complier overload resolution प्रोसेस का use नहीं कर सकता है | ओएँ सभी मे से अगर कोई भी बेस्ट vialble function नहीं होता है तब complier error message दे देता है |
case 1 :
कभी कभी overload resolution को generate कर दिया जाता है जन दो या दो से अधिक function मे exact match आ जाता है |pointer और reference to const data , mon const pointer या reference से perfectly match होता है | इसलिए केवल function 3 और 4 ही इस function के लिए बेस्ट solution होता है | इन दोनों मे से function 3 को choose किया जाता है क्योकि इसमें const variable को use नहीं किया गया है |
case 2 :
कोई function prototype , दुसरे function prototype से बेस्ट तब होता है जब पहला function non template function होता है दूसरा template function होता है | इस condition मे non template function , तम्प्लाते function से बेस्ट होता है |
case 3 :
जब दोनों ही function template function होता है तब जो भी function more sepecilized होता है use बेस्ट consider किया जाता है |
उदाहरन के लिए
struct blot {int a ; char b [10 ];};
tamplate <class type > void recyle (type t );
tamplate <> void recycle<blot> (blot & t) ;
………………
blot ink = {25 , “Game”};
…….
recycle(ink) ;
केवल more sepecilized होने से function tamplate बेस्ट नहीं होता है बल्कि इस function template मे execute होने वाले conversion पर भी निर्भर करता है | जैसे
tamplate <class type > void recyle (type t );
tamplate <class type > void recyle (type *t );
और main प्रोग्राम मे ,
main()
{
struct blot b = {“12”, “game”};
cout<<“blot size : “<<b.a;
cout<<“blot name : “<<b.b ;
recycle( &b);
cout<<“blot size : “<<b.a;
cout<<“blot name : “<<b.b ;
}
function call recycle(b); tamplate 1 से exact match होता है | लेकिन जब function call मे blot * को pass किया जाता है तब tamplate 2 से exact match होता है |
matching को समजने के लिए एक पूरा code को consider करता है :
#include<iostream>
template<typename t > // tamplae 1
void showarray(t a[] , int size )
template<typename t > // tamplae 2
void showarray(t * a[] , int size )
struct salary
{
char name[50];
int salary ;
};
void main()
{
using namespace std;
int dep[3]={1 , 2 , 3 };
struct salary [3]
{
{“parth”, 20000};
{“Om”,25000};
{“rahul”, 30000};
}
double * p[3];
for(int i =0 ; i<3;i++)
{
p[i]=&saalry.salary ;
}
cout<< “Department : “<<endl;
showarray(dep, 3);
cout<<“salary : “<<endl;
showarray(p, 3 );
getsch () ;
}
template<typename t>
void showarray( t a[], size )
{
using namespace std;
cout<<“template 1 “;
for (int i =0 ; i< size ; i ++)
{
cout<<a[i];
}
cout<<endl;
}
template<typename t>
void showarray( t a[], size )
{
using namespace std;
cout<<“template 2 “;
for (int i =0 ; i< size ; i ++)
{
cout<<*a[i];
}
cout<<endl;
}
जब function showarray(dep, 3); को call किया जाता है तब dep एक integer type array का नाम है इसके लिए बेस्ट template निन्म होता है :
template<typename t>
void showarray( t a[], size )
इसमें t एक integer type template है |
जब function showarray(p, 3 ); को call किया जाता है तब p एक int type pointer है जो की array के तीन address को hold करता है | जब इस function को call किया जाता है तब नीचे दिए गये function tamplate को use किया जाता है :-
template<typename t>
void showarray( t*a[], size )
इसका output होगा :
Department :
1
2
3
Salary :
10000
15000
20000
इस article मे , template के सबसे complicated concept matching को discuss किया है | अब आगे के article मे template पर based कुश उदाहरनो को discuss करेगे जिससे आ सभी के matching और template के concept को अच्छी तरह से समज सकते है |