Professional Documents
Culture Documents
myTemplates
myTemplates
1
Templates
2
Function and function templates
4
Function and function templates
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.
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