funcation overloading

what may be possible erro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 #include<iostream>
using namespace std;
class addition{
int a,b,sum;
float n,m,sumf;
public:
int add(int,int);
float add(float,float);

};
int addition::add(int x,int y){
a=x;
b=y;
sum=a+b;
return(sum);
}
float addition::add(float x,float y){
n=x;
m=y;
sumf=n+m;
return(sumf);
}
int main() {

int q;
float w;
addition o;
q=o.add(2,3);
cout<< q << endl;
w=o.add(3.2,4.5);
cout << w <<endl;
return 0;
}
The question is not clear. What did you mean?
in funcation overloading we can use a funcation with same name that have different perameters but it is showing error that ""call of overloaded 'add(double, double)' is ambiguous""!
i mean can we have the funcation with same name with different perameter in same class?
closed account (28poGNh0)
I think you should ater type float to double like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# include <iostream>
using namespace std;

class addition
{
    int a,b,sum;
    double n,m,sumf;

    public:
        int add(int,int);
        double add(double,double);
};

int addition::add(int x,int y)
{
    a=x;
    b=y;
    sum = a+b;

    return(sum);
}

double addition::add(double x,double y)
{
    n=x;
    m=y;
    sumf = n+m;

    return(sumf);
}

int main()
{
    int q;
    double w;

    addition o;

    q = o.add(2,3);

    cout << q << endl;

    w = o.add(3.2,4.5);

    cout << w << endl;

    return 0;
}


because all floating-point literals in C++ are automatically of type double,when there is no double formal paramter the compiler cannot decide which to call add(float,float) or add(int,int)
Or call the function with values of type float.
w = o.add(3.2f, 4.5f);
thanks Techno01 and Chervil!
both things working well!
Topic archived. No new replies allowed.