Function overloading
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

Function Overloading PowerPoint PPT Presentation


  • 56 Views
  • Uploaded on
  • Presentation posted in: General

Function Overloading. A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as Function overloading. Function Overloading. Two functions have different no. of arguments

Download Presentation

Function Overloading

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Function overloading

Function Overloading

A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as Function overloading.


Function overloading1

Function Overloading

Two functions have different no. of arguments

float area (float a)

{

return a*a;

}

float area (float a, float b)

{ return a*b;

}

Area(5.4);

Area(5.4, 6.3);


Function overloading2

Function Overloading

Two functions have different type of arguments

float area (float a, int b)

{

return a*b;

}

float area (float a, float b)

{ return a*b;

}

Area(5.4,10);

Area(5.4, 6.3);


Need of function overloading

Need of function overloading

Function overloading not only implements polymorphism but also reduce number of comparisons in a program and thereby makes the program run faster.


Function overloading

A function’s argument list is known as function’s signature.

If the signatures of the two functions match the previous function’s then the second is treated as a re-declaration of the first

float area (float a,float b)

{

return a*b;

}

float area (float a, float b)

{ return a*b;

}


Function overloading

If the signatures of the two functions match exactly but the return types differ, the second declaration is treated as an erroneous re-declaration of the first and is flagged at compile time as an error.

float area (float a,float b)

{

return a*b;

}

int area (float a, float b)

{ return a*b;

}


Function overloading

  • If the signatures of the two functions differ in either the number or type of their arguments, the two functions are considered to be overloaded

    float area (float a)

    {

    return a*b;

    }

    float area (float a, float b)

    { return a*b;

    }

    float area (float a, int b)

    { return a*b;

    }


Restrictions on overloaded functions

Restrictions on overloaded functions

  • Any two functions in a set of overloaded functions must have different argument list.

  • Overloading based on different return type is erroneous

  • Functions can not be overloaded on the bases of one being static and other non static

  • Type def declaration don’t define different types

    typedef char character

    void print (char a)

    void print (character a) both are same not overloaded

  • ‘Array of’ and ‘pointer to’ taken as same

  • Enumerated types are considered different.


Calling of overloaded functions

Calling of overloaded functions

  • These functions can be called like other functions.The no. & type of arguments determine which function should be invoked.

  • For Example

    float area (float a);

    float area (float a, float b);

    float area (float a, int b);

    area(20.5); Ist fn will be called

    area(20.5,10.2); IInd fn will be called

    area(20.5,7); IIIrd fn will be called


Steps involved in finding best match 1 exact match

Steps involved in finding Best Match1.Exact match

Void simple (int);

Void simple (double);

Simple(0); Found exact match


2 promotion

2 .promotion

If no exact match is found then attempt is made to achieve a match through promotion of actual arguments.

Void simple(int);

Void simple(float);

Simple(‘c’); here ‘c’ will be promoted to int type as no exact match is there.


3 using c conversion rule

3.Using C++ conversion rule

In this case an attempt is made to achieve a match by standard conversion of actual arguments.

Void simple(char);

Void simple(double);

Simple(471); here 471 will be converted to double

Void simple(long);

Void simple(double);

Simple(471); here confusion for compiler that 471 should be converted to long or double , so error will occur.


Function overloading

PRACTICE

A function printchar is defined as

Void printchar(char ch = ‘*’, int len=40)

{

For (int x=0; x<len; x++)

Cout<<ch;

}

Qn. How will you invoke the function printchar for following output:

  • To print ‘*’ 40 times

  • To print ‘*’ 20 times

  • To print ‘=‘ 40 times

  • To print ‘=‘30 times


Function overloading

Ans:

Printchar()

Printchar(‘*’, 20)

Printchar(‘=‘)

Printchar(‘=‘, 30)


Advantages of overloading over default values

Advantages of overloading over default values

  • Default values to the function

    Float area (float a=10.5, float b=20.5)

    { return a*b;

    }

    This fn can be called by:

    Float x;

    X=area();

    X=area(12.3);

    X=area(14.2,3.5);

    So the default fns gives the appearance of fn overloading.


Function overloading

  • But Fn overloading is more beneficial. Because

  • 1.Default arguments might not work for all possible combinations of arguments whereas a function may be overloaded for all possible combinations of arguments.

  • 2.With fn overloading , multiple fn definitions can be executed but with default arguments exactly one fn definition is executed.

  • 3.Saving the compiler from the trouble of testing the default values.


Function overloading

  • THANKS


  • Login