FUNCTION OVERLOADING
What is polymorphism?
One of the important features of OOPS is Polymorphism.
Polymorphism refers to ‘one name having many forms’ different behavior of an
instance depending upon the situation.
In what two ways C++
implement polymorphism?
C++ implements polymorphism through overloaded functions and
overloaded operators.
What does overloading
mean?
The term overloading means a name having two or more
distinct meanings.
Thus an overloaded function refers to a function having(one
name and) more than one distinct meanings.
What is an overloaded
operator?
Similarly, when two or more distinct meanings are defined
for an operator, it is said to be an overloaded operator.
What is static or
early binding?
The ability of the compiler to select the appropriate
function for a particular call at the compile time itself from a set of
overloaded functions that are defined. In other words, the required function
gets determined only at compile time.
What is function
overloading?
When several function declarations are specified for a
single function name in the same scope, the (function) name is said to be
overloaded. C++ allows functions to have the same name if it can distinguish
them by their number and type of arguments.
Or
A function name having several definitions that are
differentiable by the number or types of their arguments is known as
overloading of functions.
Eg:
void
prnsqr(int i);//overloaded for integer
void
prnsqr(char c);// overloaded for character
void
prnsqr(float f);//overloaded for float
void
prnsqr(double d);//overloaded for double
void
prnsqr(int i)
{
cout<<"Integer
"<<i<<"\n";
}
void
prnsqr(char c)
{
cout<<c<<"
is a character "<<"\n";
}
void
prnsqr(float f)
{
cout<<"
Float "<<f"\n";
}
void
prnsqr(double d)
{
cout<<"Double
"<<d<<"square is"<<i*i<<"\n";
}
What is the need for function overloading?
Function overloading not only implements polymorphism but
also reduces number of comparisons in a program and thereby makes the program
run faster.
What is a function’s
signature?
A function’s argument list(i.e. number and type of
arguments) is known as the function’s signature.
Are functions with
same signature and same name but different return types allowed?
No
What are the
restrictions on overloaded functions?
·
Any two
functions in a set of overloaded functions must have different argument lists.
·
Overloading functions with argument lists of the
same types, based on return type alone is an error.
·
Member functions cannot be overloaded .
·
Typedef declarations do not affect overloading
mechanism.
What is argument
matching?
Argument matching involves comparing the actual arguments of
the call with the formal arguments of each declared instance of the function.
What are the three
possible cases a function call may result in?
1)
A match – A match is found for a function call
2)
No match – No match is found for the function
call
3)
Ambiguous match – More than one defined instance
match for the function call.
Ambiguity between float and double, int and
long.
What are the steps followed by compiler in
order to find the best possible match?
1)
Search for an exact match
void afunc(int);
void afunc(double);
afunc(13);// exact match. Matches afunc(int)
2)
A match through promotion
void afunc(int);
void afunc(double);
afunc('c');// match through
promotion. Matches afunc(int)
3)
A match through application of standard C++
conversion rules
void afunc(char);
void afunc(double);
afunc(97);// match through standard
conversion rules. Matches afunc(char)
4) A
match through application of a user-defined conversion.
SA Qns – P-137
1)
Based on the number and type of arguments, the
compiler is able to interpret more than one definition during the compile time.
Steps:
i)
Looks out for exact match, if not found
ii)
Looks out for match in promotion , if not found
iii)
Looks out for promotion through conversion
2)
When the number and type of the formal and actual arguments are the same, then
it is said to be exact match.
Eg:
void fun(int a)
{ }
void main()
{
fun(5);
}
4)
####
91827
5)
i)
void repeat(int x, char c)
{
for(int i=1;i<=x;i++)
cout<<c;
}
void repeat()
{
for(int i=1;i<=12;i++)
cout<<"*";
}
void repeat(int x, char c='*')
{
for(int i=1;i<=x;i++)
cout<<c;
}
void repeat(char c, int x=12)
{
for(int i=1;i<=x;i++)
cout<<c;
}
Both
ii)
Function overloading
iii)
Default argument
iv)
Neither
iv)
Function overloading onlye
6)
C++
allows a function to assign a parameter a default value when no argument
corresponding to that parameter is specified in a call to that function. The
default value is specified in a manner syntactically similar to a variable
initialization.
- For example, this declares MyFunction as taking one int argument with a default value of 0 :
- For example, this declares MyFunction as taking one int argument with a default value of 0 :
void MyFunction(int i
=0)
{
//…
//…
}
{
//…
//…
}
- Now,
this MyFunction() can be called one of the following two ways :
MyFunction(10); //passing
explicit value
MyFunction(); //not passing any value; function will give default value
MyFunction(); //not passing any value; function will give default value
7)
int N=5;
int sum(int
arr[])
{
int total=0;
for(int
i=0;i<N;i++)
total+=arr[i];
return
total;
}
int sum(int
arr[], char ch)
{
int
etotal=0,ototal=0;
if(ch=='E')
{for(int
i=0;i<N;i++)
if(arr[i]%2==0)
if(arr[i]%2==0)
etotal+=arr[i];
return etotal;}
else
if(ch=='O')
{for(int
i=0;i<N;i++)
if(arr[i]%2!=0)
if(arr[i]%2!=0)
ototal+=arr[i];
return ototal;}
}
No comments:
Post a Comment