C++ Programming - Functions - Discussion
Discussion Forum : Functions - Programs (Q.No. 8)
8.
What will be the output of the following program?
#include<iostream.h>
struct MyData
{
public:
int Addition(int a, int b = 10)
{
return (a *= b + 2);
}
float Addition(int a, float b);
};
int main()
{
MyData data;
cout<<data.Addition(1)<<" ";
cout<<data.Addition(3, 4);
return 0;
}
Discussion:
14 comments Page 2 of 2.
Dinesh said:
1 decade ago
I can't understand why a=18 for 3*=6? Please clarify me.
Rashmila said:
1 decade ago
Why the answer is not 12 and 14?
Hardik said:
1 decade ago
Because first prefer default arguments and if not match then go for others.
Asish Satpathy said:
1 decade ago
case "cout<<data.Addition(1)<<" ";"
Here the function will carry "Addition(1,10)"
And the evaluation will be
1*=10+2;
=>1*=12
And hence a=12.
case cout<<data.Addition(3,4);
Here the function will carry "Addition(3,4)"
And the evaluation will be
3*=4+2;
=>3*=6;
And hence a=18.
Here the function will carry "Addition(1,10)"
And the evaluation will be
1*=10+2;
=>1*=12
And hence a=12.
case cout<<data.Addition(3,4);
Here the function will carry "Addition(3,4)"
And the evaluation will be
3*=4+2;
=>3*=6;
And hence a=18.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers