Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔴 Advanced  ·  Lesson 51

Templates in C++

Templates क्या हैं?

Templates आपको एक बार ऐसा code लिखने देते हैं जो किसी भी type के लिए काम करे। int, double और string के लिए अलग max लिखने के बजाय, आप एक template लिखते हैं और compiler हर version बनाता है जो आपको चाहिए। इसे generic programming कहते हैं।

Function templates

Function से पहले template <typename T> लगाएँ और T को type के रूप में इस्तेमाल करें:

C++
#include <iostream>
using namespace std;
template <typename T>
T maxOf(T a, T b) {
    return (a > b) ? a : b;
}
int main() {
    cout << maxOf(3, 7) << "\n";        // int
    cout << maxOf(2.5, 1.5) << "\n";    // double
    cout << maxOf('a', 'z') << "\n";    // char
    return 0;
}
Output:
7
2.5
z

Compiler हर बार arguments से T निकालता है।

कई type parameters

C++
template <typename A, typename B>
void show(A a, B b) {
    cout << a << " and " << b << "\n";
}
// show(1, "hi");  → int and string

Class templates

Class template पूरी class को generic बनाता है — containers के लिए एकदम सही:

C++
template <typename T>
class Box {
    T value;
public:
    Box(T v) : value(v) {}
    T get() { return value; }
};
int main() {
    Box<int> a(42);
    Box<string> b("hello");
    cout << a.get() << " " << b.get();   // 42 hello
}
Output:
42 hello

यहाँ आप type स्पष्ट रूप से बताते हैं: Box<int>, Box<string>। संक्षिप्त syntax reference के लिए, templates syntax page देखें।

Templates और STL

पूरी Standard Template Library templates पर बनी है। इसीलिए vector<int>, vector<string> और map<string,int> सब एक ही generic code से आते हैं, और sort जैसे algorithms किसी भी उपयुक्त container पर काम करते हैं।

Templates कैसे काम करते हैं

Template अपने आप में code नहीं है — यह एक नुस्खा है। जब आप इसे किसी type के साथ इस्तेमाल करते हैं, compiler इसे instantiate करता है, उस type के लिए एक concrete version बनाते हुए। अप्रयुक्त templates कुछ नहीं बनाते।

आम गलतियाँ

  • Definition से पहले template <typename T> भूलना।
  • यह उम्मीद करना कि compiler class template का type निकालेगा (आप अक्सर इसे बताते हैं)।
  • Template के साथ ऐसा type इस्तेमाल करना जिसमें ज़रूरी operator न हो (जैसे maxOf में >)।
  • Template की declaration और definition को files में लापरवाही से बाँटना।
🏋️ अभ्यास

एक function template sumArray(T arr[], int n) लिखें जो किसी भी numeric array का योग लौटाए। फिर एक class template Pair<A,B> लिखें जो अलग types की दो values रखे और उन्हें print करे।

सारांश

  • Templates एक बार ऐसा code लिखते हैं जो किसी भी type के लिए काम करे।
  • Function templates arguments से type निकालते हैं।
  • Class templates को type बताना पड़ता है, जैसे Box<int>।
  • Compiler हर इस्तेमाल किए type के लिए एक concrete version बनाता है।
  • STL पूरी तरह templates से बनी है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में templates क्या हैं?
Templates आपको generic code लिखने देते हैं जो किसी भी data type के साथ काम करता है, हर type के लिए इसे दोबारा लिखे बिना। आप एक function या class एक बार placeholder type parameter के साथ परिभाषित करते हैं, और compiler हर type के लिए जो आप वास्तव में इस्तेमाल करते हैं एक विशिष्ट version बनाता है।
C++ में function template और class template में क्या अंतर है?
Function template जिस type के साथ आप इसे call करें उसके लिए एक function बनाता है, जैसे एक generic max जो ints या doubles पर काम करे। Class template किसी type के लिए पूरी class बनाता है, जैसे एक container जो कोई भी element type रख सके, यही तरीका है जिससे STL vector और समान types बनाता है।
C++ में function template कैसे लिखते हैं?
आप function से पहले template <typename T> लगाते हैं और फिर अंदर T को type के रूप में इस्तेमाल करते हैं, जैसे template <typename T> T maxOf(T a, T b)। जब आप इसे call करते हैं, compiler arguments से T निकालता है और मेल खाता version बनाता है।
C++ में template में typename और class में क्या अंतर है?
Template parameter सूची में, typename और class का बिल्कुल एक ही अर्थ है, तो template <typename T> और template <class T> विनिमेय हैं। कई programmers typename पसंद करते हैं क्योंकि यह किसी भी type के लिए ज़्यादा साफ़ पढ़ा जाता है, केवल class types के लिए नहीं।
C++ Standard Template Library templates का उपयोग कैसे करती है?
Standard Template Library पूरी तरह templates से बनी है, तो vector, map और set जैसे containers, और sort जैसे algorithms, किसी भी उपयुक्त type के साथ काम करते हैं। इसीलिए आप एक ही template code से vector<int> या vector<string> रख सकते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicException Handling आपकी प्रगति: 51 / 81 (63%) · Lesson 52 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें