SlideShare a Scribd company logo
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)
Subject- Object Oriented Programming (CO212)
Unit 4 – Templates and Exception Handling
Topic – 4.1 Function Templates, Overloading Function Templates
Prof. V.N.Nirgude
Assistant Professor
E-mail :
nirgudevikascomp@sanjivani.org.in
Contact No: 9975240215
Introduction
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2
 Template enable us
classes and functions
to define
and thus
generic
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.
Introduction
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3
 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
function templates.
 Template is a simple process to create a generic
anonymous type.
templates or
class with an
Function Template
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4
 Function templates are used to create a
family of functions with different argument
types.
 Syntax:
template <class T>
returntype functionname (arguments of type T)
{
………..
………..
}
Function Template
template <class T>
void swap (T x, T y)
{
T temp = x;
x = y;
y = temp;
}
void fun(int m, int n,float a, float b)
{
swap(m, n);
swap(a, b);
}
int main()
{
fun(100, 200, 11.22, 33.44);
return 0;
}
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5
Function Template with Multiple Parameters
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6
 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)
………
}
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:
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7
1999 XYZ
12.34 1234
Overloading of Function Template
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8
 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
match.
exact
⚫ Try normal overloading to ordinary function and call the one
that matches.
Overloading of Template Functions
template < class T>
void display(T x)
{
cout<<“Template Display : “ << x << “n”;
}
void display(int x)
{
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 9
Non-Type Template Arguments
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10
 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];
……..
………
};
Non-Type Template Arguments
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11
 This template supplies the size of the array as an argument to
template.
 The argument must be specified whenever a template class
object is created.
 Example:
⚫ array <int, 10> a1;
⚫ array <float, 5> a2;
⚫ array <char, 20> a3;
// Array of 10 integers
// Array of 5 floats
// String of size 20
Thank You..
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 12

More Related Content

PPTX
4.1 Function Templates,Overlaoding Function Templates.pptx
PDF
4.2 Class Template, Template and Inheritance.pdf
PDF
Software Frameworks
PDF
OOPS_Lab_Manual - programs using C++ programming language
PDF
OOPs Concepts - Android Programming
PDF
1.10 Functions in C++,call by value .pdf
PDF
Design Patterns
PDF
Classes and objects
4.1 Function Templates,Overlaoding Function Templates.pptx
4.2 Class Template, Template and Inheritance.pdf
Software Frameworks
OOPS_Lab_Manual - programs using C++ programming language
OOPs Concepts - Android Programming
1.10 Functions in C++,call by value .pdf
Design Patterns
Classes and objects

Similar to Function Templates,Overlaoding Function Templates.pdf (20)

PDF
Operator Overloading Introduction and Concept of OV
PPT
P Training Presentation
PPTX
chapter 5 Templates-Introduction in C++.pptx
PPTX
Oop concept in c++ by MUhammed Thanveer Melayi
PDF
Dot Net Design Patterns Interview Questions PDF By ScholarHat
PPTX
Ujjwalreverseengineeringppptfinal
PPT
14 operator overloading
PPTX
Learning of PL-SQL Introduction in DBMS.pptx
DOC
Ocs752 unit 4
PDF
JavaProgrammingManual
PPT
Design Patterns
PDF
Java Lab Manual
PPT
Templates
PPTX
Object Oriented Programming using C++ Unit 1
PDF
PPTX
Templates c++ - prashant odhavani - 160920107003
PDF
Object Oriented Programming using C++ - Part 5
PDF
C++ Programming
PPTX
Employee Salary Presentation.l based on data science collection of data
Operator Overloading Introduction and Concept of OV
P Training Presentation
chapter 5 Templates-Introduction in C++.pptx
Oop concept in c++ by MUhammed Thanveer Melayi
Dot Net Design Patterns Interview Questions PDF By ScholarHat
Ujjwalreverseengineeringppptfinal
14 operator overloading
Learning of PL-SQL Introduction in DBMS.pptx
Ocs752 unit 4
JavaProgrammingManual
Design Patterns
Java Lab Manual
Templates
Object Oriented Programming using C++ Unit 1
Templates c++ - prashant odhavani - 160920107003
Object Oriented Programming using C++ - Part 5
C++ Programming
Employee Salary Presentation.l based on data science collection of data
Ad

More from VikasNirgude2 (18)

PDF
2.8 Constructor and Destructor in Derived Class.pdf
PDF
Introduction to Inheritance and Its concepts
PDF
Data Conversion and Type Casting Concepts
PDF
1.11 Constructors and Destructors....pdf
PDF
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
PDF
1.7 Arrays in C++ and its detail........pdf
PDF
1.6 Control Structures in C++ .......pdf
PDF
1.5 Data Types in C++ and Its Details.pdf
PDF
C++ programming Basic and Operators of C++
PDF
1.3 Object Oriented Programming Paradigm, Basic Concepts of Object-Oriented P...
PDF
1.2 Need of Object-Oriented Programming.pdf
PDF
1.1 Introduction to procedural, modular, object-oriented and generic programm...
PDF
B Tree, Introduction ,example,Splay tree
PPT
Tree-introduction ,Definition, Types of BT
PPTX
Double Hashing.pptx
PPTX
closed hashing.pptx
PPTX
Collision resolution.pptx
PPTX
closed hashing.pptx
2.8 Constructor and Destructor in Derived Class.pdf
Introduction to Inheritance and Its concepts
Data Conversion and Type Casting Concepts
1.11 Constructors and Destructors....pdf
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
1.7 Arrays in C++ and its detail........pdf
1.6 Control Structures in C++ .......pdf
1.5 Data Types in C++ and Its Details.pdf
C++ programming Basic and Operators of C++
1.3 Object Oriented Programming Paradigm, Basic Concepts of Object-Oriented P...
1.2 Need of Object-Oriented Programming.pdf
1.1 Introduction to procedural, modular, object-oriented and generic programm...
B Tree, Introduction ,example,Splay tree
Tree-introduction ,Definition, Types of BT
Double Hashing.pptx
closed hashing.pptx
Collision resolution.pptx
closed hashing.pptx
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
573137875-Attendance-Management-System-original
PPTX
UNIT 4 Total Quality Management .pptx
PPT
Project quality management in manufacturing
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
composite construction of structures.pdf
PDF
Digital Logic Computer Design lecture notes
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Lecture Notes Electrical Wiring System Components
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
573137875-Attendance-Management-System-original
UNIT 4 Total Quality Management .pptx
Project quality management in manufacturing
CH1 Production IntroductoryConcepts.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Internet of Things (IOT) - A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
composite construction of structures.pdf
Digital Logic Computer Design lecture notes
Model Code of Practice - Construction Work - 21102022 .pdf
Sustainable Sites - Green Building Construction
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Geodesy 1.pptx...............................................
Automation-in-Manufacturing-Chapter-Introduction.pdf

Function Templates,Overlaoding Function Templates.pdf

  • 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) Subject- Object Oriented Programming (CO212) Unit 4 – Templates and Exception Handling Topic – 4.1 Function Templates, Overloading Function Templates Prof. V.N.Nirgude Assistant Professor E-mail : nirgudevikascomp@sanjivani.org.in Contact No: 9975240215
  • 2. Introduction DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2  Template enable us classes and functions to define and thus generic 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. Introduction DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3  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 function templates.  Template is a simple process to create a generic anonymous type. templates or class with an
  • 4. Function Template DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4  Function templates are used to create a family of functions with different argument types.  Syntax: template <class T> returntype functionname (arguments of type T) { ……….. ……….. }
  • 5. Function Template template <class T> void swap (T x, T y) { T temp = x; x = y; y = temp; } void fun(int m, int n,float a, float b) { swap(m, n); swap(a, b); } int main() { fun(100, 200, 11.22, 33.44); return 0; } DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5
  • 6. Function Template with Multiple Parameters DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6  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. 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: DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7 1999 XYZ 12.34 1234
  • 8. Overloading of Function Template DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8  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 match. exact ⚫ Try normal overloading to ordinary function and call the one that matches.
  • 9. Overloading of Template Functions template < class T> void display(T x) { cout<<“Template Display : “ << x << “n”; } void display(int x) { 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 9
  • 10. Non-Type Template Arguments DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10  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]; …….. ……… };
  • 11. Non-Type Template Arguments DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 11  This template supplies the size of the array as an argument to template.  The argument must be specified whenever a template class object is created.  Example: ⚫ array <int, 10> a1; ⚫ array <float, 5> a2; ⚫ array <char, 20> a3; // Array of 10 integers // Array of 5 floats // String of size 20
  • 12. Thank You.. DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 12