Imagine you want to create a function that adds two numbers. You might write one function for int:
int add(int a, int b)
But what if you also want to add double values? You would need another function:
double add(double a, double b)
Writing the same function again and again is not very useful. Templates solve this problem. A template allows you to write code that can work with different data types. Let’s understand C++ Templates.
What are C++ Templates?
A template is like a general design that C++ can use with different data types. For example, one function template can work with int, double, and other types.
One Template
↓
Different Data Types
C++ mainly uses two types of templates:
- Function templates
- Class templates
C++ Function Templates
A function template allows one function to work with different data types. Let’s start with a simple example.
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << add(10, 20) << "\n";
cout << add(2.5, 3.5);
return 0;
}
Output:
30
6
The same add() function works with both integers and decimal numbers.
How Does It Work?
Look at this line:
template <typename T>
Here, T is a placeholder for a data type.
When we write:
add(10, 20)
C++ understands that T should be int.
When we write:
add(2.5, 3.5)
C++ understands that T should be double. You can think of T as:
“C++, choose the data type for me.”
Another C++ Function Template Example
Let’s create a function that displays a value.
#include <iostream>
using namespace std;
template <typename T>
void display(T value) {
cout << value << "\n";
}
int main() {
display(10);
display(5.5);
display("Hello");
return 0;
}
Output:
10
5.5
Hello
The same function works with different types.
Class Templates in C++
Templates can also be used with classes.
A class template allows a class to work with different data types.
For example:
#include <iostream>
using namespace std;
template <typename T>
class Box {
public:
T value;
Box(T v) {
value = v;
}
void show() {
cout << "Value: " << value;
}
};
int main() {
Box<int> box1(100);
Box<double> box2(5.5);
box1.show();
cout << "\n";
box2.show();
return 0;
}
Output:
Value: 100
Value: 5.5
Here:
Box<int>
creates a Box that stores an integer.
And:
Box<double>
creates a Box that stores a decimal value.
Multiple Template Parameters
A template can also use more than one data type.
For example:
#include <iostream>
using namespace std;
template <typename T, typename U>
class Pair {
public:
T first;
U second;
Pair(T a, U b) {
first = a;
second = b;
}
void show() {
cout << first << " - " << second;
}
};
int main() {
Pair<string, int> student("Rahul", 14);
student.show();
return 0;
}
Output:
Rahul - 14
Here, the class uses two different types:
T → string
U → int
So:
Pair<string, int>
can store a string and an integer together.
Why Use Templates in C++?
Templates are useful because they help us:
- Reuse the same code
- Work with different data types
- Avoid writing similar functions or classes again
- Make programs more flexible
Templates are used a lot in the C++ Standard Template Library (STL), which we will learn later.
A Simple Way to Remember
Think of a template like a cookie cutter. The cutter stays the same, but you can use it to make different cookies. Similarly, the template stays the same, but it can work with different data types.
Template
↓
int
double
string
etc.
Key Points to Remember
- A template lets you write reusable code.
- Function templates can work with different data types.
- Class templates allow classes to work with different data types.
typename Tcreates a type placeholder.- You can use multiple template parameters.
- Templates reduce repeated code.
- STL uses templates heavily.
Mini Quiz
Mini Project
Mini Project: Generic Storage Box
Question:
Write a C++ program that uses a class template to create a storage box that can store different types of values.
- Create a class template named
Box. - Use a template parameter to allow the box to store different data types.
- Create one box for an
intvalue. - Create another box for a
doublevalue. - Ask the user to enter both values.
- Add a
show()function to display the stored value. - Display both values on the screen.
View Answer Code
#include <iostream>
using namespace std;
template <typename T>
class Box {
private:
T value;
public:
Box(T v) {
value = v;
}
void show() {
cout << "Stored value: " << value << "\n";
}
};
int main() {
int number;
double price;
cout << "Enter an integer: ";
cin >> number;
cout << "Enter a decimal value: ";
cin >> price;
Box<int> box1(number);
Box<double> box2(price);
box1.show();
box2.show();
return 0;
}
What’s Next?
In the next chapter, we will learn about C++ Exception Handling and see how to handle errors without suddenly stopping our program.
Frequently Asked Questions (FAQs)
Q1. What are templates in C++?
Templates in C++ allow you to write reusable code that can work with different data types.
Q2. What is a C++ function template?
A function template allows the same function to work with different data types without writing separate functions for each type.
Q3. What is a class template in C++?
A class template allows a class to work with different data types using a type placeholder such as T.
Q4. What are C++ template parameters?
Template parameters are placeholders that represent data types or other values used by a template. For example, T and U can represent different data types.
Q5. Why use templates in C++?
Templates help reduce repeated code, support different data types, and make functions and classes more reusable and flexible.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
