Coupon Accepted Successfully!


Member Function Templates

In the previous example, the template functions defined were inline, that is, they were defined inside the class. It is also possible to define them outside the class. While defining them outside, the function template should define the function, and the template classes are parameterized by the type argument.
17.11 Write a program to define definition of member function template outside the class and invoke the function.
template<class T>
class data
data (T c);
template<class T>
data<T>::data (T c)
cout<<“\n”<< “ c=”<<c;
int main()
data <char> h(‘A’);
data <int> i(100);
data <float> j(3.12);
return 0;
c = A
c = 100
c = 3.12

Explanation: In the above program, the constructor is defined outside the class. In such a case, the member function should be preceded by the template name as per the following statements:

Statements of program 17.11

template<class T>

data<T>::data (T c)

The first line defines the template, and the second line indicates the template class type T.

Test Your Skills Now!
Take a Quiz now
Reviewer Name