3
Most read
4
Most read
5
Most read
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

PDF
Function Templates,Overlaoding Function Templates.pdf
PDF
4.2 Class Template, Template and Inheritance.pdf
PDF
Software Frameworks
PPTX
Employee Salary Presentation.l based on data science collection of data
PDF
OOPs Concepts - Android Programming
PPT
Templates
PPTX
Object Oriented Programming using C++ Unit 1
PPTX
TEMPLATES in C++ are one of important topics in Object Oriented Programming
Function Templates,Overlaoding Function Templates.pdf
4.2 Class Template, Template and Inheritance.pdf
Software Frameworks
Employee Salary Presentation.l based on data science collection of data
OOPs Concepts - Android Programming
Templates
Object Oriented Programming using C++ Unit 1
TEMPLATES in C++ are one of important topics in Object Oriented Programming

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

PPTX
Oop concept in c++ by MUhammed Thanveer Melayi
PPTX
Ch.1 oop introduction, classes and objects
PDF
Dot Net Design Patterns Interview Questions PDF By ScholarHat
PDF
Python Interview Questions And Answers 2019 | Edureka
PDF
Examples of SOLID Principles in Test Automation
PDF
Object Oriented Programming With C 2140705 Darshan All Unit Darshan Institute...
PPTX
Introduction to Object Oriented Programming
PPT
14 operator overloading
PDF
11.Object Oriented Programming.pdf
PPTX
chapter 5 Templates-Introduction in C++.pptx
PDF
Towards a Unified Data Analytics Optimizer with Yanlei Diao
PPTX
Learning of PL-SQL Introduction in DBMS.pptx
PDF
PPT
Concepts In Object Oriented Programming Languages
PPTX
Ujjwalreverseengineeringppptfinal
DOCX
Cs6301 programming and datastactures
PDF
1.10 Functions in C++,call by value .pdf
PPTX
Chapter 04 object oriented programming
PPT
P Training Presentation
Oop concept in c++ by MUhammed Thanveer Melayi
Ch.1 oop introduction, classes and objects
Dot Net Design Patterns Interview Questions PDF By ScholarHat
Python Interview Questions And Answers 2019 | Edureka
Examples of SOLID Principles in Test Automation
Object Oriented Programming With C 2140705 Darshan All Unit Darshan Institute...
Introduction to Object Oriented Programming
14 operator overloading
11.Object Oriented Programming.pdf
chapter 5 Templates-Introduction in C++.pptx
Towards a Unified Data Analytics Optimizer with Yanlei Diao
Learning of PL-SQL Introduction in DBMS.pptx
Concepts In Object Oriented Programming Languages
Ujjwalreverseengineeringppptfinal
Cs6301 programming and datastactures
1.10 Functions in C++,call by value .pdf
Chapter 04 object oriented programming
P Training Presentation
Ad

Recently uploaded (20)

PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
August Patch Tuesday
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Five Habits of High-Impact Board Members
PDF
CloudStack 4.21: First Look Webinar slides
DOCX
search engine optimization ppt fir known well about this
PDF
Architecture types and enterprise applications.pdf
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Getting Started with Data Integration: FME Form 101
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
Unlock new opportunities with location data.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
Getting started with AI Agents and Multi-Agent Systems
Group 1 Presentation -Planning and Decision Making .pptx
August Patch Tuesday
Enhancing emotion recognition model for a student engagement use case through...
sustainability-14-14877-v2.pddhzftheheeeee
Five Habits of High-Impact Board Members
CloudStack 4.21: First Look Webinar slides
search engine optimization ppt fir known well about this
Architecture types and enterprise applications.pdf
Final SEM Unit 1 for mit wpu at pune .pptx
A comparative study of natural language inference in Swahili using monolingua...
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Developing a website for English-speaking practice to English as a foreign la...
Getting Started with Data Integration: FME Form 101
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Web Crawler for Trend Tracking Gen Z Insights.pptx
Hindi spoken digit analysis for native and non-native speakers
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Unlock new opportunities with location data.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Ad

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