Thursday 4 April 2019

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.When the compiler shows the ambiguity error, the compiler does not run the program.


Causes of Function Overloading:

  1. Type Conversion.
  2. Function with default arguments.
  3. Function with pass by reference
Type Conversion.

The below example shows an error "call of overloaded 'fun(double)' is ambiguous". The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.

#include <iostream>  
using namespace std;  
class Cal {  
    public:  
static int add(int a,int b){    
        return a + b;    
    }    
static int add(int a, int b, int c)    
    {    
        return a + b + c;    
    }    
};   
int main(void) {  
    Cal C;
    cout<<C.add(10, 20)<<endl;    
    cout<<C.add(12, 20, 23);   
   return 0;  
}  

Function with default arguments

In the below example shows an error "call of overloaded 'fun(int)' is ambiguous". The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).

    #include<iostream>  
    using namespace std;  
    void fun(int);  
    void fun(int,int);  
    void fun(int i)  
    {  
        std::cout << "Value of i is : " <<i<< std::endl;  
    }  
    void fun(int a,int b=9)  
    {  
        std::cout << "Value of a is : " <<a<< std::endl;  
        std::cout << "Value of b is : " <<b<< std::endl;  
    }  
    int main()  
    {  
        fun(12);  
       
        return 0;  
    }  


Function with pass by reference

In the below example shows an error "call of overloaded 'fun(int&)' is ambiguous". The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &).

    #include <iostream>  
    using namespace std;  
    void fun(int);  
    void fun(int &);   
    int main()  
    {  
    int a=10;  
    fun(a); // error, which fundefined)?  
    return 0;  
    }  
    void fun(int x)  
    {  
    std::cout << "Value of x is : " <<x<< std::endl;  
    }  
    void fun(int &b)  
    {  
    std::cout << "Value of b is : " <<b<< std::endl;  
    }  

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top