C++ के Class Templates
In this page:
template <typename T>
class ClassName {
public:
T member;
T method_name(T param) { return param; }
};
ClassName<type> object_name;
Class Templates Define करना
एक class template एक generic class blueprint define करता है, आपको एक single data structure बनाने देते हुए — एक stack, एक container, एक wrapper — जो एक specific type तक locked होने की बजाय किसी भी data type के elements manage कर सके।
उदाहरण: Defining Class Templates
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
T value;
Box(T v) : value(v) {}
};
// Program execution starts in main()
int main() {
Box<int> b(5);
// Print to the console with cout
std::cout << b.value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Class Templates Instantiate करना
एक class template instantiate करने के लिए, आपको एक object बनाते समय angle brackets के अंदर explicitly data type specify करना ज़रूरी है, क्योंकि function templates के उलट, compiler आमतौर पर constructor arguments से अकेले एक class template के type parameters infer नहीं कर सकता।
उदाहरण: Instantiating Class Templates
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
T value;
Box(T v) : value(v) {}
};
// Program execution starts in main()
int main() {
Box<double> b(3.14);
// Print to the console with cout
std::cout << b.value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Body के बाहर Member Functions Define करना
एक class template का member function class body के बाहर define करते समय, आपको function definition से पहले एक prefix की तरह पूरी template parameter list दोहरानी होगी, क्योंकि compiler को जब भी यह definition सामने आए तब वह context चाहिए।
उदाहरण: Defining Member Functions Outside the Body
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T>
// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
T value;
Box(T v) : value(v) {}
T getValue();
};
// Declare a template so this works with any suitable type
template <typename T>
T Box<T>::getValue() {
// Send value back to the caller
return value;
}
// Program execution starts in main()
int main() {
Box<int> b(10);
// Print to the console with cout
std::cout << b.getValue() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Default Template Arguments
आप एक class template के type parameters को default types दे सकते हैं, जो callers को angle-bracket type argument पूरी तरह omit करने देता है और जब उन्हें कुछ ज़्यादा specific न चाहिए तो एक sensible default पर fall back होने देता है।
उदाहरण: Default Template Arguments
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T = int>
// Define the type Box
class Box {
// Members below can be accessed from outside the class
public:
T value;
Box(T v) : value(v) {}
};
// Program execution starts in main()
int main() {
Box<> b(5);
// Print to the console with cout
std::cout << b.value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Classes में Non-Type Parameters
Class templates non-type parameters भी accept करते हैं, जैसे integers, जो यह है कि compile-time-sized array class जैसी fixed-size structures अपनी capacity को इसे ordinary runtime data की तरह store करने की बजाय सीधे type में bake कैसे कर सकती हैं।
उदाहरण: Non-Type Parameters in Classes
// Include std::cout and std::cin
#include <iostream>
// Declare a template so this works with any suitable type
template <typename T, int Size>
// Define the type FixedArray
class FixedArray {
// Members below can be accessed from outside the class
public:
T data[Size];
};
// Program execution starts in main()
int main() {
FixedArray<int, 3> arr;
arr.data[0] = 10;
// Print to the console with cout
std::cout << arr.data[0] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक object बनाते समय type भूल जाना, जैसे
Box<int> b;की बजायBox b;(C++17 से पहले)। template <typename T>दोहराए बिना class के बाहर member functions define करना।- class template member definitions को एक
.cppfile में रखना, जो linker errors cause करता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: