C++ member function template

C++ member function template

When was not necessary. we could have defined outside the class as well. But remember that the member functions of the template classes themselves are parameterized by the type argument(to their template classes) and therefore these functions must be defined by the functions template it takes the following general form

Template< class T >
returntype classname< t >::functionname(arglist)
{
//………
// function body
//……
}

The vector class template and its member functions are redefined as follows

//class template…..
template < class T >
class vector
{
T* v;
int size;
public:
vector(int m);
vector(T* a);
T operator*(vector & y);
};
// Member function templates…..
template< class T >
vector< t >:: Vector(int m);
{
v=new T[size=m];
for(int i=0;i < size;i++)
v[i]=0;
}
template< class T >
vector< T >::vector< T* a)
{
for(int i=0;i < size;i++)
v[i]=a[i];
}
template< class T >
T vector< T >:: operator*(vector & y)
{
T sum=0;
for(int i=0;i < size;i++)
{
T sum=0;
for(int i=0;i < size;i++)
sum+=this->v[i]*y..v[i];
return sum;
}
}

OVERLOADING OF TEMPLATE FUNCTIONS

A template function may be overloaded wither by template function or ordinary functions of its name. In such types cases the overloading resolution is accomplished as follows

1. call an general function that has an exact match.
2. call a template function that could be create with an exactly match.
3. Try the normal overloading resolution to ordinary functions and call the one that matches.

An Error is generated when no match is found. Note: that no automatic conversions are applied to arguments on the template functions in the below example shows how a template function is overloaded with an explicit function.

TEMPLATE FUNCTION WITH EXPLICIT FUNCTION
#include<iostream>
#include<string>
using namespace std;
template<class T>
void display(T x)
{
cout<<"Template display:" << x << "\n";
}
void display(int x) // overloads the
{
cout << "Explicit display:" << x << "\n";
}
int main()
{
display(100);
display(12.34);
display('c');
}

The output
Explicit display:100
Template display:12.34
template display:C

Note: The call display(100) invokes the ordinary version of display() and not the template version.

NON TYPE TEMPLATE ARGUMENTS

We have seen in previous that a template can have multiple arguments. It’s also possible to use non type arguments. That is in addition to the type argument T, we can also use other arguments or parameters such as strings, function names, constant expressions and built in types. consider the following example

template< class T, int size >
class array
{
T a[size];
//…..
//…..
};

This template supplies the size of the array as an argument in above program. This implies that the size of the array is known to the compiler at the compiler time itself. The arguments or paramteres must be specified whenever a template class is created example

array< int,10 > a1; // Array of 10 integers
Array< float,5 > a2; // array of 5 floats
array< char,20 > a3; //string of size 20
The size is given as an argument to the template class

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise