Function Template and Class Template in C++ – Complete Tutorial
Last Updated on: 22nd Dec 2025 13:40:45 PM
In C++, Templates allow you to write generic programs—programs that work with any data type without rewriting code.
Instead of writing multiple versions of the same function or class for int, float, double, etc., templates let you write once and use for all.
👉 Templates support code reusability, type safety, and clean design.
What is a Template?
Concept Explanation
A template is a blueprint that allows creating functions or classes that can work with different data types without rewriting code.
Definition
A template is a C++ feature that enables generic programming by defining functions or classes with placeholders for data types.
Real-Life Example of Templates
Example: Universal Remote Control
-
One remote
-
Works with TV, AC, Sound System
-
Same buttons, different devices
👉 Template = Universal logic
👉 Data type = Device
There are two main types of templates:
-
Function Template
-
Class Template
PART 1: FUNCTION TEMPLATE IN C++
1. What is a Function Template?
A Function Template is a blueprint that allows a function to work with different data types using a single definition.
2. Why Function Templates are Needed?
Without templates ❌:
int add(int a, int b);
float add(float a, float b);
double add(double a, double b);
With templates
template <typename T>
T add(T a, T b);
3. Syntax of Function Template
template <class T>
return_type function_name(T variable);
or
template <typename T>
👉 class and typename are interchangeable.
4. Simple Function Template Example
The same function performs addition for multiple data types.
Example
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
cout << "Integer Addition: " << add(10, 20) << endl;
cout << "Float Addition: " << add(10.5f, 20.3f) << endl;
cout << "Double Addition: " << add(5.25, 3.75) << endl;
return 0;
}
Output
Integer Addition: 30
Float Addition: 30.8
Double Addition: 9
Explanation of Output
-
Compiler generates different versions of
add() -
Same logic, different data types
-
Type is decided at compile time
5. Function Template with Multiple Data Types
Concept Explanation
Templates can accept more than one data type.
Example
#include <iostream>
using namespace std;
template <typename T1, typename T2>
void display(T1 a, T2 b) {
cout << "Value 1: " << a << endl;
cout << "Value 2: " << b << endl;
}
int main() {
display(10, 20.5);
display("Age", 25);
return 0;
}
Output
Value 1: 10
Value 2: 20.5
Value 1: Age
Value 2: 25
Explanation of Output
-
Template supports different parameter types
-
Function adapts automatically
6. Real-Life Use of Function Template
Example: Calculator App
-
Same operations (+, −, ×)
-
Works for int, float, double
-
No duplicate code
7. Advantages of Function Templates
✔ Code reusability
✔ Type safety
✔ Cleaner and shorter programs
✔ Compile-time efficiency
PART 2: CLASS TEMPLATE IN C++
8. What is a Class Template?
A Class Template allows you to define a class once and use it for multiple data types.
It is widely used in STL containers like vector, stack, queue.
9. Real-Life Example of Class Template
Example: Storage Box
-
One box design
-
Can store books, clothes, electronics
👉 Class Template = Box design
👉 Data type = Item stored
10. Syntax of Class Template
template <class T>
class ClassName {
T variable;
};
11. Simple Class Template Example
A class stores and displays a value of any data type.
Example
#include <iostream>
using namespace std;
template <class T>
class Data {
T value;
public:
Data(T v) {
value = v;
}
void show() {
cout << "Value: " << value << endl;
}
};
int main() {
Data<int> d1(100);
Data<float> d2(45.6f);
Data<string> d3("iKeySkills");
d1.show();
d2.show();
d3.show();
return 0;
}
Output
Value: 100
Value: 45.6
Value: iKeySkills
Explanation of Output
-
Same class used for different types
-
Memory allocated based on type
12. Class Template with Multiple Parameters
A class can accept multiple data types.
Example
#include <iostream>
using namespace std;
template <class T1, class T2>
class Pair {
T1 first;
T2 second;
public:
Pair(T1 a, T2 b) {
first = a;
second = b;
}
void display() {
cout << "First: " << first << endl;
cout << "Second: " << second << endl;
}
};
int main() {
Pair<int, float> p1(10, 20.5f);
Pair<string, int> p2("Age", 25);
p1.display();
p2.display();
return 0;
}
Output
First: 10
Second: 20.5
First: Age
Second: 25
Explanation of Output
-
Template class handles mixed types
-
Useful for key-value pairs
13. Function Template vs Class Template
| Function Template | Class Template |
|---|---|
| Generic function | Generic class |
| Used for operations | Used for data storage |
| Less memory usage | More flexible |
| Simple logic | Complex structures |
14. Template Specialization (Intro)
Concept Explanation
Template specialization allows custom behavior for a specific data type.
Example
template <>
class Data<char> {
public:
void show() {
cout << "Character type detected" << endl;
}
};
15. Common Mistakes
❌ Forgetting template keyword
❌ Type mismatch during object creation
❌ Overusing templates unnecessarily
16. Interview Questions
-
What are templates in C++?
-
Difference between function and class templates?
-
What is template specialization?
-
Are templates resolved at compile time?
-
Why STL uses templates?
17. Summary
-
Templates support generic programming
-
Function templates avoid code duplication
-
Class templates create flexible data structures
-
Used heavily in STL and modern C++
-
Improve scalability and maintainability
Keep practicing — you're doing amazing!
Happy Coding! ![]()