110 likes | 297 Views
Templates. Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter. Sometime overloaded functions are identical. A simple example: square function int square( int x) { return x*x;}
E N D
Templates • Overload function: define more than one function • With same function name • Different parameter type • Different type • Different number of parameter
Sometime overloaded functions are identical • A simple example: square function int square( int x) { return x*x;} float square( float x) { return x* x;} double square(double x) { return x* x;} complex square( complex x) { return x*x;} // if complex class is defined and //overloaded operators are defined
Define function template • All square functions are identical in function body • Only data type are different • Define function templates • Programmer write a single function • Parameterize the data type • Based on argument types provided from calls to this function, compiler generates separate object code functions to handle each function call appropriately
Define template square function template <class T> T square( T x) { return x*x;} • template is the keyword followed by angle bracket with class T • Class is keyword but T is a formal type name which can be named by programmer
Use template in main function #include<iostream> using namespace std; template <class T> T square(T x) { return x*x;} int main() { int x = 10; float y = 3.3; double z = 4.5; cout<<“square of x”<<square(x)<<endl; cout<<“square of y”<<square(y)<<endl; cout<<“square of z”<<square(z)<<endl; return 0; }
Different definition template<class T> template<typename elementtype> template <class Btype,class Ftype> • The formal type parameters of a template definition are used to • Specify the types of the arguments • Specify the return type of the function • Declare variables within the function • The function definition follows and is defined like any other function • Function-template type can be any built-in type or user defined type
Another example: PrintArray • Write a function template to print array of any type #include<iostream> using namespace std; template<class T> void printArray( const T *array, const int count) { for ( int I = 0; I < count; I++) cout<< array[I]<<“ “; cout<<endl; }
printArray continue int main() { const int acount = 5; const int bcount = 7; const int ccount = 6; int a[acount] = { 1,2,3,4,5}; double b[bcount] = { 1.1,2.2,3.3,4.4,5.5}; char c[ccount] = {“HELLO”}; printArray(a,acount); printArray(b,bcount); Printarray(c,ccount); Return 0; }
A few more notes • Every formal type parameter in a function-template definition must appear in the function’s parameter list at least once • Formal type parameter names among function templates need not be unique • In printArray, the template mechanism saves the programmer from writing 3 overloaded functions with prototypes void printarray(const int *, const int); void printarray( const double *, const int); void printArray( const char *, const int);
Overloading function template • A function template may be overloaded in several ways • We can provide other function templates that specify the same function name but different function parameters • A function template also can be overloaded by providing non-template functions with the same name but different function arguments
Overload printArray template • Overload printarray function template with additional parameters lowsubscript and highSubscript to specify the portion of the array to output • template<class T> void printarray( const T *array,const int count, const int low, const int high) { // assignment to complete }