Professional Documents
Culture Documents
Tech CSE
CSL0203 OOP Paradigm with C++
UNIT - V
Templates in C++
1
Prerequisite
Knowledge of basic programming concepts
Knowledge of CPP Class Structure and Objects.
Polymorphism in CPP
2
Problem Objective
Templates in CPP.
3
Polymorphism
Polymorphism
Method
Overloading Templates
Virtual
Operator Functions
Overloading
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Template
Template is one of the most sophisticated and high-powered feature of
C++
Templates help to achieve one of the most elusive goals in
programming: the creation of reusable code
Template is a mechanism provided to implement the concept of generic
programming
C++ provides two kinds of templates:
Function Template
Class Template
8
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Function Template
Why to use?
To perform identical operations for each type of data compactly and
conveniently
What is the advantage to the programmer?
As a programmer, one has to write a single function template definition.
Later on, the C++ compiler automatically instantiates a separate object
code function based on the argument types provided in calls to the
function, to handle each type of call appropriately
Any example where function template is used?
The STL algorithms are implemented as function templates
9
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Function Template - Syntax
Syntax with one parameter:
template <class T>
return-type function-name (arguments of type T){
//Body of the function with type T
}
Example - Function template for fnSwap():
template <class T>
void fnSwap(T &rNum1, T &rNum2)
{ T Temp = rNum1;
rNum1 = rNum2;
rNum2 = Temp;
}
Example – Invoking fnSwap() template function:
fnSwap(intX, intY);
fnSwap(charX, charY);
fnSwap(doubleX, doubleY);
10
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Function Template – Syntax (Contd…)
11
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Function Template –
Syntax (Contd…)
Example - Function template for fnSearch():
//Search the value existence in given array, return the index if found
otherwise return -1
template <class T1, class T2>
int fnSearch(T1* array, T1 value, T2 iSize){
for(int iCount = 0; iCount< iSize; iCount++) {
if (array[iCount] == value)
return iCount; //Return index of value, if found
return -1; //Return -1 if value is not found
} }
Example – Invoking fnSearch():
char cMyChar = 5, acArr[] = {1,3,4,2,5,6,23};
int iMyInt = 99, aiArr[] = {1,3,4,2,5,6,23};
cout<<"\n Index of 5 in acArr : "<< fnSearch( acArr, ch, 7);
cout<<"\n Index of 99 in intArr : "<< fnSearch( intArr, i, 7);
12
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Function Template -
Overloading
Can be overloaded either by template functions or ordinary functions
Priority is given to non-generic functions
#include <iostream.h>
int main() {
template<class T1, class T2>
int i;
void fn( T1 oA , T2 oB ) {
double d;
cout <<"G1 “ << oA + oB;
float f;
?
}
template<class T> void fn( T oA ){
fn( i );
cout <<"G2 “ << oA;
}
void fn( int iNum, double dNum){
? fn(f);
fn (int)(f);
fn( i, f );
cout <<"UD1 “ << iNum - dNum;
fn( i, d );
}
void fn( int iNum ){ return 0;
cout <<"UD2 “ << iNum * 2; } }
13
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Learning Outcomes
Templates
Function Template
Class Template
SELO points Applicable
12
References
<Subject Code> 15