SlideShare a Scribd company logo
1 of 21
Templates (Functions)
Templates ?

 Templates are a feature of the C++ programming
 language that allow functions and classes to operate
 with generic types. This allows a function or class to
 work on many different data types without being
 rewritten for each one.
Types of templates ?

 C++ provides two kinds of templates:
   Class templates and

   Function templates.
Function Template?

 Function templates are special functions that can
 operate with generic types. This allows us to create a
 function template whose functionality can be
 adapted to more than one type or class without
 repeating the entire code for each type. In C++ this
 can be achieved using template parameters.
What is template parameter ?

 A template parameter is a special kind of parameter
 that can be used to pass a type as argument: just like
 regular function parameters can be used to pass
 values to a function, template parameters allow to
 pass values and also types to a function.
Template Instantiation

 When the compiler generates a class, function or
 static data members from a template, it is referred to
 as template instantiation.
    A function generated from a function template is called a
     generated function.
From Compiler’s point of view…

 Templates are not normal functions or classes. At
 that moment, when an instantiation is required, the
 compiler generates a function specifically for those
 arguments from the template.
template <class myType>
myType GetMax (myType a, myType b)
 {
   return (a>b?a:b);
 }

     Template function with two arguments of same
 type.
template <class T, class U>
 T GetMin (T a, U b)
{
  return (a<b?a:b);
}

      Template function with two arguments of
 different type or same type. It depends on the
 argument passed.
More…

    We can also overload a Function Template as
well as Override a Function Template.
    Overloading and Overriding can be achieved
through Functions as well as Template Functions.
Example-Overloading

#include<iostream.h>
#include<conio.h>
template <class t>
void max(t a,t b)
{
  if(a>b)
       cout<<a;
  else
       cout<<b;
}
template <class t>
void max(t a,t b,t c)
{
  if(a>b&&a>c)
       cout<<a;
  else if(b>a&&b>c)
       cout<<b;
  else
       cout<<c;
}
void main()
{
  clrscr();
  max(1,2);
  max(3,2,1);
  getch();
}
Example-Overriding

#include<iostream.h>
#include<conio.h>
#include<string.h>
template <class T>
void sorting(T a[],int n)
{
    T temp;
    int i,j;
    for(i=0;i<n;i++)
    {
             for(j=i+1;j<n;j++)
             {
                          if(a[i]>a[j])
                          {
                                          temp=a[i];
                                          a[i]=a[j];
                                          a[j]=temp;
                          }
             }
    }
}
void sorting(char a[10][10],int n)
{
   char temp[10];
   int i,j;
   for(i=0;i<n;i++)
   {
            for(j=i+1;j<n;j++)
            {
                      if(strcmp(a[i],a[j])>0)
                      {
                               strcpy(temp,a[i]);
                               strcpy(a[i],a[j]);
                               strcpy(a[j],temp);
                      }
            }
   }
}
template <class T>
void print(T a[],int n)
{
   int i;
   cout<<"nSorted Listn";
   for(i=0;i<n;i++)
   {
           cout<<a[i]<<"n";
   }
}
void print(char a[10][10],int n)
{
   int i;
   cout<<"nSorted Listn";
   for(i=0;i<n;i++)
   {
           cout<<a[i]<<"n";
   }
}
template <class T>
void get(T a[],int n)
{
   int i;
   for(i=0;i<n;i++)
   {
          cin>>a[i];
   }
}
void get(char a[10][10],int n)
{
   int i;
   for(i=0;i<n;i++)
   {
          cin>>a[i];
   }
}
void main()
{
  clrscr();
  int i=1,a[20],size;
  float b[10];
  char c[10],d[10][10];
  while(i!=5)
  {
  cout<<"nChoose that you want to sort..
  n1.Integern2.Floatn3.Charactern4.Stringn5.Exitn"
  ;
       cin>>i;
if(i==1)
{
           cout<<"nEnter the size of Listn";
           cin>>size;
           get(a,size);
           sorting(a,size);
           print(a,size);
}
else if(i==2)
{
           cout<<"nEnter the size of Listn";
           cin>>size;
           get(b,size);
           sorting(b,size);
           print(b,size);
}
else if(i==3)
        {
                    cout<<"nEnter the size of Listn";
                    cin>>size;
                    get(c,size);
                    sorting(c,size);
                    print(c,size);
        }
        else if(i==4)
        {
                    cout<<"nEnter the size of Listn";
                    cin>>size;
                    get(d,size);
                    sorting(d,size);
                    print(d,size);
        }
    }
}
Thank You.

More Related Content

What's hot

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaAbhilash Nair
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basicsgourav kottawar
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 

What's hot (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Java threads
Java threadsJava threads
Java threads
 

Viewers also liked

Viewers also liked (20)

Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Templates
TemplatesTemplates
Templates
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
file handling c++
file handling c++file handling c++
file handling c++
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Spm unit 4
Spm unit 4Spm unit 4
Spm unit 4
 
Template at c++
Template at c++Template at c++
Template at c++
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
polymorphism
polymorphism polymorphism
polymorphism
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
C++ Templates 2
C++ Templates 2C++ Templates 2
C++ Templates 2
 

Similar to Templates in C++

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFTimur Safin
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0Yaser Zhian
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methodsphil_nash
 
Structured data type
Structured data typeStructured data type
Structured data typeOmkar Majukar
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templateskinan keshkeh
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfImplement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfsktambifortune
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdfrushabhshah600
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanriturajj
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011Deepak Singh
 

Similar to Templates in C++ (20)

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Templates2
Templates2Templates2
Templates2
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
Bw14
Bw14Bw14
Bw14
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
C++ extension methods
C++ extension methodsC++ extension methods
C++ extension methods
 
Structured data type
Structured data typeStructured data type
Structured data type
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfImplement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
 
Arrays
ArraysArrays
Arrays
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011CBSE Question Paper Computer Science with C++ 2011
CBSE Question Paper Computer Science with C++ 2011
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

More from Tech_MX

Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimationTech_MX
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating SystemTech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pcTech_MX
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbmsTech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbmsTech_MX
 

More from Tech_MX (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Uid
UidUid
Uid
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
String & its application
String & its applicationString & its application
String & its application
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Spss
SpssSpss
Spss
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Parsing
ParsingParsing
Parsing
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
More on Lex
More on LexMore on Lex
More on Lex
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 
Linkers
LinkersLinkers
Linkers
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
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
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
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
 
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
 
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
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
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 🔝✔️✔️
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
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
 
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
 
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
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
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
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 

Templates in C++

  • 2. Templates ?  Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
  • 3. Types of templates ?  C++ provides two kinds of templates:  Class templates and  Function templates.
  • 4. Function Template?  Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.
  • 5. What is template parameter ?  A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass values and also types to a function.
  • 6. Template Instantiation  When the compiler generates a class, function or static data members from a template, it is referred to as template instantiation.  A function generated from a function template is called a generated function.
  • 7. From Compiler’s point of view…  Templates are not normal functions or classes. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.
  • 8. template <class myType> myType GetMax (myType a, myType b) { return (a>b?a:b); } Template function with two arguments of same type.
  • 9. template <class T, class U> T GetMin (T a, U b) { return (a<b?a:b); } Template function with two arguments of different type or same type. It depends on the argument passed.
  • 10. More… We can also overload a Function Template as well as Override a Function Template. Overloading and Overriding can be achieved through Functions as well as Template Functions.
  • 12. template <class t> void max(t a,t b,t c) { if(a>b&&a>c) cout<<a; else if(b>a&&b>c) cout<<b; else cout<<c; }
  • 13. void main() { clrscr(); max(1,2); max(3,2,1); getch(); }
  • 14. Example-Overriding #include<iostream.h> #include<conio.h> #include<string.h> template <class T> void sorting(T a[],int n) { T temp; int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } }
  • 15. void sorting(char a[10][10],int n) { char temp[10]; int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(a[i],a[j])>0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } } } }
  • 16. template <class T> void print(T a[],int n) { int i; cout<<"nSorted Listn"; for(i=0;i<n;i++) { cout<<a[i]<<"n"; } } void print(char a[10][10],int n) { int i; cout<<"nSorted Listn"; for(i=0;i<n;i++) { cout<<a[i]<<"n"; } }
  • 17. template <class T> void get(T a[],int n) { int i; for(i=0;i<n;i++) { cin>>a[i]; } } void get(char a[10][10],int n) { int i; for(i=0;i<n;i++) { cin>>a[i]; } }
  • 18. void main() { clrscr(); int i=1,a[20],size; float b[10]; char c[10],d[10][10]; while(i!=5) { cout<<"nChoose that you want to sort.. n1.Integern2.Floatn3.Charactern4.Stringn5.Exitn" ; cin>>i;
  • 19. if(i==1) { cout<<"nEnter the size of Listn"; cin>>size; get(a,size); sorting(a,size); print(a,size); } else if(i==2) { cout<<"nEnter the size of Listn"; cin>>size; get(b,size); sorting(b,size); print(b,size); }
  • 20. else if(i==3) { cout<<"nEnter the size of Listn"; cin>>size; get(c,size); sorting(c,size); print(c,size); } else if(i==4) { cout<<"nEnter the size of Listn"; cin>>size; get(d,size); sorting(d,size); print(d,size); } } }