Function Overloading Assignment Help

Assignment Help: >> Functions, Arguments and Overloading - Function Overloading

Function Overloading

Overloading refers to the use of the similar thing for various purposes. C++ language also allows overloading of functions. This means in which we can use the similar function name to create functions which perform a variety of various argument tasks. This is known as function polymorphism in OOP.

By using the concept of function overloading, we can design a family of functions along with one function name but within different argument lists. The function would perform various operations that depending on the argument list in the function call. The right function to be invoked is determined through checking the number and type of the arguments but on the function type. For Instance, an overloaded add ( ) function handles various types of data as display below:

// Declarations

int add(int a, int b);

int add(int a,int b, int c);

double add(double x,double y);

double add(int p, double q);

double add(double p,int q);

// Function calls cout << add(5,10);

cout << add(15,10.0);

cout << add(12.5,7.5);

cout << add(5.10,15);

cout << add(0.75,5);

A function call first matches the prototype which having the similar number and type of arguments and then calls the suitable function for execution. A finest match must be unique. The function selection includes the following steps:

1.   The compiler first tries to find an exact match in that the types of actual arguments are the similar and use that function.

2.   The compiler uses the integral promotions to the actual arguments if an exact match is not found, like as,

Char to int

Float to double

to find a match.

3.   When either of them fails, the compiler tries to use the built-in conversions (the implicit assignment conversions) to the original arguments and then uses the function whose match is unique. The compiler will generate an error message if the conversion is possible to have multiple matches. Assume we use the following two functions:

long square(long n);

double square(double x)

a function call like as square(10) will cause an error since int argument could be converted to either long or double, thus creating an ambiguous situation as to that version of square () should be used.

4.   If all of the steps fill, then the compiler will try the user-defined conversions in combination along with integral promotions and built-in conversions to search a unique match. User-defined conversions are frequently used in handling class objects.

The given program describes function overloading

#include<iostream.h>

int volume(int);

double volume(double,int);

long volume(long,int,int);

main()

{

cout << volume(10) << "\n";

cout << volume(2.5,8) << endl;

cout << volume(100L,75,15);

}

int volume(int s)

{

return (s*s*s);

}

double volume(double r,int h)

{

return(3.14519*r*r*h);

}

long volume(long l,int b,int h)

{

return (1*b*h);

}

The output of program would be

1000

157.2595

112500

Overloading of the functions should be complete with caution. We should not overload not related functions and should reserve function overloading for functions which perform closely associated tasks. A few times, the default arguments might be used instead of overloading. This might decrease the number of functions to be defined.

Overloaded functions are broadly used for handling the class objects.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd