0% found this document useful (0 votes)
9 views21 pages

Templates

dsa notes

Uploaded by

Vishal Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

Templates

dsa notes

Uploaded by

Vishal Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Templates

Template
It is an approach where generic types are used
as parameters in algorithms so that they work
for a variety of suitable data types and data
structures.
• Generic classes
• Generic functions
Templates
• Class template- For eg. An array class for
multiple data types(int , float etc.).

• Function template- For eg. A function mul()


for multiplying int, float, double data types.
Templates
• A template is a mechanism that allows you to
create functions and classes that can be
reused with every data types.

• Template is defined with a parameter that


would be replaced, the templates are
sometimes called as parameterized classes or
functions.
Example
• To add 2 no.(2 int, 2 float, 2 double)
• One method can be – define a different
function for each
• Second method can be- define a template
with generic datatype as parameter
Syntax of class Template
template<class T>
Class classname
{
//……………
//Class member specification with
//anonymous type T
//wherever appropriate.
……………
};

T may be substituted by any datatype including user


defined datatypes.
Class Template example
• // class templates
• #include <iostream.h>
• template <class T>
• class pair
• {
• T value1, value2;
• public: pair (T first, T second)
• {value1=first;
• value2=second;
• }
• T getmax ();
• };
• template <class T>
• T pair<T>::getmax ()
• { T retval;
• retval = value1>value2? value1 : value2; return retval;
• }
• int main ()
• { pair <int> myobject (100, 75);
• cout << myobject.getmax();
• return 0; }
Class template with multiple
parameters
Template <class T1, class T2,….>
Class classname
{
……..
………(body of the class)
};
Two generic data types in a class
#include<iostream> main()
template<class T1, class T2> {
Class display display <float,int>d1(1.23,12);
{
T1 a;
display <int ,char> d2(20, ‘z’);
T2 b; d1.show();
Public: d2.show();
display(T1 x, T2 y) };
{ a=x;
b=y;}
Output:
Void show()
{ cout<<a<<“and”<<b;} 1.23 and 12
}; 20 and z
Function Templates
Template <class T>
returntype functionname(arguments of type t )
{
// body of function with type T
………..
}
Example
Template <Class T> Void f(int m, int n,float
Void swap(T& x , T& y) a,float b)
{
{
Swap(m,n);
T temp= x;
Swap(a,b);
X=y;
}
Y= temp; main()
} {
f(2,3,3.1,4.2);
}
Example of function template
#include <iostream.h> cout<<a and b before
Template<class T> swap:<<a<<“”<<b;
Void swap(T &x, T &y) swap(a,b);
{ cout<< a and b after
swap:<<a<<“”<<b;
T temp= x;
}
x=y;
Int main()
y= temp;
{
}
fun(10,30,23.32,45.32);
Void fun(int m, int n,float a, float
b) Return 0;
{ }
cout<<m and n before
swap:<<m<<“”<<n;
swap(m,n);
cout<< m and n after
swap:<<m<<“”<<n;
Function templates with multiple parameters

Template<class T1,class T2,…..>


returntype functionname(arguments of type t1,t2,…..)
{
//………
//body of the function
//……..
}
Function with two generic types
#include<iostream.h> Int main()
#include <conio.h> {
Template<class T1,class display(10,25.34);
T2> display(‘a’,200);
Void display(T1 x, T2 y) return 0;
{ }
Cout<<x<<“”<<y<<“\n”;
}
Overloading of template functions
• A template function may be overloaded either
by a template function or ordinary function.
• Overloading resolution is accomplished as:
– Call ordinary function that has an exact match.
– Call a template function that could be created
with an exact match.
Overloading of template function
#include<iostream.h> Int main()
#include<conio.h> {
Template<class T> display(100);
Void display(T x) display(56.78);
{ display(‘a’);
cout<<“Template return 0;
display”<<x<<“\n”; }
}
Void display(int x)
{
cout<<“Explicit
display<<x<<“\n”;
}
Member function templates

Template <class T>


returntype classname<T>::functionname(argument_list)
{
//……
//function body
//……
}
Non- Type Template Arguments
• In addition to type T array<int,10> a1;
arguments, we can also array<float,5> a2;
use other arguments like
int, char etc. array <char,50> a3;

Template <class T, int size>


Class array
{
T a[size];
//………
//………
};

You might also like