Templates in C++ Notes | Function Templates, Class Templates & Generic Programming | Basic Computer Engineering | RGPV BTech First Year
Templates in C++ Notes | Function Templates, Class Templates & Generic Programming | Basic Computer Engineering | RGPV BTech First Year
Templates in C++
Templates C++ की एक शक्तिशाली विशेषता (Powerful Feature) है जो Generic Programming को संभव बनाती है। Templates की सहायता से ऐसा Code लिखा जा सकता है जो अलग-अलग Data Types जैसे int, float, double, char तथा string के लिए बिना दोबारा Code लिखे कार्य कर सके।
यदि किसी Program में एक ही Logic को विभिन्न Data Types पर लागू करना हो, तो प्रत्येक Data Type के लिए अलग-अलग Function अथवा Class बनाने की आवश्यकता नहीं होती। इसके स्थान पर Template का उपयोग किया जाता है, जिससे Code Reusability, Maintainability तथा Efficiency बढ़ जाती है।
RGPV B.Tech First Year के Basic Computer Engineering Subject में Templates in C++ एक महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Function Template, Class Template, Generic Programming तथा Template Programs से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।
↓
Provide Different Data Types
↓
Compiler Generates Required Code
↓
Reusable Program
Introduction to Templates
C++ में Templates का उपयोग Generic Functions तथा Generic Classes बनाने के लिए किया जाता है। Template एक Blueprint की तरह कार्य करता है। जब किसी विशेष Data Type के साथ Template का उपयोग किया जाता है, तब Compiler उसी Data Type के अनुसार आवश्यक Code स्वतः Generate कर देता है।
Templates का मुख्य उद्देश्य Program में बार-बार समान Code लिखने की आवश्यकता को समाप्त करना तथा Software को अधिक Flexible एवं Reusable बनाना है।
What are Templates?
Template C++ का ऐसा Mechanism है जिसकी सहायता से एक ही Function अथवा Class को विभिन्न Data Types के साथ उपयोग किया जा सकता है। Compiler Template को Compile Time पर आवश्यक Data Type के अनुसार अलग-अलग Version में Convert कर देता है।
ऊपर दिए गए उदाहरण में T एक Template Parameter है। Compiler Program में उपयोग किए गए Data Type के अनुसार T को Replace कर देता है।
Need of Templates
जब किसी Program में समान Logic को अनेक Data Types पर लागू करना हो, तब Templates का उपयोग किया जाता है।
- Code Duplication को कम करने के लिए।
- Generic Programming लागू करने के लिए।
- Code Reusability बढ़ाने के लिए।
- Development Time कम करने के लिए।
- Maintainable Software विकसित करने के लिए।
- Type Safety बनाए रखने के लिए।
- Large Scale Applications में Common Logic साझा करने के लिए।
Advantages of Templates
| Advantage | Description |
|---|---|
| Code Reusability | एक ही Code अनेक Data Types के लिए उपयोग किया जा सकता है। |
| Generic Programming | Flexible एवं Generic Software Design संभव होता है। |
| Less Code | बार-बार समान Function अथवा Class लिखने की आवश्यकता नहीं होती। |
| Type Safety | Compiler Compile Time पर Type Checking करता है। |
| Easy Maintenance | एक स्थान पर परिवर्तन करने से सभी Data Types के लिए प्रभाव पड़ता है। |
Generic Programming
Generic Programming ऐसी Programming Technique है जिसमें एक ही Program Logic को विभिन्न Data Types के साथ उपयोग किया जा सकता है। C++ में Generic Programming मुख्य रूप से Templates द्वारा Implement की जाती है। इससे Program अधिक Flexible, Reusable तथा Efficient बन जाता है।
यदि किसी Function का Logic सभी Data Types के लिए समान हो, तो अलग-अलग Functions बनाने के स्थान पर Template का उपयोग करना अधिक उपयुक्त होता है।
↓
Pass Different Data Types
↓
Compiler Generates Required Functions
↓
Reusable Program
Function Template
Function Template ऐसा Generic Function होता है जिसे विभिन्न Data Types के साथ उपयोग किया जा सकता है। Compiler आवश्यकता अनुसार प्रत्येक Data Type के लिए Function का अलग Version तैयार कर देता है।
Function Template का उपयोग तब किया जाता है जब Function का Logic समान हो लेकिन Input Data Types अलग-अलग हों।
Syntax of Function Template
ऊपर दिए गए उदाहरण में T Template Parameter है। Program में int, float अथवा double Data Type उपयोग करने पर Compiler उसी प्रकार का Function स्वतः Generate करता है।
Function Template Example
Output
ऊपर दिए गए Program में एक ही add() Function Template का उपयोग Integer तथा Floating Point दोनों प्रकार के Data पर किया गया है।
Template Parameters
Template में उपयोग होने वाले Placeholder को Template Parameter कहा जाता है। सामान्यतः T, U, Type आदि नामों का उपयोग किया जाता है।
यहाँ T किसी भी Valid Data Type जैसे int, float, double अथवा char को Represent कर सकता है।
Multiple Template Parameters
यदि Function अथवा Class में एक से अधिक Data Types की आवश्यकता हो, तो Multiple Template Parameters का उपयोग किया जाता है।
ऊपर दिए गए उदाहरण में T तथा U दो अलग-अलग Template Parameters हैं। इनके माध्यम से Function विभिन्न प्रकार के Data Types को एक साथ Handle कर सकता है।
Important Points
- Generic Programming Templates द्वारा संभव होती है।
- Function Template एक ही Logic को अनेक Data Types पर लागू करता है।
- template <class T> तथा template <typename T> दोनों का उपयोग किया जा सकता है।
- Compiler प्रत्येक Data Type के लिए अलग Function Generate करता है।
- Multiple Template Parameters द्वारा विभिन्न Data Types एक साथ उपयोग किए जा सकते हैं।
- Templates Compile Time पर Type Checking करते हैं।
- RGPV Practical एवं Viva में Function Template तथा Generic Programming अत्यंत महत्वपूर्ण Topics हैं।
Class Template
Class Template C++ का ऐसा Generic Class होता है जिसे विभिन्न Data Types के साथ उपयोग किया जा सकता है। यदि किसी Class का Logic सभी Data Types के लिए समान हो, तो अलग-अलग Classes बनाने के स्थान पर Class Template बनाया जाता है।
Compiler आवश्यकता अनुसार प्रत्येक Data Type के लिए उस Class का अलग Version स्वतः Generate कर देता है। इससे Code Reusability तथा Maintainability दोनों बढ़ती हैं।
Syntax of Class Template
ऊपर दिए गए उदाहरण में T Template Parameter है। Class का Object बनाते समय Compiler वास्तविक Data Type के अनुसार Class तैयार करता है।
Class Template Example
Output
ऊपर दिए गए Program में एक ही Number Class Template का उपयोग int तथा float दोनों Data Types के साथ किया गया है।
Working of Templates
जब Programmer किसी Template का उपयोग किसी विशेष Data Type के साथ करता है, तब Compiler Compile Time पर उस Data Type के अनुसार वास्तविक Function अथवा Class Generate करता है। इस प्रक्रिया को Template Instantiation कहा जाता है।
↓
Pass Data Type
↓
Compiler Instantiates Template
↓
Generates Required Function / Class
↓
Program Executes
Templates vs Function Overloading
| Templates | Function Overloading |
|---|---|
| एक Generic Function लिखा जाता है। | प्रत्येक Data Type के लिए अलग Function लिखा जाता है। |
| Code Reusability अधिक होती है। | Code Duplication की संभावना अधिक होती है। |
| Compiler आवश्यकता अनुसार Functions Generate करता है। | सभी Functions Programmer द्वारा लिखे जाते हैं। |
| Large Projects के लिए अधिक उपयुक्त। | छोटे Programs के लिए उपयुक्त। |
Templates vs Macros
| Templates | Macros |
|---|---|
| Compile Time Type Checking होती है। | Type Checking नहीं होती। |
| Type Safe होते हैं। | Type Safe नहीं होते। |
| Compiler द्वारा Process किए जाते हैं। | Preprocessor द्वारा Expand किए जाते हैं। |
| Modern C++ Programming में अधिक उपयोग किए जाते हैं। | पुराने Programs में अधिक उपयोग होते थे। |
Important Points
- Class Template Generic Class बनाने के लिए उपयोग किया जाता है।
- Compiler Compile Time पर Template Instantiation करता है।
- Templates Function Overloading की आवश्यकता को कम करते हैं।
- Templates Macros की तुलना में अधिक सुरक्षित (Type Safe) होते हैं।
- Templates Generic Programming का आधार हैं।
- Templates Compile Time पर आवश्यक Code Generate करते हैं।
- RGPV Practical एवं Viva में Function Template तथा Class Template आधारित Programs अत्यंत महत्वपूर्ण हैं।
Advantages of Templates
Templates C++ Programming में Code Reusability तथा Generic Programming को बढ़ावा देते हैं। इनके उपयोग से Programmer एक ही Function अथवा Class को अनेक Data Types के साथ उपयोग कर सकता है, जिससे Program अधिक Efficient तथा Maintainable बनता है।
- Code Reusability में वृद्धि होती है।
- Code Duplication कम हो जाता है।
- Generic Programming को Support करता है।
- Compile Time Type Checking उपलब्ध होती है।
- Program अधिक Flexible एवं Maintainable बनता है।
- Development Time कम हो जाता है।
- Large Scale Software Projects के लिए अत्यंत उपयोगी है।
Limitations of Templates
यद्यपि Templates अत्यंत उपयोगी हैं, फिर भी इनके उपयोग से कुछ सीमाएँ (Limitations) उत्पन्न हो सकती हैं।
- Compile Time बढ़ सकता है।
- Template Errors समझना Beginners के लिए कठिन हो सकता है।
- प्रत्येक Data Type के लिए अलग Code Generate होने से Executable Size बढ़ सकता है।
- Complex Templates Program को पढ़ने में कठिन बना सकते हैं।
- Debugging सामान्य Functions की तुलना में कठिन हो सकती है।
- अनावश्यक Templates Program की Complexity बढ़ा सकते हैं।
Applications of Templates
| Application | Purpose |
|---|---|
| Standard Template Library (STL) | Vector, List, Queue, Stack तथा Map जैसी Generic Containers बनाना। |
| Mathematical Libraries | विभिन्न Numeric Data Types पर समान Algorithms लागू करना। |
| Database Applications | Generic Data Processing Functions विकसित करना। |
| Scientific Software | Floating Point एवं Integer Data के लिए समान Logic उपयोग करना। |
| Game Development | Generic Data Structures तथा Algorithms विकसित करना। |
| Enterprise Applications | Reusable Business Components तैयार करना। |
Best Practices
- जहाँ समान Logic विभिन्न Data Types पर लागू हो, वहाँ Templates का उपयोग करें।
- Template Parameters के लिए Meaningful Names रखें।
- अनावश्यक Complex Templates बनाने से बचें।
- Templates के साथ Proper Documentation लिखें।
- Type Safety बनाए रखने के लिए Templates का सही उपयोग करें।
- Reusable तथा Maintainable Generic Components विकसित करें।
- Modern C++ Programming में STL Templates का अधिकतम उपयोग करें।
Real Life Example
मान लीजिए किसी Bank Management System में Deposit, Withdrawal तथा Balance Calculation जैसे Operations अलग-अलग प्रकार के Numeric Data पर कार्य करते हैं। यदि प्रत्येक Data Type के लिए अलग-अलग Functions बनाए जाएँ तो Code बहुत बड़ा हो जाएगा। इसके स्थान पर Function Template का उपयोग करके एक ही Generic Function बनाया जा सकता है जो int, float, double आदि सभी Data Types के साथ कार्य करेगा। इसी प्रकार Customer Records, Transaction Objects तथा Reports के लिए Class Templates का उपयोग करके Generic Software Design तैयार किया जा सकता है।
Working of Templates
↓
Specify Required Data Type
↓
Compiler Performs Template Instantiation
↓
Creates Required Function / Class
↓
Compile Program
↓
Execute Generic Code
Applications of Templates
Templates आधुनिक C++ Programming का महत्वपूर्ण भाग हैं। इनका उपयोग उन Applications में किया जाता है जहाँ एक ही Logic को विभिन्न Data Types पर लागू करना होता है। Generic Programming तथा Standard Template Library (STL) पूरी तरह Templates पर आधारित हैं।
| Application | Purpose |
|---|---|
| Standard Template Library (STL) | Vector, List, Stack, Queue, Map तथा Set जैसे Generic Containers बनाना। |
| Mathematical Libraries | विभिन्न Numeric Data Types पर समान Mathematical Algorithms लागू करना। |
| Database Management Systems | Generic Data Processing तथा Searching Algorithms विकसित करना। |
| Scientific Applications | Scientific Calculations के लिए Generic Functions एवं Classes बनाना। |
| Game Development | Reusable Game Objects एवं Generic Data Structures तैयार करना। |
| Enterprise Software | Business Logic के लिए Reusable Generic Components विकसित करना। |
| Artificial Intelligence Applications | Generic Algorithms एवं Data Containers का उपयोग करना। |
Advantages of Templates
- एक ही Code अनेक Data Types के लिए उपयोग किया जा सकता है।
- Code Duplication में उल्लेखनीय कमी आती है।
- Compile Time Type Checking उपलब्ध होती है।
- Generic Programming को सरल बनाता है।
- Software Development Time कम हो जाता है।
- Code अधिक Reusable तथा Maintainable बनता है।
- Modern C++ Libraries के निर्माण का आधार है।
Limitations of Templates
- Compile Time अपेक्षाकृत अधिक हो सकता है।
- Template Errors कई बार बहुत बड़े एवं कठिन होते हैं।
- प्रत्येक Data Type के लिए अलग Code Generate होने से Program Size बढ़ सकता है।
- Complex Template Syntax Beginners के लिए कठिन हो सकती है।
- Debugging सामान्य Functions की तुलना में अधिक चुनौतीपूर्ण हो सकती है।
- अनावश्यक Template Usage Program की Complexity बढ़ा सकता है।
- Template Specialization समझने के लिए अतिरिक्त अध्ययन की आवश्यकता होती है।
Best Practices
- जहाँ समान Logic विभिन्न Data Types पर लागू हो, वहीं Templates का उपयोग करें।
- Template Parameters के लिए Meaningful Names का उपयोग करें।
- Templates को सरल एवं Readable रखें।
- अनावश्यक Nested Templates से बचें।
- Reusable Generic Components विकसित करें।
- STL Containers एवं Algorithms का अधिकतम उपयोग करें।
- Proper Documentation एवं Comments लिखें ताकि Template Code समझना आसान हो।
Real Life Example
मान लीजिए किसी University Management System में Student IDs, Employee IDs तथा Library IDs अलग-अलग Data Types में Store किए जा सकते हैं। यदि प्रत्येक Data Type के लिए अलग-अलग Search एवं Display Functions बनाए जाएँ तो Code बहुत बड़ा हो जाएगा। इसके स्थान पर Function Templates एवं Class Templates का उपयोग करके एक ही Generic Program तैयार किया जा सकता है, जो सभी प्रकार के Data Types के साथ समान रूप से कार्य करेगा। इससे Software अधिक Maintainable तथा Efficient बनता है।
Working of Templates
↓
Specify Template Parameter
↓
Pass Required Data Type
↓
Compiler Generates Specialized Code
↓
Compile Program
↓
Execute Required Function / Class
Summary
Templates C++ की एक महत्वपूर्ण विशेषता है जो Generic Programming को संभव बनाती है। Templates की सहायता से Programmer एक ही Function अथवा Class को विभिन्न Data Types के साथ उपयोग कर सकता है। Compiler Compile Time पर आवश्यक Data Type के अनुसार Function अथवा Class का नया Version स्वतः Generate करता है, जिसे Template Instantiation कहा जाता है। C++ में मुख्य रूप से दो प्रकार के Templates होते हैं—Function Template तथा Class Template। Templates Code Reusability, Type Safety, Maintainability तथा Development Speed को बढ़ाते हैं और आधुनिक C++ Libraries जैसे Standard Template Library (STL) का आधार हैं।
| Concept | Description |
|---|---|
| Template | Generic Function अथवा Generic Class बनाने की तकनीक। |
| Function Template | एक Function अनेक Data Types के लिए उपयोग किया जाता है। |
| Class Template | एक Class अनेक Data Types के लिए उपयोग की जाती है। |
| Generic Programming | एक ही Logic को विभिन्न Data Types पर लागू करना। |
| Template Parameter | Placeholder जो Compile Time पर वास्तविक Data Type से Replace होता है। |
| Template Instantiation | Compiler द्वारा आवश्यक Function अथवा Class Generate करना। |
| Major Benefit | Code Reusability एवं Type Safety। |
| Applications | STL, Scientific Software, Database Systems, AI तथा Enterprise Applications। |
Important Viva Questions
- Template क्या है?
- Generic Programming क्या है?
- Function Template क्या होता है?
- Class Template क्या होता है?
- Template Parameter क्या है?
- Template Instantiation क्या है?
- Templates तथा Function Overloading में क्या अंतर है?
- Templates तथा Macros में क्या अंतर है?
- Templates के Advantages क्या हैं?
- STL में Templates का क्या महत्व है?
University Examination Important Questions
- Templates क्या हैं? उदाहरण सहित समझाइए।
- Function Template का Program लिखिए।
- Class Template का Program उदाहरण सहित लिखिए।
- Generic Programming क्या है? विस्तार से समझाइए।
- Templates तथा Function Overloading में अंतर स्पष्ट कीजिए।
- Templates तथा Macros की तुलना कीजिए।
- Multiple Template Parameters को उदाहरण सहित समझाइए।
- Templates के Advantages एवं Applications लिखिए।
- Template Instantiation क्या है? समझाइए।
- Modern C++ Programming में Templates का महत्व स्पष्ट कीजिए।
Exam Tips
- हमेशा याद रखें कि Templates Compile Time पर कार्य करते हैं।
- Function Template तथा Class Template के Syntax का नियमित अभ्यास करें।
- template <class T> तथा template <typename T> दोनों Valid Syntax हैं।
- Templates तथा Function Overloading का Comparison परीक्षा के लिए अत्यंत महत्वपूर्ण है।
- Templates तथा Macros के अंतर को Table के रूप में याद करें।
- STL पूरी तरह Templates पर आधारित है, यह तथ्य Viva में अक्सर पूछा जाता है।
- RGPV Practical एवं Viva में Function Template तथा Class Template आधारित Programs अत्यंत महत्वपूर्ण हैं।
Conclusion
Templates C++ Programming को अधिक शक्तिशाली, Flexible तथा Reusable बनाते हैं। इनके माध्यम से एक ही Function अथवा Class को विभिन्न Data Types के साथ उपयोग किया जा सकता है, जिससे Code Duplication कम होती है तथा Development अधिक Efficient बनता है। Function Templates, Class Templates, Generic Programming तथा Template Instantiation आधुनिक C++ Software Development के आधारभूत Concepts हैं। Standard Template Library (STL), Scientific Computing, Database Management Systems, Artificial Intelligence तथा Enterprise Applications में Templates का व्यापक उपयोग किया जाता है। RGPV B.Tech First Year के विद्यार्थियों के लिए यह Topic Theory, Practical तथा Viva तीनों दृष्टिकोण से अत्यंत महत्वपूर्ण है।
Related Articles
Standard Template Library (STL) in C++ Notes | Containers, Iterators & Algorithms | Basic Computer Engineering | RGPV BTech First Year
Standar...
Read More →Exception Handling in C++ Notes | try, throw, catch & Exception Handling Mechanism | Basic Computer Engineering | RGPV BTech First Year
Excepti...
Read More →Virtual Functions in C++ Notes | Virtual Function, Pure Virtual Function & Dynamic Binding | Basic Computer Engineering | RGPV BTech First Year
Virtual...
Read More →Function Overriding in C++ Notes | Method Overriding, Runtime Polymorphism & Examples | Basic Computer Engineering | RGPV BTech First Year
Functio...
Read More →Constructors and Destructors in Inheritance Notes | Constructor & Destructor Calling Order in C++ | Basic Computer Engineering | RGPV BTech First Year
Constru...
Read More →