1 / 11

Templates

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;}

dezso
Download Presentation

Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Templates • Overload function: define more than one function • With same function name • Different parameter type • Different type • Different number of parameter

  2. 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

  3. 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

  4. 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

  5. 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; }

  6. 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

  7. 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; }

  8. 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; }

  9. 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);

  10. 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

  11. 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 }

More Related