Templates allow functions and classes to work with multiple data types. They define generic pieces of code that can be specialized for specific types later. There are two main types of templates - class templates, which define classes that can work on different types, and function templates, which define functions operating on generic types. Templates provide benefits like code reusability, flexibility, and time savings in large programs.
1. SESSION โ (2022-23)
A Seminar
on
โTemplatesโ
Oopโs concept in c++
Guided by : Presented by :
Md. Afroj Ansari Sanju Sanjeev Toppo
(Ass. Professor of cs) Msc.cs II-semester
Roll no : 09
Date : 18/05/2023
2. โข Template allow the function or class to work on more
than one data type at once without writing different
codes for different data types.
โข The parameters used during its definition is of generic
type and can be replaced later by actual parameter.
โข This is called the concept of generic programming
TEMPLATES
3. โข Used in large programs
โข Code reusability
โข Time saving
โข Flexibility of program
Purpose of Templates
5. โข A Class Template can represent various similar
classes operating on different data types.
Syntax:
template < class T1, class T2,โฆ>
class classname
{
functions;
};
Class Templates
6. โข A class created from a class template is called
a Template class.
classname < type> objectname (arglist);
โข The process of creating a specific class from
class template is called Instantiation.
Template class
7. โข More than one generic data types can be used
in a class template.
โข They can be declared by comma separated list
within the template specification.
template < class T1, class T2, โฆ..>
class classname
{
โฆโฆโฆ
body of the class
โฆโฆโฆ
};
Class Templates with Multiple Parameters
8. โข Function templates used to create a family of
functions with different argument types.
Syntax:
Template < class T>
returntype functionname (arguments of type T)
{
body of the function
}
Function Templates
9. template < class T>
void swap ( T &x, T &y)
{
T temp = x;
x = y;
y = temp;
}
Example
10. Syntax:
template < class T1, class T2,โฆ.>
returntype functionname(arguments of types T1,T2,..)
{
body of the function
}
Function Template With Multiple
Parameters