Loading....
Coupon Accepted Successfully!

 

Normal Function Templates

In the previous section, we observed how to make a template class. In the same manner, a normal function (not a member function) can also use template arguments. The difference between normal and member functions is that normal functions are defined outside the class. They are not members of any class and, hence, can be invoked directly without using the object of a dot operator. The member functions are class members, and they can be invoked using the object of the class they belong to. The declaration of template member functions is described later in this chapter. In C++, normal functions are commonly used as in C. However, the user who wants to use C++ as better C can utilize this concept. The declaration of a normal template function can be done in the following manner:
 

Normal Template Function Declaration

template < class T>

retuntype function_name (arguments )

{

// code

}

 
The following program shows the practical working of template function:
 
17.3 Write a program to define normal template function.
#include<iostream.h>
#include<conio.h>
template<class T>
void show ( T x)
{cout<<“\n x=”<<x ;}
void main()
{
clrscr();
char c=‘A’;
int i=65;
double d=65.254;
show(c);
show(i);
show(d);
}
OUTPUT
x=A
x=65
x=65.254
 
Explanation: Before the body of the function show(), the template argument T is declared. The function show() has one argument x of template type. As explained earlier, the template type variable can accept all types of data. Thus, the normal function show can be used to display values of different data types. In main function, the show() functions are invoked with char, int, and double type of values being passed. The same is displayed in the output.
 
You are now familiar with utilities of templates. One more point to remember is that when we declare a class template, we can define the class data member of the template type as well as the member function of the class can also use the template member. For making a member function of template type, no separate declaration is needed. The following program explains the above point:
 
17.4 Write a program to define data members of template type.
#include<iostream.h>
#include<conio.h>
template<class T>
class data
{
T x;
public:
data (T u) {x=u;}
void show (T y)
{
cout<<“ x=”<<x;
cout<<“ y=” <<y<<“\n”;
}
};
int main()
{
clrscr();
data <char> c(‘B’);
data <int> i(100);
data <double> d(48.25);
c.show(‘A’);
i.show(65);
d.show(68.25);
return 0;
}
OUTPUT
x=B y=A
x=100 y=65
x=48.25 y=68.25
 
Explanation: In this program, before the declaration of class data, template <class T> is declared. This declaration allows the entire class, including member function and data member, to use the template-type argument. We have declared data member x of template type. In addition, the one-argument constructor and member function show() also have one formal argument of template type.
 
17.5 Write a program to create square() function using template.
#include<iostream.h>
#include<conio.h>
template <class S>
class sqr
{
public:
sqr (S c)
{
cout<<“\n”<< “ c=”<<c*c;
}
};
int main()
{
clrscr();
sqr <int> i(25);
sqr <float> f(15.2);
return 0;
}
OUTPUT
c = 625
c = 231.039993
 
Explanation: In the above program, the class sqr is declared. It contains a constructor with one argument of template type. In main() function, the object i indicates int type, and f indicates float type. The objects i and f invoke the constructor sqr() with values 25 and 15.2, respectively. The constructor displays the squares of these numbers.




Test Your Skills Now!
Take a Quiz now
Reviewer Name