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 (20)

Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
Function in C
Function in CFunction in C
Function in C
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Function in C program
Function in C programFunction in C program
Function in C program
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
This pointer
This pointerThis pointer
This pointer
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 

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

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
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.pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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); } } }