Foundations — Templates — function templates, class templates
5.2.14 · D1· Coding › C++ Programming › Templates — function templates, class templates
Ye foundations page kuch bhi assume nahi karta. Har squiggle, keyword, aur angle bracket jo parent note mein hai, yahan se ground up build ki gayi hai, ek aisi order mein jahan har piece sirf pehle wale pieces pe lean karta hai.
0. "Type" kya hota hai? (wo blank jise hum parameterize karenge)
Pehle type ko parameter banane se pehle, tumhe bilkul clear hona chahiye ki type kya hoti hai.
Picture: socho ek value ek labelled box mein rehne wale object ki tarah. Label hi type hai. Wahi bit pattern alag labels ke neeche alag cheezein mean karta hai — isliye label matter karta hai.
Topic ko ye kyun chahiye: templates exactly isliye exist karte hain kyunki wahi algorithm (return a > b ? a : b) kaafi alag labels ke liye kaam karta hai. Jab tum dekh lo ki int, double, aur string sirf ek hi logic shape pe interchangeable labels hain, toh "label blank chhod do" ki desire obvious ho jaati hai. Dekho Compile-time vs Runtime ki kab label decide hota hai.
1. Function — typed slots ke saath ek named recipe
int maximum(int a, int b) { return (a > b) ? a : b; }
// ^ret ^name ^param1 ^param2 ^body- Name se pehle
int= return type (answer box pe label). a,b= parameters, har ek ki typeinthai.(a > b) ? a : b= ternary operator: ise padho "agara > btrue hai, tohado, nahi tohbdo". Ye sirf ek compactif/elsehai jo ek value produce karta hai.
Picture: ek machine jiski do labelled input trays aur ek labelled output tray hai. Wahi machine kaam karti hai jo bhi tum feed karo — jab tak trays us type ko accept karti hain.
Topic ko ye kyun chahiye: parent exactly isi concrete maximum se shuru karta hai. Template tab paida hota hai jab tum yahan har int ki taraf point karo aur pucho "agar ye blank hota?" Agar tumhe manually ek recipe kai types ke liye likhne ki practice chahiye, wo hai Function Overloading — templates uski jagah lete hain.
2. Body jo operators silently rely karta hai: > aur copy
Body do operations use karti hai, aur sirf do:
>— greater-than comparison.trueyafalsereturn karta hai.- Copy —
return avalue ki ek copy wapas deta hai.
Topic ko ye kyun chahiye: ye samajhna ki ek template quietly kuch operations demand karta hai, "templates magic hain" aur "templates ek substitution phir compile hain" ke beech ka fark hai. Ye idea Generic Programming ka seed hai.
3. <> — wo angle brackets jo parameters carry karte hain
Tumne () ko value arguments carry karte dekha hai: maximum(3, 7). Templates ek doora type ka bracket introduce karte hain.
| Bracket | Carry karta hai | Kab decide hota hai |
|---|---|---|
( ) |
values (data) | runtime pe |
< > |
types / compile-time numbers | compile time pe |
Picture: do conveyor belts ek hi machine ko feed kar rahe hain. () belt values daalta hai; <> belt type-labels daalta hai aur pehle, build time pe, khaali ho jaata hai.
Topic ko ye kyun chahiye: vector<int>, Box<string>, Array<double, 5> — un <...> mein se har ek compile-time belt ka kaam hai. Is split ko samajhna hi Compile-time vs Runtime hai.
4. T — ek naam jo ek type ke liye khada hota hai
T ko f(x) = x + 1 mein x ki tarah socho. x koi number nahi hai — ye ek stand-in hai jise tum real number se replace karte ho jab f actually use karte ho. Isi tarah T ko ek real type (int, string) se replace kiya jaata hai jab tum template actually use karte ho.
Topic ko ye kyun chahiye: T poori parent page ka sabse zyada use hone wala symbol hai. Har T maximum(T a, T b) padha jaata hai "ek function jiska return, pehla param, aur doosra param sabka ek hi type hai, jise T kaha jaata hai".
5. template<typename T> — wo header jo blank declare karta hai
Ab pieces assemble karte hain:
template<typename T>
T maximum(T a, T b) { return (a > b) ? a : b; }Pehli line ko teen alag words ki tarah padho:
template— wo keyword jo announce karta hai "jo aage hai wo blueprint hai, final code nahi".<typename T>— "iska ek blank hai; blank ek type hai, aur main iseTbulaunga".typename— literally "ek type ka naam". Tumclass Tbhi likh sakte ho — type parameter ke liye identical meaning (ek historical quirk, Macros vs Templates aur parent ke steel-man mein covered).
Picture: ek rubber stamp. template<...> head handle aur khaali die hai; neeche wala function T ki jagah wala engraving hai. Ise int pe press karo aur jagah int se bhar jaati hai.
Topic ko ye kyun chahiye: yahi exact header parent page pe har function template aur class template kholti hai. Agar tum is line ko plain words mein zor se padh sako, baaki note follow karta hai.
6. Instantiation — stamp actually pressing karna
Key consequence (parent pe stated): ek unused template zero machine code produce karta hai. Stamp tab imprint karta hai jab tum ise press karte ho. Isliye templates ki zero runtime overhead hoti hai — stamped code exactly wahi hai jo tum hand-written karte.
Topic ko ye kyun chahiye: "instantiation" wo verb hai jo blueprint ko working part mein turn karta hai; templates ka poora payoff yahan rehta hai.
7. Specialization — ek type ke liye hand-carved override
Parent se example: ek bool print karna. Generic cout << b 1/0 print karta hai; specialization true/false print karta hai. Compiler hamesha sabse specific matching version prefer karta hai. Full treatment Template Specialization mein hai.
Picture: tumhare paas ek star cookie-cutter hai (generic template). Ek special order ke liye tum cutter side rakh dete ho aur haath se ek alag star sculpt karte ho (specialization). Baaki sab ko machine-stamped star milta rehta hai.
Topic ko ye kyun chahiye: ye answer karta hai "agar ek size sab pe fit na ho toh?" — ek sawaal jo reader ko zaroor aayega.
Prerequisite map
Har box sirf uske andar aane wale boxes use karta hai — upar se neeche padho aur koi cheez build hone se pehle use nahi hoti.
Equipment checklist
Right side cover karo aur khud test karo. Agar koi jawab fuzzy lage, parent note kholne se pehle wo section phir padho.
Type computer ko kya batata hai?
int maximum(int a, int b) mein return type aur parameters batao.
int; parameters a aur b, dono type int ke.Ternary (a > b) ? a : b plain words mein kya maane rakhta hai?
a > b true hai, toh a do, nahi toh b do.maximum ki body kisi bhi type se secretly kaun se do operations require karti hai?
> (greater-than) aur value ko copy karne ki ability.( ) kya carry karte hain, aur kab decide hote hain?
< > kya carry karte hain, aur kab decide hote hain?
Kya T ek type hai?
template<typename T> ko plain words mein zor se padho.
T bulaaunga."