SlideShare a Scribd company logo
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++

More Related Content

What's hot

C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 

What's hot (20)

C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
class and objects
class and objectsclass and objects
class and objects
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
virtual function
virtual functionvirtual function
virtual function
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
C++ string
C++ stringC++ string
C++ string
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Function in C program
Function in C programFunction in C program
Function in C program
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 

Viewers also liked

Templates presentation
Templates presentationTemplates presentation
Templates presentation
malaybpramanik
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
C++ Template
C++ TemplateC++ Template
C++ Template
Saket Pathak
 
Template at c++
Template at c++Template at c++
Template at c++
Lusain Kim
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Laxman Puri
 
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 .17myrajendra
 
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
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
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
HSA Foundation
 
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
 
Quicksort
QuicksortQuicksort
Quicksort
maamir farooq
 
Exception Handling
Exception HandlingException Handling
Exception HandlingAlpesh Oza
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15myrajendra
 

Viewers also liked (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 to Templates in c++

TEMPLATES in C++ are one of important topics in Object Oriented Programming
TEMPLATES in C++ are one of important topics in Object Oriented ProgrammingTEMPLATES in C++ are one of important topics in Object Oriented Programming
TEMPLATES in C++ are one of important topics in Object Oriented Programming
208BVijaySunder
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
Michael Heron
 
Templates2
Templates2Templates2
Templates2
zindadili
 
c++ Unit III - PPT.pptx
c++ Unit III - PPT.pptxc++ 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
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
FarazKhan89093
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
MZGINBarwary
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
MuskanSony
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
YashpalYadav46
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
DevliNeeraj
 
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
Abdullah Yousafzai
 
templates.ppt
templates.ppttemplates.ppt
templates.ppt
Saiganesh124618
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
Muhammad khan
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
guestf0562b
 
c++ Unit I.pptx
c++ Unit I.pptxc++ 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
Dhaval Kaneria
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
Prashant odhavani
 
C#2
C#2C#2

Similar to Templates in c++ (20)

TEMPLATES in C++ are one of important topics in Object Oriented Programming
TEMPLATES in C++ are one of important topics in Object Oriented ProgrammingTEMPLATES in C++ are one of important topics in Object Oriented Programming
TEMPLATES in C++ are one of important topics in Object Oriented Programming
 
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
 

More from Mayank Bhatt

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

More from 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
 

Recently uploaded

2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

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.