Ambiguity in function overloading arises when
1) Function 1 takes in char, Function 2 takes in int
Function call sends a float/double/long
2) Function 1 takes in char, Function 2 takes in float
Function call sends a double/int/long
3) Function 1 takes in int and a default integer value, Function 2 takes in integer,
Function call sends an int/float/double
Example 1
#include <iostream.h>
void fun(char x)
{
cout<<x;
}
void fun(int ch)
{
cout<<ch;
}
void main()
{
fun(10.5f);
}
Example 2
#include <iostream.h>
void fun(char x)
{
cout<<x;
}
void fun(float ch)
{
cout<<ch;
}
void main()
{
fun(10.5);
}
Example 3
#include <iostream.h>
void fun(int x, int y=5)
{
cout<<x;
}
void fun(int ch)
{
cout<<ch;
}
void main()
{
fun(10);
}
1) Function 1 takes in char, Function 2 takes in int
Function call sends a float/double/long
2) Function 1 takes in char, Function 2 takes in float
Function call sends a double/int/long
3) Function 1 takes in int and a default integer value, Function 2 takes in integer,
Function call sends an int/float/double
Example 1
#include <iostream.h>
void fun(char x)
{
cout<<x;
}
void fun(int ch)
{
cout<<ch;
}
void main()
{
fun(10.5f);
}
Example 2
#include <iostream.h>
void fun(char x)
{
cout<<x;
}
void fun(float ch)
{
cout<<ch;
}
void main()
{
fun(10.5);
}
Example 3
#include <iostream.h>
void fun(int x, int y=5)
{
cout<<x;
}
void fun(int ch)
{
cout<<ch;
}
void main()
{
fun(10);
}
No comments:
Post a Comment