SlideShare una empresa de Scribd logo
1 de 13
Templates in C++
CREATED BY – MAYANK BHATT
Contents
• Templates
• Function templates
• Class templates
• Applictaions of templates
Templates
• Templates are the foundation of generic
programming, which involves writing code in a
way that is independent of any particular type.
• We can use templates to define functions as well
as classes
Function Templates
• There are several functions of considerable importance
which have to be used frequently with different data
types.
• The limitation of such functions is that they operate only
on a particular data type.
• It can be overcome by defining that function as a
function template or generic function.
• Syntax:
template <typename parameter, …..>
returntype function_name (arguments)
{
…… // body of template function
……
}
Example Program:
• #include<iostream>
• #include<conio.h>
• using namespace std;
• template <class t>
• t mymax(t a,t b)
• { return(a>b?a:b);
• }
• main()
• { int a,b;
• float c,d;
• cout<<"Enter two integers:n";
• cin>>a>>b;
• cout<<"nMax="<<max(a,b);
• cout<<"nEnter two floating point numbers:n";
• cin>>c>>d;
• cout<<"nMax="<<max(c,d);
• getch();
• }
Output:
Class Templates
• Class can also be declared to operate on different data types.
Such class are called class templates.
• A class template specifies how individual classes can be
constructed similar to normal class specification.
• These classes model a generic class which support similar
operations for different data types.
• Syntax :
template <class T1, class T2, …..>
class class_name
{
T1 data1; // data items of template type
void func1 (T1 a, T2 &b); // function of template
argument
T func2 (T2 *x, T2 *y);
}
• #include<iostream>
• #include<conio.h>
• using namespace std;
• template <class t>
• class mypair
• { private: t a,b;
• public: mypair(t first,t second)
• {a=first;
• b=second;
• }
• t getmax();
• };
• template <class t>
• t mypair<t>::getmax()
• {return(a>b?a:b);
• }
• main()
• {
• mypair<int> myints(100,275);
• mypair<float> myfloat(1.7777,1.7778);
• cout<<"max integer="<<myints.getmax();
• cout<<"nmax float="<<myfloat.getmax();
• getch();
• }
Output:
Applications of Templates
• Templates support generic programming, which
allows to develop reusable software
components such as function, class, etc.
• Supporting different data types in a single
framework.
• A template in C++ allows the construction of a
family of template functions and classes to
perform the same operation on different data
types.
• It allows a single template to deal with a generic
data type T.
Templates in c++

Más contenido relacionado

La actualidad más candente

Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

La actualidad más candente (20)

classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Function in C
Function in CFunction in C
Function in C
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
C functions
C functionsC functions
C functions
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
C++ string
C++ stringC++ string
C++ string
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
File in C language
File in C languageFile in C language
File in C language
 
Strings
StringsStrings
Strings
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Templates
TemplatesTemplates
Templates
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Array in c++
Array in c++Array in c++
Array in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Destacado

C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
Rishikesh Agrawani
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
myrajendra
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)
Hemant Jain
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
Kumar
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Alpesh Oza
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
myrajendra
 

Destacado (20)

Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Templates
TemplatesTemplates
Templates
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Template at c++
Template at c++Template at c++
Template at c++
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
C++ Templates_ Program to Swap Two Numbers Using Function Template - The Craz...
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
 
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++)
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
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
 
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++
 
Quicksort
QuicksortQuicksort
Quicksort
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Operators
OperatorsOperators
Operators
 

Similar a Templates in c++

Similar a Templates in c++ (20)

2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Templates2
Templates2Templates2
Templates2
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ Unit III - PPT.pptx
c++ Unit III - PPT.pptx
 
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
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
an introduction to c++ templates-comprehensive guide.ppt
an introduction to c++ templates-comprehensive guide.pptan introduction to c++ templates-comprehensive guide.ppt
an introduction to c++ templates-comprehensive guide.ppt
 
templates.ppt
templates.ppttemplates.ppt
templates.ppt
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
C#2
C#2C#2
C#2
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 

Más de Mayank Bhatt

Más de Mayank Bhatt (7)

Android application development part2
Android application development part2Android application development part2
Android application development part2
 
Android application development
Android application developmentAndroid application development
Android application development
 
Flickr
FlickrFlickr
Flickr
 
Javascript
JavascriptJavascript
Javascript
 
Fibre optics cable
Fibre optics cableFibre optics cable
Fibre optics cable
 
Instruction set
Instruction setInstruction set
Instruction set
 
Curve fitting
Curve fittingCurve fitting
Curve fitting
 

Último

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Templates in c++

  • 1. Templates in C++ CREATED BY – MAYANK BHATT
  • 2. Contents • Templates • Function templates • Class templates • Applictaions of templates
  • 3. Templates • Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type. • We can use templates to define functions as well as classes
  • 4. Function Templates • There are several functions of considerable importance which have to be used frequently with different data types. • The limitation of such functions is that they operate only on a particular data type. • It can be overcome by defining that function as a function template or generic function. • Syntax: template <typename parameter, …..> returntype function_name (arguments) { …… // body of template function …… }
  • 5. Example Program: • #include<iostream> • #include<conio.h> • using namespace std; • template <class t> • t mymax(t a,t b) • { return(a>b?a:b); • }
  • 6. • main() • { int a,b; • float c,d; • cout<<"Enter two integers:n"; • cin>>a>>b; • cout<<"nMax="<<max(a,b); • cout<<"nEnter two floating point numbers:n"; • cin>>c>>d; • cout<<"nMax="<<max(c,d); • getch(); • }
  • 8. Class Templates • Class can also be declared to operate on different data types. Such class are called class templates. • A class template specifies how individual classes can be constructed similar to normal class specification. • These classes model a generic class which support similar operations for different data types. • Syntax : template <class T1, class T2, …..> class class_name { T1 data1; // data items of template type void func1 (T1 a, T2 &b); // function of template argument T func2 (T2 *x, T2 *y); }
  • 9. • #include<iostream> • #include<conio.h> • using namespace std; • template <class t> • class mypair • { private: t a,b; • public: mypair(t first,t second) • {a=first; • b=second; • } • t getmax(); • };
  • 10. • template <class t> • t mypair<t>::getmax() • {return(a>b?a:b); • } • main() • { • mypair<int> myints(100,275); • mypair<float> myfloat(1.7777,1.7778); • cout<<"max integer="<<myints.getmax(); • cout<<"nmax float="<<myfloat.getmax(); • getch(); • }
  • 12. Applications of Templates • Templates support generic programming, which allows to develop reusable software components such as function, class, etc. • Supporting different data types in a single framework. • A template in C++ allows the construction of a family of template functions and classes to perform the same operation on different data types. • It allows a single template to deal with a generic data type T.