English

What is Function Overloading ? Give examples of Function Overloading. - Computer Science 1

Advertisements
Advertisements

Questions

What is Function Overloading ? Give examples of Function Overloading.

Explain the concept of function overloading with example.

Answer in Brief

Solution

(1) The use of same function name to create functions that perform a variety of different tasks is called as
function overloading.
(2) Overloading refers to the use of same thing for different purposes. Function overloading or function
polymorphism, is an example of compile time polymorphism.
(3) Using the concept of function overloading, create a family of functions with one function name but
with different argument lists.
(4) The function would perform different operation, depending on argument list in function call.
(5) The correct function to be invoked is determined by checking the number and the type of the
arguments and not on the function type.

(6) e.g. #include<iostream.h>
int area (int s);                  //prototype declaration
int area (int l, int b);         //for overloading area( )
void main ( )
{
cout<<area (10); //function call
cout <<area (5, 10);
}
int area (int s) //function definition
{
return (s*s);
}
int area (int 1, int b)
{
return (1*b);
}

In above example the function area ( ) is overloaded. The first function is used to calculate area of square. It has one integer parameter.

The second function is used to calculate area of rectangle. It has two integer parameters.

(7) When a function is called, the compiler first matches the prototype having same number and types of
arguments and then calls appropriate function for execution. A best match must be unique.

shaalaa.com
C++ Programming
  Is there an error in this question or solution?
2016-2017 (March)
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×