basic

according to me sum should be 0 as when we call the add function a and b should be 0 and b should be 0 so sum should be zero! please explain i am not getting that!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>
using namespace std;
class addition{
 int x,y;
 public:
 void add(int a=0,int b=0){
     x=a;
     y=b;
     int sum;
     sum=x+y;
    cout<<sum <<endl;
 }
    
         
     
     
 };
 int main()
 {
     addition o;
     o.add(4,5);
    return 0; 
 }
Last edited on
You are calling the function with arguments 4 and 5.

o.add(4,5);

So the sum can not be equal to 0.

If you would call the function the following way

o.add();

you would get 0.
Topic archived. No new replies allowed.