Lecture 11
Lecture 11
Lecture - 10
We can use more than one generic type in the template function
by using the comma to separate the list.
Syntax
template<class T1, class T2,.....>return_type function_name
(arguments of type T1, T2....)
{
// body of function.
}
Note : In the above syntax, we have seen that the template
function can accept any number of arguments of a different
type.
Example
#include <iostream>
using namespace std;
template<class X,class Y> void fun(X a,Y b)
{
cout << "Value of a is : " <<a<< endl;
cout << "Value of b is : " <<b<< endl;
}
int main()
{
fun(15,12.3); r
return 0;
}
Overloading a Function Template
#include <iostream>
using namespace std; int main()
template<class X> void fun(X a) {
{
fun(10);
cout << "Value of a is : " <<a<<endl;
} fun(20,30.5);
return 0;
template<class X,class Y> void fun(X b ,Y c)
}
{
cout << "Value of b is : " <<b<< endl;
cout << "Value of c is : " <<c<< endl;
}
Restrictions of Generic Functions
Generic functions perform the same operation for all the versions of
a function except the data type differs. Let's see a simple example of
an overloaded function which cannot be replaced by the generic
function as both the functions have different functionalities.
#include <iostream>
void fun(double a)
using namespace std;
{
void fun(int b)
cout<<"value of a is : "<<a<<'\n';
{
}
if(b%2==0)
int main()
{
{
cout<<"Number is even";
fun(4.6);
}
fun(6);
else
return 0;
{
}
cout<<"Number is odd";
}
}
CLASS TEMPLATE
Class Template can also be defined similarly to the Function Template. When
a class uses the concept of Template, then the class is known as generic class.
Syntax
template<class Ttype>
class class_name
{
}
Ttype is a placeholder name which will be determined when the class is
instantiated.
We can define more than one generic data type using a comma-separated
list.
The Ttype can be used inside the class body.
CLASS TEMPLATE EXAMPLE
#include <iostream>
using namespace std;
template<class T> int main()
class A {
{ A<int> d;
public: d.add();
T num1 = 5; return 0;
T num2 = 6;
void add() }
{
cout << num1+num2<<endl;
}
};
CLASS TEMPLATE WITH MULTIPLE PARAMETERS
Syntax
template<class T1, class T2, ......>
class class_name
{
// body of Class
}
Example
#include <iostream>
using namespace std;
template<class T1, class T2> void display()
class A {
{
cout << a<<" ,"<<b << endl ;
T1 a;
T2 b;
}
public: int main()
A(T1 x,T2 y) {
{ A<int,float> d(5,6.5);
a = x; d.display();
b = y; return 0;
} }
};
Nontype Template Arguments
The template can contain multiple arguments, and we can also use the non-
type arguments(basic/derived data types) In addition to the type T argument.
we can also use other types of arguments such as strings, function names,
constant expression and built-in types.
template<class T, int size>
class array
{
T arr[size]; // automatic array initialization.
};
In the above case, the nontype template argument is size and
therefore, template supplies the size of the array as an
argument.
Arguments are specified when the objects of a class are created:
1. array<int, 15> t1; // array of 15 integers.
2. array<float, 10> t2; // array of 10 floats.
3. array<char, 4> t3; // array of 4 chars.
Example
#include <iostream>
using namespace std;
template<class T, int size>
class A
{
public:
int main()
T arr[size]; {
void insert()
{ A<int,10> t1;
int i =1;
for (int j=0;j<size;j++) t1.insert();
{ t1.display();
arr[j] = i;
i++; return 0;
}
} }
void display()
{
for(int i=0;i<size;i++)
{
cout << arr[i] << " ";
}
}
};