You are on page 1of 17

Program: B.

Tech CSE
CSL0203 OOP Paradigm with C++

UNIT - V
Templates in C++

Lecture No. 8-A


Dr. Shashi Kant Gupta
Assistant Professor, Department of CSA, SOET
Slide Contents
 Topic Prerequisite
 Problem Objective
 Topic-I: Templates in CPP.
 Assignment / Exercise
 Learning Outcome
 References

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

 Is a Greek word, which means one name, multiple forms.


 Is the capability of objects to respond differently to the same message.
 The kind of response is dependent on the data types used at a given
instance.
 Same external interface can be shared by multiple objects having
different internal structures.

Polymorphism

Compile Time Run Time


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

SELO: 1, 5, 8, 12 Reference No.: R1, R2


Function Template -
Motivation
 Function fnSwap() to swap 2 integers
void fnSwap(int &riNum1, int &riNum2) { Can we use
int iTemp = riNum1; already written
riNum1 = riNum2; fnSwap() to
riNum2 = iTemp; swap 2 floats?
}
NO, we
 We need a function to swap 2 floats cannot
 Sol – overload the previously written fnSwap
void fnSwap(float &rfNum1, float &rfNum2) { Can we use
float fTemp = rfNum1; already written
rfNum1 = rfNum2; fnSwap() to
rfNum2 = fTemp; swap 2 doubles?
}
NO, we
 We need a function to swap 2 doubles
cannot
 Sol – overload the previously written fnSwap()
7
SELO: 1, 5, 8, 12 Reference No.: R1, R2
Function Template –
Motivation (Contd…)

 A separate overloaded function to swap 2 integers, 2 floats, 2 doubles, 2


longs, 2 characters etc
 What is common in all overloaded functions?
 Function Implementation is exactly same only the data type of local
variables is differing in the overloaded functions
 This approach has following disadvantages:
 Time Consuming
 Source Code becomes lengthy
 More Maintenance Cost
 Is there any better approach which overcomes all above disadvantages?
 Function 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…)

 Syntax with Multiple parameter:

template <class T1, class T2, . . .>


return-type function-name (arguments of types T1, T2, ...)
{
//Body of the function with types T1, T2, etc.
}

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

SELOs (Student Effective Learning Outcome) following Points are


applicable on this Lecture:
1. Ability to solve problems through application of theoretical & practical
concept.
5. Design thinking ability.
8. Ability to understand subject related concepts clearly along with
contemporary issues.
12. Computational thinking ability (including to translate vast data into
concepts and to understand database reasoning.

12
References

1. Object Oriented Programming with CPP “E-Balgurusamy”, Tata


McGraw Hill Publishing Company
2. Object Oriented Programming in C++ “Robert Lafore”, SAMS
Series

<Subject Code> 15

You might also like