SlideShare a Scribd company logo
1 of 4
Download to read offline
3/6/2016 C++ Templates: Program to Swap Two Numbers Using Function Template ­ The Crazy Programmer
http://www.thecrazyprogrammer.com/2013/11/c­templates­program­to­swap­two­numbers.html 1/4
By Admin | November 22, 2013 3 Comments
C++ Templates: Program to Swap Two Numbers
Using Function Template
What are Templates in C++?
Templates help in de ning generic classes and functions and
hence allow generic programming. Generic programming is an approach where
generic data types are used as parameters and the same piece of code work for
various data types.
Function templates are used to create family of functions
3/6/2016 C++ Templates: Program to Swap Two Numbers Using Function Template ­ The Crazy Programmer
http://www.thecrazyprogrammer.com/2013/11/c­templates­program­to­swap­two­numbers.html 2/4
with di䄗erent argument types. The format of a function template is shown
below:
template<class T>
return_type function_name (arguments of type T)
{
                . . . .
.
                . . . .
.
}
I have written a program below which will swap two numbers
using function templates.
#include<iostream>
using namespace std;
template <class T>
void swap(T&a,T&b)      //Function Template
{
    T temp=a;
    a=b;
    b=temp;
}
int main()
{
    int x1=4,y1=7;
    oat x2=4.5,y2=7.5;
    cout<<“Before Swap:”;
    cout<<“nx1=”<<x1<<“ty1=”<<y1;
    cout<<“nx2=”<<x2<<“ty2=”<<y2;
    swap(x1,y1);
    swap(x2,y2);
    cout<<“nnAfter Swap:”;
    cout<<“nx1=”<<x1<<“ty1=”<<y1;
    cout<<“nx2=”<<x2<<“ty2=”<<y2;
    return 0;
3/6/2016 C++ Templates: Program to Swap Two Numbers Using Function Template ­ The Crazy Programmer
http://www.thecrazyprogrammer.com/2013/11/c­templates­program­to­swap­two­numbers.html 3/4
You May Also Like:
}
There are so many things that I have missed in this
tutorial. In below video templates are explained very nicely. I am sure that
after watching this video you will understand templates very easily.
Category: Functions
3 thoughts on “C++ Templates: Program to Swap Two Numbers Using Function Template”
C++ Basic Skills: Lesson 27 "Templates"
0:00 / 10:35
      
Rod Ochoa
November 22, 2013
995 1
3/6/2016 C++ Templates: Program to Swap Two Numbers Using Function Template ­ The Crazy Programmer
http://www.thecrazyprogrammer.com/2013/11/c­templates­program­to­swap­two­numbers.html 4/4
Try to do it without
T temp
Yes this can be done without temp as shown below.
a=a+b;
b=a-b;
a=a-b;
without using arithmetic operators as well as temporary variable
Neeraj Mishra
November 23, 2013
mantu pani
February 16, 2014

More Related Content

What's hot (20)

C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning Objects
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Type hints Python 3
Type hints  Python 3Type hints  Python 3
Type hints Python 3
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Array within a class
Array within a classArray within a class
Array within a class
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Intake 37 linq1
Intake 37 linq1Intake 37 linq1
Intake 37 linq1
 
Arrays
ArraysArrays
Arrays
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 

Similar to C++ Templates_ Program to Swap Two Numbers Using Function Template - The Crazy Programmer

59_OOP_Function Template and multiple parameters.pptx
59_OOP_Function Template and multiple parameters.pptx59_OOP_Function Template and multiple parameters.pptx
59_OOP_Function Template and multiple parameters.pptxBhushanLilhare
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003Prashant odhavani
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++TemplatesGanesh Samarthyam
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsWilliam Olivier
 
C++ Tutorial
C++ TutorialC++ Tutorial
C++ Tutorialfreema48
 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxurvashipundir04
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxVigneshkumar Ponnusamy
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java scriptmichaelaaron25322
 
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...Anil Sharma
 
Angularjs2 presentation
Angularjs2 presentationAngularjs2 presentation
Angularjs2 presentationdharisk
 
Binary code obfuscation through c++ template meta programming
Binary code obfuscation through c++ template meta programmingBinary code obfuscation through c++ template meta programming
Binary code obfuscation through c++ template meta programmingnong_dan
 

Similar to C++ Templates_ Program to Swap Two Numbers Using Function Template - The Crazy Programmer (20)

59_OOP_Function Template and multiple parameters.pptx
59_OOP_Function Template and multiple parameters.pptx59_OOP_Function Template and multiple parameters.pptx
59_OOP_Function Template and multiple parameters.pptx
 
PRELIM-Lesson-2.pdf
PRELIM-Lesson-2.pdfPRELIM-Lesson-2.pdf
PRELIM-Lesson-2.pdf
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
 
Book management system
Book management systemBook management system
Book management system
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
Notes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculationsNotes how to work with variables, constants and do calculations
Notes how to work with variables, constants and do calculations
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C++ Tutorial
C++ TutorialC++ Tutorial
C++ Tutorial
 
C++ Lab Maual.pdf
C++ Lab Maual.pdfC++ Lab Maual.pdf
C++ Lab Maual.pdf
 
C++ Lab Maual.pdf
C++ Lab Maual.pdfC++ Lab Maual.pdf
C++ Lab Maual.pdf
 
Anagha
AnaghaAnagha
Anagha
 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptx
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
 
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
tutorials-c-sharp_understanding-generic-anonymous-methods-and-lambda-expressi...
 
Angularjs2 presentation
Angularjs2 presentationAngularjs2 presentation
Angularjs2 presentation
 
Templates1
Templates1Templates1
Templates1
 
Binary code obfuscation through c++ template meta programming
Binary code obfuscation through c++ template meta programmingBinary code obfuscation through c++ template meta programming
Binary code obfuscation through c++ template meta programming
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 

C++ Templates_ Program to Swap Two Numbers Using Function Template - The Crazy Programmer