← Back to C++ Course | Chapter 11: Templates | Lesson 3 of 5

C++ के Class Templates

एक class template type के लिए एक blank वाला एक blueprint है, apples या oranges रखने के लिए बनाए जा सकने वाले एक box की तरह। आप इसे इस्तेमाल करते समय type भर देते हैं।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

Default Template Arguments

आप एक class template के type parameters को default types दे सकते हैं, जो callers को angle-bracket type argument पूरी तरह omit करने देता है और जब उन्हें कुछ ज़्यादा specific न चाहिए तो एक sensible default पर fall back होने देता है।

उदाहरण: Default Template Arguments

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक object बनाते समय type भूल जाना, जैसे Box<int> b; की बजाय Box b; (C++17 से पहले)।
  2. template <typename T> दोहराए बिना class के बाहर member functions define करना।
  3. class template member definitions को एक .cpp file में रखना, जो linker errors cause करता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.