9 Templates And Generic Programming

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.

Templates and Generic Programming: notes

Template concept and importance

A template is a blueprint that lets the compiler generate functions or classes for different types. We write the logic once and use it with int, float, char and so on.

Importance:

  • It avoids writing the same code again for each type.
  • It keeps type safety, unlike macros.
  • It improves reuse and makes programs shorter.
  • Templates are resolved at compile time, so there is no run-time cost.

The syntax starts with template <class T> or template <typename T>. Here T is a type parameter.

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 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 template followed 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, T is a type parameter (placeholder for an actual data type like int, 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

AspectDetail
template <typename T>Declares T as a generic type parameter
T &a, T &bPass by reference so original values are modified
T tempTemporary variable of the same generic type
Compiler behaviorGenerates separate swapValues<int>, swapValues<float>, swapValues<char> automatically

Advantage: Without templates, we would need to write three separate swap functions for int, float, and char. The function template handles all types with a single definition.

Open this answer on its own page →

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]

A function template is a blueprint or generic function that allows a single function definition to work with different data types. Instead of writing separate functions for each data type, a function template uses a type parameter (placeholder) that is replaced by the actual data type at compile ...

Open this answer on its own page →

0 · 5 marks

Why concept of templates is important? Explain function templates with example. [5]

Templates are important in C++ for the following reasons: 1. Generic Programming: Templates allow writing generic (type-independent) code that works with any data type, avoiding code duplication. 2. Code Reusability: A single template function or class can be used for int, float, double, char, et...

Open this answer on its own page →

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]

A class template in C++ is a blueprint for creating a family of classes where the data type is parameterized. Instead of writing separate classes for different data types, a single class template can be defined and the compiler generates the appropriate class for the specified type at compile tim...

Open this answer on its own page →

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]

A template is a feature in C++ that allows writing generic programs -- functions or classes that work with any data type without rewriting the code for each type. 1. Code Reusability Without templates, we must write separate functions for each data type: With a template, one function handles all ...

Open this answer on its own page →