Q:

C++ Program to Swap data using function template

belongs to collection: C++ Template Solved Programs

0

Write a C++ Program to Swap data using function template. Here’s a Simple C++ Program to Swap data using function template in C++ Programming Language.


What are Templates in C++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

 
 

A template is a blueprint or formula for creating a generic class or a function. The library containers like iterators and algorithms are examples of generic programming and have been developed using template concept.

There is a single definition of each container, such as vector, but we can define many different kinds of vectors for example, vector <int> or vector <string>..


Function Template :

The general form of a template function definition is shown here:

 

template <class type> ret-type func-name(parameter list)

{
// body of function
}


Class Template :

Just as we can define function templates, we can also define class templates. The general form of a generic class declaration is shown here:

template <class type> class class-name

{
.
.
.
}


Below is the source code for C++ Program to Swap data using function template which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* C++ Program to Swap data using function template  */

#include <iostream>
using namespace std;

template <typename T>
void Swap(T &n1, T &n2)
{
        T temp;
        temp = n1;
        n1 = n2;
        n2 = temp;
}

int main()
{
        int i1 = 6, i2 = 3;
        float f1 = 7.2, f2 = 4.5;
        char c1 = 'p', c2 = 'x';

        cout << "Before passing data to function template.\n";
        cout << "i1 = " << i1 << "\ni2 = " << i2;
        cout << "\nf1 = " << f1 << "\nf2 = " << f2;
        cout << "\nc1 = " << c1 << "\nc2 = " << c2;

        Swap(i1, i2);
        Swap(f1, f2);
        Swap(c1, c2);

        cout << "\n\nAfter passing data to function template.\n";
        cout << "i1 = " << i1 << "\ni2 = " << i2;
        cout << "\nf1 = " << f1 << "\nf2 = " << f2;
        cout << "\nc1 = " << c1 << "\nc2 = " << c2;

        return 0;
}

OUTPUT : :


/* C++ Program to Swap data using function template */

Before passing data to function template.
i1 = 6
i2 = 3
f1 = 7.2
f2 = 4.5
c1 = p
c2 = x

After passing data to function template.
i1 = 3
i2 = 6
f1 = 4.5
f2 = 7.2
c1 = x
c2 = p 
Exit code: 0 (normal program termination)

Above is the source code and output for C++ Program to Swap data using function template which is successfully compiled and run on Windows System to produce desired output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to build Simple calculator using Class... >>
<< C++ Program to find Largest among two numbers usin...