---
title: "Need of template; function template overloading"
subject: "Object Oriented Programming"
chapter: 9
key: "semester_3/object_oriented_programming:why-template-function-overloading"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/why-template-function-overloading
site: BCT Engineering
---

# Need of template; function template overloading

Object Oriented Programming · Chapter 9: Templates

- Asked in: 2074 Chaitra, 3+5 marks
- Video: [Overloading Function Templates | C++ Tutorial](https://www.youtube.com/watch?v=l0Tg_95OKWw) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/why-template-function-overloading
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-9

## Question

Explain why do we need template. Explain the function template overloading with suitable example.

## Short answer

Templates eliminate redundant overloads for same logic with different types, providing reusable, type-safe code. Function template overloading occurs with multiple templates differing in parameter count or a non-template function. The compiler chooses exact non-template first, then exact template instantiation. Example: maxOf(a,b) (two args) and maxOf(a,b,c) (three args), plus a specialized maxOf for const char* using strcmp to compare strings properly.

## Answer

#### Why we need templates
Many algorithms (swap, find largest, sort, stack, queue) are the same for every data type. Without templates we would have to:
- write many overloaded functions or classes that differ only in type, which wastes effort and is hard to maintain; or
- use macros or `void*`, which are not type safe.

**Templates** (generic programming) let us write the logic once with a type parameter, and the compiler generates code for each required type. They give reusability, type safety, shorter code and easier maintenance. The standard library containers (vector, list) are built with templates.

#### Function template overloading
A function template can be overloaded in two ways:
1. By another template with a **different number or types of parameters**.
2. By an ordinary (non-template) function with the same name.

The compiler picks the function by this order:
1. An exact-match non-template function.
2. A template that can be instantiated with an exact match.
3. Otherwise, a function needing conversion (non-template only).

```cpp
#include <iostream>
#include <cstring>
using namespace std;

template <class T>                 // template 1: two values
T maxOf(T a, T b) { return (a > b) ? a : b; }

template <class T>                 // template 2: three values (overload)
T maxOf(T a, T b, T c) { return maxOf(maxOf(a, b), c); }

const char* maxOf(const char* a, const char* b) {   // ordinary overload
    return (strcmp(a, b) > 0) ? a : b;
}

int main() {
    cout << maxOf(3, 8) << endl;            // template 1, int  -> 8
    cout << maxOf(2.5, 1.5, 9.1) << endl;   // template 2, double -> 9.1
    cout << maxOf("apple", "mango") << endl;// ordinary function -> mango
    return 0;
}
```

Without the ordinary overload, comparing two C-strings with the template would compare their addresses, not their text; so overloading a template with a specific function handles special cases.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why do we need templates?
   A. To stop using functions
   B. To write logic once with a type parameter instead of repeating it for every type
   C. To make all variables global
   D. To avoid compiling the code
   Answer: B. Many algorithms are identical for every data type; templates remove the repetition. (Key point: Why templates)

2. In which ways can a function template be overloaded?
   A. Only by changing the return type
   B. Only by using macros
   C. By another template with different parameters or by an ordinary function of the same name
   D. Templates cannot be overloaded
   Answer: C. A template can be overloaded by another template with different parameter lists or by a non-template function. (Key point: Ways to overload)

3. Given an exact-match ordinary function and an exact-match template, which does the compiler pick?
   A. The template
   B. It is an error always
   C. Whichever is declared last
   D. The ordinary non-template function
   Answer: D. An exact-match non-template function has the highest priority. (Key point: Selection order)

4. Why is an ordinary overload of maxOf for const char* provided?
   A. The template would compare addresses, not the text
   B. Templates cannot take pointers
   C. strcmp is slower in templates
   D. To avoid using cout
   Answer: A. With the template, comparing two C-strings compares pointers, so a special overload uses strcmp. (Key point: Why ordinary overload for strings)

## Related in Object Oriented Programming

- [Operator overloading: need and definition](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-why-define) — Object Oriented Programming, chapter 5, asked 3×
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Importance of function template](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-importance) — Object Oriented Programming, chapter 9, asked 2×
- [Function template power(base, exp)](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apower-function-template) — Object Oriented Programming, chapter 9
- [Default arguments and function overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-args-function-overloading) — Object Oriented Programming, chapter 3, asked 5×

## Related in other subjects

- [Why do we need DFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-dft) — Digital Signal Analysis and Processing, chapter 7, asked 6×
- [Why do we need FFT](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-need-fft) — Digital Signal Analysis and Processing, chapter 7, asked 2×
- [Need for difference equation; LCCDE and system function](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Awhy-difference-equation-lccde) — Digital Signal Analysis and Processing, chapter 1, asked 2×
- [Firewall, its need and types](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Afirewall-types-working) — Computer Networks and Security, chapter 8, asked 7×
