A template in C++ allows you to write generic code that works with any data type. It helps in creating functions and classes that operate on different types without rewriting code.
1️⃣ Why Use Templates?
Code Reusability: Instead of writing multiple functions for different data types, you write one generic function/class.
Type Safety: Works for any data type while maintaining strong type checking.
Performance: The compiler generates type-specific versions of the template at compile-time.
2️⃣ Function Template
A function template allows a single function to work with different data types.
Syntax:
template <typename T>
return_type function_name(T parameter) {
// Function body
}
template <typename T>: Declares T as a generic type.
T is replaced with an actual type when the function is called.
Example: Function Template for Finding Maximum
#include <iostream>
using namespace std;
template <typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << "Max of 10 and 20: " << findMax(10, 20) << endl;
cout << "Max of 5.5 and 2.3: " << findMax(5.5, 2.3) << endl;
cout << "Max of 'A' and 'Z': " << findMax('A', 'Z') << endl;
return 0;
}
Output:
Max of 10 and 20: 20
Max of 5.5 and 2.3: 5.5
Max of 'A' and 'Z': Z
3️⃣ Class Template
A class template allows you to create a generic class that can handle multiple data types.
Syntax:
template <typename T>
class ClassName {
T variable;
public:
ClassName(T value) { variable = value; }
void show() { cout << "Value: " << variable << endl; }
};
Example: Class Template for a Generic Box
#include <iostream>
using namespace std;
template <typename T>
class Box {
private:
T value;
public:
Box(T v) { value = v; }
void display() { cout << "Stored Value: " << value << endl; }
};
int main() {
Box<int> intBox(100);
Box<double> doubleBox(12.34);
Box<string> stringBox("Hello");
intBox.display();
doubleBox.display();
stringBox.display();
return 0;
}
Output:
Stored Value: 100
Stored Value: 12.34
Stored Value: Hello
4️⃣ Template with Multiple Parameters
You can define multiple template parameters for a function or class.
Example: Function Template with Multiple Parameters
#include <iostream>
using namespace std;
template <typename T1, typename T2>
void display(T1 a, T2 b) {
cout << "Value 1: " << a << ", Value 2: " << b << endl;
}
int main() {
display(10, "Hello");
display(5.5, 100);
return 0;
}
Output:
Value 1: 10, Value 2: Hello
Value 1: 5.5, Value 2: 100
5️⃣ Specialization of Templates
If you want to provide a special implementation for a specific data type, you can use template specialization.
Example: Specializing a Class Template for char Type
#include <iostream>
using namespace std;
template <typename T>
class Data {
public:
Data(T value) { cout << "Generic Data: " << value << endl; }
};
// Specialization for char
template <>
class Data<char> {
public:
Data(char value) { cout << "Character Data: " << value << endl; }
};
int main() {
Data<int> d1(100);
Data<double> d2(12.34);
Data<char> d3('A'); // Calls specialized template
return 0;
}
Output:
Generic Data: 100
Generic Data: 12.34
Character Data: A
6️⃣ Advantages of Templates
✅ Code Reusability – Write a single function/class and use it for multiple data types.
✅ Type Safety – Ensures type correctness at compile time.
✅ Efficiency – Avoids code duplication and improves maintainability.
7️⃣ Key Takeaways
Function templates allow writing generic functions.
Class templates allow creating generic classes.
Multiple template parameters can be used.
Template specialization provides custom implementations for specific types.
Would you like to explore STL (Standard Template Library) or template metaprogramming next? 🚀
0 Comments