Object Oriented Programming · Unit 9
Templates and Generic Programming
Exam-focused notes for Templates and Generic Programming (Object Oriented Programming, BIT153): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.
Templates and Generic Programming in short
Templates let us write one piece of code that works for many data types. They are the base of generic programming in C++ and of the Standard Template Library.
What this unit covers
- Template concept and importance
- Function templates
- Class templates
- Template instantiation
- Generic programming with templates
Function templates
2082 · 5 marks
What are function templates? Also, write a program that swaps two values using function templates. [1+4]
Show solved answerHide answer
Function Templates
What are Function Templates?
A function template is a blueprint or formula for creating a family of related functions. It allows a single function definition to work with different data types, where the actual data type is determined at compile time when the function is called.
Key Points:
- Defined using the keyword
templatefollowed by template parameter list - Enables generic programming in C++
- The compiler automatically generates the appropriate function for each data type used
- Avoids code duplication for functions that perform the same operation on different types
Syntax:
template <typename T>
return_type function_name(T param1, T param2, ...) {
// function body
}
Here,
Tis a type parameter (placeholder for an actual data type likeint,float,char, etc.)
Program: Swapping Two Values Using Function Templates
#include <iostream>
using namespace std;
// Function template for swapping two values
template <typename T>
void swapValues(T &a, T &b) {
T temp;
temp = a;
a = b;
b = temp;
}
int main() {
// Swapping two integers
int x = 10, y = 20;
cout << "Before swap (int): x = " << x << ", y = " << y << endl;
swapValues(x, y);
cout << "After swap (int): x = " << x << ", y = " << y << endl;
cout << endl;
// Swapping two floats
float p = 3.14, q = 7.77;
cout << "Before swap (float): p = " << p << ", q = " << q << endl;
swapValues(p, q);
cout << "After swap (float): p = " << p << ", q = " << q << endl;
cout << endl;
// Swapping two characters
char c1 = 'A', c2 = 'Z';
cout << "Before swap (char): c1 = " << c1 << ", c2 = " << c2 << endl;
swapValues(c1, c2);
cout << "After swap (char): c1 = " << c1 << ", c2 = " << c2 << endl;
return 0;
}
Output
Before swap (int): x = 10, y = 20
After swap (int): x = 20, y = 10
Before swap (float): p = 3.14, q = 7.77
After swap (float): p = 7.77, q = 3.14
Before swap (char): c1 = A, c2 = Z
After swap (char): c1 = Z, c2 = A
Explanation
| Aspect | Detail |
|---|---|
template <typename T> | Declares T as a generic type parameter |
T &a, T &b | Pass by reference so original values are modified |
T temp | Temporary variable of the same generic type |
| Compiler behavior | Generates separate swapValues<int>, swapValues<float>, swapValues<char> automatically |
Advantage: Without templates, we would need to write three separate
swapfunctions forint,float, andchar. The function template handles all types with a single definition.
2080.1 · 5 marks
What is function template? Write a program to demonstrate the use of function template that returns the greatest among integers, and float passed to it. [1+4]
0 · 5 marks
Why concept of templates is important? Explain function templates with example. [5]
Class templates
2080 · 5 marks
What do you mean by class template in C++? Write a program in C++ containing function template which determines the greater number between two integer inputs and two floating point inputs. [5]
Template concept and importance
2079 · 10 marks
What do we need template? Why the program terminate after the occurrence of exception?Describe the types of constructor.[4+6]
Make Unit 9 stick
Practice BIT153 with flashcards & quizzes