Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

Templates

1
Templates

• Type-independent patterns that can work with


multiple data types.
– Generic programming
– Code reusable
Types:
• Function Templates
• Class Templates

2
Function and function templates

• C++ routines work on specific types. We


often need to write different routines to
perform the same operation on different
data types.

int maximum(int a, int b, int c)


{
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
3
Function and function templates

float maximum(float a, float b, float c)


{
float max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}

4
Function and function templates

double maximum(double a, double b, double c)


{
double max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}

The logic is exactly the same, but the data type is


different.

Function templates allow the logic to be written once


and used for all data types – generic function.
5
Function Templates
• Generic function to find a maximum value
(see maximum example).
template <class T>
T maximum(T a, T b, T c)
{ T max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
• Template function itself is incomplete because the compiler will
need to know the actual type to generate code. So template program
are often placed in .h or .hpp files to be included in program that
uses the function.
• C++ compiler will then generate the real function based on the use
of the function template.
6
Function Templates Usage
• After a function template is included (or defined),
the function can be used by passing parameters of
real types.
• Function template/generic function
Template <class T>
T maximum(T a, T b, T c)

int i1, i2, i3;

int m = maximum(i1, i2, i3);
• maximum(i1, i2, i3) will invoke the template function with
T==int(template function/function template specialization).
The function returns a value of int type.

7
Function Templates Usage
• Each call to maximum() on a different data
type forces the compiler to generate a different
function using the template. See the maximum
example.
– One copy of code for many types.

int i1, i2, i3;


// invoke int version of maximum
cout << "The maximum integer value is: "
<< maximum( i1, i2, i3 );
// demonstrate maximum with double values
double d1, d2, d3;
// invoke double version of maximum
cout << "The maximum double value is: "
<< maximum( d1, d2, d3 );

8
Another example(usage of second
parameter)
template< class T >
void printArray( const T *array, const int count )
{
for ( int i = 0; i < count; i++ )
cout << array[ i ] << " "; cout << endl;
}

9
Usage

char cc[100];
int ii[100];
double dd[100];
……
printArray(cc, 100);
printArray(ii, 100);
printArray(dd, 100);

10
Function template overriding
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
template <class T>
T maximum(T a, T b)
{ cout<<"From template\n“;
T max;
if (a > b) max = a; else max=b;
return max; }
char maximum(char a,char b)
{cout<<"From function\n";
char max;
if(a>b) max=a; else max=b; return max; }
main()
{ cout<<maximum('a','b'); cout<<maximum(4,5); } 11
Function Template with multiple
Arguments
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
template <class T1,class T2>
void maximum(T1 a, T2 b)
{cout<<a<<b;}
main()
{
maximum(10,'b');
maximum(4.4,5);
}
12
Overloaded function templates
#include<iostream> template <class T>
#include<bits/stdc++.h> void print(T a,int n)
using namespace std; {
cout<<"From template2\n";
template <class T> int i;
void print(T a) for(i=0;i<n;i++)
{ cout<<a;
cout<<"From template1\n"; }
cout<<a; main()
} { print(10); print(10,2); }

13
Overloaded function templates with
default arguments
#include<iostream> main()
#include<bits/stdc++.h> {
using namespace std; print(10);
template <class T> print(10,3);
void print(T a, int n=2) }
{
cout<<"From template2\n";
int i;
for(i=0;i<n;i++)
cout<<a;
}
14
Function templates with
classes(userdefined)
template <class T>
#include<iostream> void get(T &o)
#include<bits/stdc++.h> {cin>>o;}
using namespace std; template <class T>
class sample void print(T &o)
{ int a,b; {cout<<o;}
public: main()
friend istream& operator>>(istream & i,sample &s) {
{i>>s.a>>s.b; } sample s1;
friend ostream& operator<<(ostream & o,sample get(s1);
&s)
print(s1);
{o<<s.a<<s.b; } };
}
15
Class Templates-generic class
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
template<class T>
class Pair {
T a,b;
public: Pair( ) {a=b=0;}
Pair(T first, T second){a=first;b=second; }
void disp(){cout<<a<<b;}
};
main()
{ Pair<int>obj(100,75);obj.disp();
Pair<float>obj1;obj1.disp();
Pair<char>obj2('s','r');obj2.disp(); } 16
Defining disp() function outside class
template<class T>
class Pair {
T a,b;
public: Pair( ) {a=b=0;}
Pair(T first, T second){a=first;b=second; }
void disp();
};
template<class T>
void Pair<T>::disp()//no class keyword with T
{cout<<a<<b;}

17
Class Templates-generic class(Multiple template
arguments)
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
template<class T,class T1 >
class Pair { T a; T1 b;
public: Pair( ){a=b=0;}
Pair(T firstValue, T1 secondValue){a=firstValue;b=secondValue; }
void disp(){cout<<a<<b;} };
main()
{
Pair<float,int>obj1;obj1.disp();
Pair<char,int>obj2('s',1000);obj2.disp();
}
18
Class Templates-generic class(second
#include<iostream>
argument)
#include<bits/stdc++.h>
using namespace std;
template<class T,int s>
class Pair {
T a,b;
public: Pair( ){a=b=s;}
Pair(T firstValue, T secondValue){a=firstValue;b=secondValue; }
void disp(){cout<<a<<b;}};
main()
{
Pair<int,1>obj(100,75);obj.disp();
Pair<float,3>obj1;obj1.disp();
Pair<char,1>obj2('s','r');obj2.disp(); } 19
Class Templates- Explicit class Specializations
#include<iostream> #include<bits/stdc++.h>
main()
using namespace std; {
template<class T > Pair<float>obj1;
class Pair { T a; obj1.disp();
public: Pair( ){cout<<"template";a=0;} Pair<char>obj2('s');
obj2.disp();
Pair(T firstValue){cout<<"template"; Pair<int>obj(100);
a=firstValue;} obj.disp();
void disp(){cout<<"template" ; cout<<a;} }; }
template< > Note:
class Pair<int>
•explicit for (int)
{ int a; •no different syntax
public: Pair(){cout<<"exlplicit";a=0;} without <int>
Pair(int firstValue){cout<<"explicit";a=firstValue; }
void disp(){cout<<“explicit"; cout<<a;}}; 20

You might also like