Presentation is loading. Please wait.

Presentation is loading. Please wait.

Name: Rubaisha Rajpoot

Similar presentations


Presentation on theme: "Name: Rubaisha Rajpoot"— Presentation transcript:

1 Name: Rubaisha Rajpoot
ID:mc

2 why and how overloaded functions can be changed to single function template
In some programming language function overloading or method overloading is the ability to create multiple methods of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context. function-declaration-a function declaration The function name declared become a template name. Function-declaration-with-placeholders a function declaration  where the type of at least one parameter uses the placeholder auto or a constrained type specified: the template parameter list will have one invented parameter for each placeholder. Parameter-list-a non-empty comma-separated list of the template parameter each of which is either non-type parameter a type parameter a template parameter or a parameter pack of any of those. A function template by itself is not a type, or a function, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be determined so that the compiler can generate an actual function (or class, from a class template). Examples are in next slides:

3 overloaded functions #include <iostream> using namespace std; int sum (int a, int b) { return a+b; } double sum (double a, double b) int main () { cout << sum (10,20) << '\n'; cout << sum (1.0,1.5) << '\n'; return 0; }

4 function template #include <iostream> using namespace std;
template <class T> T sum (T a, T b) { T result; result = a + b; return result; } int main () { int i=10, j=20, k; double f=1.0, g=1.5, h; k=sum<int>(i,j); h=sum<double>(f,g); cout << k << '\n'; cout << h << '\n'; return 0; }


Download ppt "Name: Rubaisha Rajpoot"

Similar presentations


Ads by Google