SlideShare a Scribd company logo
1 of 11
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423 603
(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Computer Engineering
(NBA Accredited)
Prof. I. B. Tirse
Assistant Professor
E-mail : tirseishwaricomp@Sanjivani.org.in
Contact No: 7507113718
Subject- Object Oriented Programming (CO212)
Unit 4 – Templates and Exception Handling
Topic – 4.1 Function Templates, Overloading Function Templates
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2
Introduction
 Template enable us to define generic classes and
functions and provides support for generic
programming.
 Generic programming is
where generic types
an approach
are used as
that they
parameters in algorithms so
work for a variety of data types.
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3
Introduction
 A template can be used to create a family of classes or functions.
 For eg: a class template for an array class would enable us to
create arrays of various data types such as: int, float etc.
 Templates are created as parameterized class templates or function
templates.
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4
Function Template
 Function templates are used to create a
family of functions with different argument
types.
 Syntax:
template <class T>
returntype functionname (arguments of type T)
{
………..
………..
}
T it is Datatype on which class is working on
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5
Function Template
#include <iostream>
using namespace std;
template<class T>
T add(T a, T b)
{
T result = a+b;
return result;
}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
cout<<"Addition of i and j is :"<<
add(i,j);
cout<<'n';
cout<<"Addition of m and n is :"
<<add(m,n);
return 0;
}
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6
Function Template with Multiple Parameters
 We can have more than one generic data
type in the function template.
template < class T1, class T2>
returntype functionname(arguments of type T1, T2…)
{
…….. (Body of function)
………
}
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7
Function Template with Multiple Parameters
template <class T1, class T2>
void display(T1 x, T2 y)
{
cout<<x <<“ “ << y << “n”;
}
int main()
{
display(1999, “XYZ”);
display (12.34, 1234);
return 0;
}
Output:
1999 XYZ
12.34 1234
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8
Overloading of Function Template
 A template function may be overloaded either by
template functions or ordinary functions of its name.
 The overloading is accomplished as follows:
 Call an ordinary function that has an exact match.
 Call a template function that could be created with an exact
match.
 Try normal overloading to ordinary function and call the one
that matches.
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9
Overloading of Template Functions
template < class T>
void display(T x) //Generic Function
{
cout<<“Template Display : “ << x << “n”;
}
void display(int x) // Ordinary Function
{
cout << “Explicit Display: “ << x << “n”;
}
int main()
{
display(100);
display(12.34);
display(‘C’);
return 0;
}
Output:
Explicit Display:100
Template Display :12.34
Template Display :C
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10
Non-Type Template Arguments
 It is also possible to use non-type arguments.
 In addition to the type argument T, we can also use other
arguments such as strings, int, float, built-in types.
 Example:
template <class T, int size>
class array
{
T a[size];
……..
………
};
Class T is generic class it can be int, float, char.
Thank You..
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11

More Related Content

Similar to 4.1 Function Templates,Overlaoding Function Templates.pptx

Ujjwalreverseengineeringppptfinal
UjjwalreverseengineeringppptfinalUjjwalreverseengineeringppptfinal
Ujjwalreverseengineeringppptfinal
ujjwalchauhan87
 
Cs6301 programming and datastactures
Cs6301 programming and datastacturesCs6301 programming and datastactures
Cs6301 programming and datastactures
K.s. Ramesh
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 

Similar to 4.1 Function Templates,Overlaoding Function Templates.pptx (20)

Towards a Unified Data Analytics Optimizer with Yanlei Diao
Towards a Unified Data Analytics Optimizer with Yanlei DiaoTowards a Unified Data Analytics Optimizer with Yanlei Diao
Towards a Unified Data Analytics Optimizer with Yanlei Diao
 
Templates
TemplatesTemplates
Templates
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 
Ujjwalreverseengineeringppptfinal
UjjwalreverseengineeringppptfinalUjjwalreverseengineeringppptfinal
Ujjwalreverseengineeringppptfinal
 
Cs6301 programming and datastactures
Cs6301 programming and datastacturesCs6301 programming and datastactures
Cs6301 programming and datastactures
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
OOP in java
OOP in javaOOP in java
OOP in java
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Car Showroom management System in c++
Car Showroom management System in c++Car Showroom management System in c++
Car Showroom management System in c++
 
Unit 1
Unit  1Unit  1
Unit 1
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
lecture-intro-pet-nams-ai-in-toxicology.pptx
lecture-intro-pet-nams-ai-in-toxicology.pptxlecture-intro-pet-nams-ai-in-toxicology.pptx
lecture-intro-pet-nams-ai-in-toxicology.pptx
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Unit 5.ppt
Unit 5.pptUnit 5.ppt
Unit 5.ppt
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

4.1 Function Templates,Overlaoding Function Templates.pptx

  • 1. Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423 603 (An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune) NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified Department of Computer Engineering (NBA Accredited) Prof. I. B. Tirse Assistant Professor E-mail : tirseishwaricomp@Sanjivani.org.in Contact No: 7507113718 Subject- Object Oriented Programming (CO212) Unit 4 – Templates and Exception Handling Topic – 4.1 Function Templates, Overloading Function Templates
  • 2. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2 Introduction  Template enable us to define generic classes and functions and provides support for generic programming.  Generic programming is where generic types an approach are used as that they parameters in algorithms so work for a variety of data types.
  • 3. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3 Introduction  A template can be used to create a family of classes or functions.  For eg: a class template for an array class would enable us to create arrays of various data types such as: int, float etc.  Templates are created as parameterized class templates or function templates.
  • 4. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4 Function Template  Function templates are used to create a family of functions with different argument types.  Syntax: template <class T> returntype functionname (arguments of type T) { ……….. ……….. } T it is Datatype on which class is working on
  • 5. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5 Function Template #include <iostream> using namespace std; template<class T> T add(T a, T b) { T result = a+b; return result; } int main() { int i =2; int j =3; float m = 2.3; float n = 1.2; cout<<"Addition of i and j is :"<< add(i,j); cout<<'n'; cout<<"Addition of m and n is :" <<add(m,n); return 0; }
  • 6. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6 Function Template with Multiple Parameters  We can have more than one generic data type in the function template. template < class T1, class T2> returntype functionname(arguments of type T1, T2…) { …….. (Body of function) ……… }
  • 7. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7 Function Template with Multiple Parameters template <class T1, class T2> void display(T1 x, T2 y) { cout<<x <<“ “ << y << “n”; } int main() { display(1999, “XYZ”); display (12.34, 1234); return 0; } Output: 1999 XYZ 12.34 1234
  • 8. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8 Overloading of Function Template  A template function may be overloaded either by template functions or ordinary functions of its name.  The overloading is accomplished as follows:  Call an ordinary function that has an exact match.  Call a template function that could be created with an exact match.  Try normal overloading to ordinary function and call the one that matches.
  • 9. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9 Overloading of Template Functions template < class T> void display(T x) //Generic Function { cout<<“Template Display : “ << x << “n”; } void display(int x) // Ordinary Function { cout << “Explicit Display: “ << x << “n”; } int main() { display(100); display(12.34); display(‘C’); return 0; } Output: Explicit Display:100 Template Display :12.34 Template Display :C
  • 10. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10 Non-Type Template Arguments  It is also possible to use non-type arguments.  In addition to the type argument T, we can also use other arguments such as strings, int, float, built-in types.  Example: template <class T, int size> class array { T a[size]; …….. ……… }; Class T is generic class it can be int, float, char.
  • 11. Thank You.. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11