PPT11 Templates
PPT11 Templates
Templates in C++
1
Introduction
Templates in C++ allow you to write generic code that can work with any
data type. Instead of writing multiple versions of the same function or class
for different data types, templates let you create type-independent functions
and classes that automatically adapt to the type of data they work with.
• Templates come in two main forms:
• Function Templates: Used to create functions that can operate on any data type.
• Class Templates: Used to create classes that can store, manipulate, or process data of
any type.
Templates are a cornerstone of generic programming in C++, providing
flexibility, reusability, and type safety.
Need of Template
• To reduce code duplication when supporting numerous data types
int main(){
cout<<MaxElement (10,20)<<endl;
cout<<MaxElement ('a','z')<<endl; Calling Function Template
cout<<MaxElement (-2.5,7.7)<<endl;
}
Example:2
Example: Bubble Sort without Template
void bubbleSort(int a[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
Example: Bubble Sort with Template
template <class T>
void bubbleSort(T arr[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = n - 1; i < j; j--)
if (a[j] < a[j - 1])
swap(a[j], a[j - 1]);
}
Function Template with Multiple Parameters
Example: Write a template for
int main(){
show(100,"hello hello");
show('k',1500);
show(1.23,2987);
}
Example: Write a template for
template <class T1, class T2>
void show(T1 a, T2 b){
cout<<a<<“, “<<b<<endl; int main(){
} show(100,"hello hello");
show('k',1500);
show(1.23,2987);
}
Function template with non-type parameter
• Non-type parameter is not a type (datatype) but a
value e.g. 100
• They are used to initialize a class or to specify the sizes
of class members
int main(){
show(100,"hello hello");
show(3,3);
}
Usage of Template Argument
• No-argument template function
Template <class T>
T void (void) //error: T is not used as an argument
{
//…..
Return parameter;
}
• Template-type Argument Unused
Template <class T>
Void test (int x) //error: T is not used as an argument
{
//…..
Return parameter;
}
•Display<int> uses the general Display template, while Display<bool> uses the specialized version
specifically designed for bool.
•This feature is particularly useful when you need customized behavior for certain data
types, such as printing true/false instead of 1/0.
Partial Specialization (Class Templates Only)
Partial specialization is only available for class templates (not function
templates) and allows you to specialize a template for a subset of its
template parameters.
Partial Specialization (Class Templates Only)
Note:
Pair<int, double> uses the general Pair
template, while Pair<int, int> uses the
partially specialized version where both
template parameters are the same.
Variadic Templates
• Variadic templates allow a template to accept an arbitrary number of
template parameters. This is useful for functions like printf-style
functions or containers that handle multiple elements of varying
types.
Note:
•The first print function is the base case for
recursion.
return 0;
}
Example:02
int main() {
Derived<int, double> obj(10, 20.5);
obj.show(); // Call Base class function
obj.display(); // Call Derived class function
return 0;
}
Example:03
Example:04
Thank You