on Leave a Comment

C++ Templates

Template is a feature in C++ that supports generic programming. C++ templates enable us to define generic classes and functions. In generic programming, generic data types are used that can work on different data types and data structures. 

Let us consider a class that handles an array of float values, but if we want to create and handle an array of int values then we have to make another class. This problem can be solved by making template class. 

Template Classes

template class can be defined as:

template <class type>
class class-name
{
     //class body
};

Here type is a placeholder, that is specified when we create object of template class

Object of template class is declared as:

class-name <type> object-name;

Here, type can be any data type like int, float, double and so on.

C++ Program to Sorts an Array Using Class Template

#include<iostream>
using namespace std;
template <class A>
class SelctionSort
{
private:
    A arr[50];
    int size;
public:
    SelctionSort(int s)
    {
        size = s;
    }
    void setElements()
    {
        int i;
        cout<<"Enter the array elements: ";
        for(i=0; i<size; i++)
            cin>>arr[i];
    }
    void sortArray()
    {
        int i, j, temp;
        for(i=0; i<size; i++)
            for(j=i+1; j<size; j++)
            if(arr[i]>arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
    }
    void displayArray()
    {
        int i, j;
        /*Sorting Array Using Selection Sort*/
        cout<<"\nArray in ascending order: ";
        for(i=0; i<size; i++)
            cout<<arr[i]<<"  ";
    }
};
int main()
{
    int size;
    cout<<"Enter the size of array: ";
    cin>>size;
    SelctionSort <float> a(size);
    a.setElements();
    a.sortArray();
    a.displayArray();
    return 0;
}

OUTPUT:

Enter the size of array: 5
Enter the array elements: 8 9 6 1 5

Array in ascending order: 1  5  6  8  9

Template Arguments

A template can have more than one argument. These arguments are separated by commas.

Syntax:

template <class type1, class type2, ...>
class class-name
{
     //class body
};

We can set the type of these arguments as string, int etc. For example,

#include<iostream>
using namespace std;
template <class A, int size>
class setArray
{
private:
    A arr[size];
public:
    void setData()
    {
        cout<<"Enter array elements: ";
        for(int i=0; i<size; i++)
            cin>>arr[i];
    }
    void displayData()
    {
        cout<<endl<<"Elements are: ";
        for(int i=0; i<size; i++)
            cout<<arr[i]<<"  ";
    }
};
int main()
{
    setArray <int, 5> Arr;
    Arr.setData();
    Arr.displayData();
    return 0;
}

OUTPUT:

Enter array elements: 8 1 6 2 4

Elements are: 8  1  6  2  4

In this program we define a variable size of data type int. When we create an object of this type of template class, we have to specify two parameters int, 5 as per our example. Here, 5 is the value of variable size.

Template Functions

Template function can be defined as:

template <class type> 
return-type function-name(parameter list) {
   // function body

Swapping of two numbers using template function

#include<iostream>
using namespace std;
template <class A>
void swapping(A &a, A &b)
{
    cout<<"Before swapping\na = "<<a<<"  b = "<<b<<endl;
    A temp;
    temp = a;
    a = b;
    b = temp;
    cout<<"\nAfter swapping\na = "<<a<<"  b = "<<b;
}
int main()
{
    int a =10, b = 20;
    swapping(a, b);
    return 0;
}

OUTPUT:

Before swapping
a = 10  b = 20

After swapping
a = 20  b = 10



0 comments:

Post a Comment