Dar Khuda Say


Breaking News

Templat in C++

What is Template ?

                           Template is a power full feature in C++ Programing Language. It is allow the functions and classes to operate generic type. It is allow to function or class to work on many different data type using template. we can use it without rewrite code.
  

Function Template:

                The general form of template function is :

template <class type>
return_type name (parameter list) {
// piece of code
return type;
}

Example

#include  < iostream >
using namespace std;
template <class X>
void Swaping(X &n1,  X &n2){
            X temp;
            temp =  n1;
            n1 = n2;
            n2 = temp;
}
int main()
{
            int n1,n2;
            float f1,f2;
            cout << " Enter Two Integer Type Value : " ;
            cin >> n1;
            cin >> n2;
            cout << " Enter Two Floating Type Value : " ;
            cin >> f1;
            cin >> f2;
            cout << endl << " Before " << endl;
            cout << " Integer 1 = " << n1 << endl;
            cout << " Integer 2 = " << n2 << endl;
            cout << " Float 1 = " << f1 << endl;
            cout << "  Float 2 = " << f2 << endl;
            Swaping(n1, n2);
            Swaping(f1, f2);
            cout << endl  << " After " << endl;
            cout << " Integer 1 = " << n1 << endl;
            cout << " Integer 2 = " << n2 << endl;
            cout << " Float 1 = " << f1 << endl; 
            cout << " Float 2 = " << f2;
            return 0;
}

Output









No comments