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

Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++•sreejith •sree
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsNilesh Dalvi
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialKuntal Bhowmick
 

What's hot (20)

07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
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++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Array in c++
Array in c++Array in c++
Array in c++
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Command line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorialCommand line-arguments-in-java-tutorial
Command line-arguments-in-java-tutorial
 

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

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
 
Linear regression
Linear regressionLinear regression
Linear regressionTech_MX
 

More from Tech_MX (20)

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
 
Linear regression
Linear regressionLinear regression
Linear regression
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
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
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
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
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
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
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
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
 

Recently uploaded (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
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
 

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