SlideShare a Scribd company logo
1 of 13
OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg 
wwiitthh CC++++ 
TTeemmppllaatteess iinn CC++++ 
By 
Nilesh Dalvi 
LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. 
http://www.slideshare.net/nileshdalvi01
Introduction 
• A function that works for all C++ data types is 
called as generic function. 
• Templates help the programmer to declare group 
of functions or classes. 
• When used with functions they are called as 
function templates. 
• For example, We can create template for function 
square(), it calculate square of int, float, double 
and long. 
• Templates associated with class are called as class 
templates. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Need of template 
• It is a technique that allows using single functions or 
class to work with different data types. 
• Using template we can create a single function 
that can be process any type of data. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Definition Of Class Template 
• Template declaration 
template <class T> 
class name_of_class 
{ 
//class data member and function 
• Line 1 tells the compiler that the following class 
declaration can use the template data type. 
• T is the variable of template type. 
• < > (angle bracket) is used to declare variables of 
template type, one or variables are declared 
separated by comma. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
}
Without Class Template 
#include<iostream> 
using namespace std; 
class data 
{ 
public: 
data(char c) 
{ 
cout << "c = " << c <<endl; 
} 
data(int c) 
{ 
cout << "c = " << c <<endl; 
} 
data(double c) 
{ 
cout << "c = " << c <<endl; 
} 
}; 
int main() 
{ 
data h('A'); 
data i(100); 
data j(68.2); 
return 0; 
}
Using Class Template 
#include<iostream> 
using namespace std; 
template <class T> 
class data 
{ 
public: 
data(T c) 
{ 
cout << "c = " << c <<endl; 
} 
}; 
int main() 
{ 
data <char> h('A'); 
data <int> i(100); 
data <float> j(68.2); 
return 0; 
}
Class Template with more arguments 
#include<iostream> 
using namespace std; 
template <class T1, class T2> 
class data 
{ 
public: 
data(T1 a, T2 b) 
{ 
cout << "a = " << a <<" || b = " << b <<endl; 
} 
}; 
int main() 
{ 
data <char, int> h('A',20); 
data <int, float> i(100, 23.3); 
data <float, char> j(68.2, 'D'); 
return 0; 
} 
Output: 
a = A || b = 20 
a = 100 || b = 23.3 
a = 68.2 || b = D
Definition Of Function Template 
• Template declaration 
template <class T> 
name_of_function() 
{ 
//code 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
}
Function Template 
#include<iostream> 
using namespace std; 
template <class T> 
void show (T x) 
{ 
cout << "x = " << x << endl; 
} 
int main() 
{ 
char c = 'A'; 
show(c); 
int i = 65; 
show(i); 
return 0; 
}
Function Template with more arguments 
#include<iostream> 
using namespace std; 
template <class T> 
void exchange (T &a, T &b) 
{ 
T t = a; 
a = b; 
b = t; 
} 
int main() 
{ 
int x = 5, y = 6; 
cout << "Before exchanging: "; 
cout << "x = " << x << " | " << "y = " << y <<endl; 
exchange (x, y); 
cout << "After exchanging: "; 
cout << "x = " << x << " | " << "y = " << y <<endl; 
return 0; 
} 
Output: 
Before exchanging: x = 5 | y = 6 
After exchanging: x = 6 | y = 5
Overloading of template functions 
• It can be overloaded by normal function or 
template function. 
• The compiler follows the following rules: 
– Searches for accurate match; if found it is 
invoked. 
– Searches for a template function through which 
a function that can be invoked with accurate 
match can be generated; if found it is invoked. 
– Attempts normal overloading declaration for the 
function. 
– In case no match is found; an error will be 
reported. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Overloading of template functions 
#include<iostream> 
using namespace std; 
template <class T> 
void show (T x) 
{ 
cout << "Template variable x = " << x << endl; 
} 
void show (int f) 
{ 
cout << "Integer variable f = " << f << endl; 
} 
int main() 
{ 
show('C'); 
show(50); 
show(50.25); 
return 0; 
} 
Output: 
Template variable x = C 
Integer variable f = 50 
Template variable x = 50.25
Templates

More Related Content

What's hot (20)

Templates
TemplatesTemplates
Templates
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Array within a class
Array within a classArray within a class
Array within a class
 
C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning Objects
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays
ArraysArrays
Arrays
 
C arrays
C arraysC arrays
C arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 

Viewers also liked

Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handlingsanya6900
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++TemplatesGanesh Samarthyam
 
This pointer .17
This pointer .17This pointer .17
This pointer .17myrajendra
 
[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - templateSeok-joon Yun
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)Hemant Jain
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)Sangharsh agarwal
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++Abhishek Pratap
 
Bolt C++ Standard Template Libary for HSA by Ben Sanders, AMD
Bolt C++ Standard Template Libary for HSA  by Ben Sanders, AMDBolt C++ Standard Template Libary for HSA  by Ben Sanders, AMD
Bolt C++ Standard Template Libary for HSA by Ben Sanders, AMDHSA Foundation
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programmingAbzetdin Adamov
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchymyrajendra
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++ppd1961
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 

Viewers also liked (20)

Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
file handling c++
file handling c++file handling c++
file handling c++
 
Exception handling
Exception handlingException handling
Exception handling
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Generic programming
Generic programmingGeneric programming
Generic programming
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
 
[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)
 
How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++
 
Bolt C++ Standard Template Libary for HSA by Ben Sanders, AMD
Bolt C++ Standard Template Libary for HSA  by Ben Sanders, AMDBolt C++ Standard Template Libary for HSA  by Ben Sanders, AMD
Bolt C++ Standard Template Libary for HSA by Ben Sanders, AMD
 
Introduction to object oriented programming
Introduction to object oriented programmingIntroduction to object oriented programming
Introduction to object oriented programming
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchy
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 

Similar to Templates

TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVAMuskanSony
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxshivam460694
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020Joseph Kuo
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#Rohit Rao
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in SwiftSaugat Gautam
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181Mahmoud Samir Fayed
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)PROIDEA
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义yiditushe
 

Similar to Templates (20)

TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Object Oriented Programming using C++ - Part 5
Object Oriented Programming using C++ - Part 5Object Oriented Programming using C++ - Part 5
Object Oriented Programming using C++ - Part 5
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
C#2
C#2C#2
C#2
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
 
Google cloud Dataflow & Apache Flink
Google cloud Dataflow & Apache FlinkGoogle cloud Dataflow & Apache Flink
Google cloud Dataflow & Apache Flink
 
2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
 
Oops concept
Oops conceptOops concept
Oops concept
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
 
Bc0037
Bc0037Bc0037
Bc0037
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
C++ theory
C++ theoryC++ theory
C++ theory
 

More from Nilesh Dalvi

10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to DatastructureNilesh Dalvi
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception HandlingNilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and IntefacesNilesh Dalvi
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of JavaNilesh Dalvi
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryNilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++Nilesh Dalvi
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 

More from Nilesh Dalvi (20)

14. Linked List
14. Linked List14. Linked List
14. Linked List
 
13. Queue
13. Queue13. Queue
13. Queue
 
12. Stack
12. Stack12. Stack
12. Stack
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
8. String
8. String8. String
8. String
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
File handling
File handlingFile handling
File handling
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Strings
StringsStrings
Strings
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 

Recently uploaded

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 

Recently uploaded (20)

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 

Templates

  • 1. OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg wwiitthh CC++++ TTeemmppllaatteess iinn CC++++ By Nilesh Dalvi LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. http://www.slideshare.net/nileshdalvi01
  • 2. Introduction • A function that works for all C++ data types is called as generic function. • Templates help the programmer to declare group of functions or classes. • When used with functions they are called as function templates. • For example, We can create template for function square(), it calculate square of int, float, double and long. • Templates associated with class are called as class templates. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Need of template • It is a technique that allows using single functions or class to work with different data types. • Using template we can create a single function that can be process any type of data. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Definition Of Class Template • Template declaration template <class T> class name_of_class { //class data member and function • Line 1 tells the compiler that the following class declaration can use the template data type. • T is the variable of template type. • < > (angle bracket) is used to declare variables of template type, one or variables are declared separated by comma. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). }
  • 5. Without Class Template #include<iostream> using namespace std; class data { public: data(char c) { cout << "c = " << c <<endl; } data(int c) { cout << "c = " << c <<endl; } data(double c) { cout << "c = " << c <<endl; } }; int main() { data h('A'); data i(100); data j(68.2); return 0; }
  • 6. Using Class Template #include<iostream> using namespace std; template <class T> class data { public: data(T c) { cout << "c = " << c <<endl; } }; int main() { data <char> h('A'); data <int> i(100); data <float> j(68.2); return 0; }
  • 7. Class Template with more arguments #include<iostream> using namespace std; template <class T1, class T2> class data { public: data(T1 a, T2 b) { cout << "a = " << a <<" || b = " << b <<endl; } }; int main() { data <char, int> h('A',20); data <int, float> i(100, 23.3); data <float, char> j(68.2, 'D'); return 0; } Output: a = A || b = 20 a = 100 || b = 23.3 a = 68.2 || b = D
  • 8. Definition Of Function Template • Template declaration template <class T> name_of_function() { //code Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). }
  • 9. Function Template #include<iostream> using namespace std; template <class T> void show (T x) { cout << "x = " << x << endl; } int main() { char c = 'A'; show(c); int i = 65; show(i); return 0; }
  • 10. Function Template with more arguments #include<iostream> using namespace std; template <class T> void exchange (T &a, T &b) { T t = a; a = b; b = t; } int main() { int x = 5, y = 6; cout << "Before exchanging: "; cout << "x = " << x << " | " << "y = " << y <<endl; exchange (x, y); cout << "After exchanging: "; cout << "x = " << x << " | " << "y = " << y <<endl; return 0; } Output: Before exchanging: x = 5 | y = 6 After exchanging: x = 6 | y = 5
  • 11. Overloading of template functions • It can be overloaded by normal function or template function. • The compiler follows the following rules: – Searches for accurate match; if found it is invoked. – Searches for a template function through which a function that can be invoked with accurate match can be generated; if found it is invoked. – Attempts normal overloading declaration for the function. – In case no match is found; an error will be reported. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 12. Overloading of template functions #include<iostream> using namespace std; template <class T> void show (T x) { cout << "Template variable x = " << x << endl; } void show (int f) { cout << "Integer variable f = " << f << endl; } int main() { show('C'); show(50); show(50.25); return 0; } Output: Template variable x = C Integer variable f = 50 Template variable x = 50.25

Editor's Notes

  1. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  2. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  3. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  4. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  5. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  6. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  7. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  8. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  9. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  10. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  11. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.
  12. Be selective. You do not need to cover both research and education. It does not need to be a long list. You can put down just one opportunity that you are really excited about. Just identify what you think are the biggest opportunities for your department faculty. Strike a balance between “thinking big” and being realistic. One way to think would be to say that if you were the Dean, you would invest in these opportunities. Remember the goal is to have national level prominence and visibility where our peer group will recognize our activities and accomplishments. For example, the NSF ERC on Particle Science and Technology As you go to the next slide, please bear in mind that there may well be very strong connections between this slide and the next on multi-disciplinary collaborations.