
Download this note as PDF at no cost
If any AD appears on download click please wait for 30sec till it gets completed and then close it, you will be redirected to pdf/ppt notes page.
A template allows us to write a function/class once and use it for different data types.
Why needed:
int, float, double, etc.Example idea: max(a,b) should work for int, float, double.
A function template is a generic function definition.
Basic syntax:
template <class T> or template <typename T>T func(T a, T b) { ... }When called with a specific type, compiler creates the real function.
T is a type parameter (placeholder).int, compiler instantiates a version for int.Important point: template code is generated at compile time.
Templates can be overloaded like normal functions. If a non-template function matches better, compiler may prefer it.
For Sem 1, keep idea simple: “Templates can be overloaded and compiler chooses best match.”
A class template is a generic class.
Example idea:
template<class T> class Box { T data; ... }When used as Box<int>, it becomes a class storing int.
Templates:
An exception is an abnormal situation that occurs during program execution (e.g., divide by zero, file open failure).
Exception handling is a mechanism to handle such errors gracefully without crashing the program.
This improves reliability and separates normal logic from error-handling logic.
Flow:
We can write multiple catch blocks for different exception types.
Catch-all:
catch(...) catches any type of exception.Order matters:
When an exception is thrown, C++ searches for a matching catch. If it moves out of functions, destructors for local objects are called automatically.
This process is called stack unwinding.
Common situations:
bad_alloc in advanced cases)In Sem 1, focus on understanding try/throw/catch and writing a small flow.
catch(...) before specific catch blocks.Normal code → try block
↓ exception occurs
throw exception → search matching catch → handle → continue/exit
catch(specific) → catch(less specific) → catch(...)
Get instant access to notes, practice questions, and more benefits with our mobile app.
A template allows us to write a function/class once and use it for different data types.
Why needed:
int, float, double, etc.Example idea: max(a,b) should work for int, float, double.
A function template is a generic function definition.
Basic syntax:
template <class T> or template <typename T>T func(T a, T b) { ... }When called with a specific type, compiler creates the real function.
T is a type parameter (placeholder).int, compiler instantiates a version for int.Important point: template code is generated at compile time.
Templates can be overloaded like normal functions. If a non-template function matches better, compiler may prefer it.
For Sem 1, keep idea simple: “Templates can be overloaded and compiler chooses best match.”
A class template is a generic class.
Example idea:
template<class T> class Box { T data; ... }When used as Box<int>, it becomes a class storing int.
Templates:
Access the complete note and unlock all topic-wise content
It's free and takes just 5 seconds
From this topic
Templates support generic programming by allowing the same function/class to work for different data types. Advantages (any three):
Hence templates are widely used in C++ libraries.
A function template is written using template<class T> and uses T as a placeholder type. When called with int/double, compiler generates the specific function (instantiation). Example idea: generic max(a,b).
A function template is a generic function definition that works for different data types.
Syntax:
template <class T> (or template <typename T>)T func(T a, T b) { ... }Working:
T acts as a type parameter.Advantage: reduces duplication and supports generic programming.