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 1 of 2.
Shriya Kar said:
8 years ago
Thank you for your explanation @Mayur.
(1)
Meet Sonani said:
8 years ago
Please explain why structure can hold a function in it?
Structure can hold a group of variables, not functions. Whereas classes can hold both variables and functions.
Structure can hold a group of variables, not functions. Whereas classes can hold both variables and functions.
(1)
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.
Hardik said:
1 decade ago
Because first prefer default arguments and if not match then go for others.
Rashmila said:
1 decade ago
Why the answer is not 12 and 14?
Dinesh said:
1 decade ago
I can't understand why a=18 for 3*=6? Please clarify me.
Vivek said:
1 decade ago
Because the precedence of "=" is less than "+" or "*" or any arithmetic operator.
Raghib said:
10 years ago
Ya "=" precedence is less than "+" or "*".
But the equation a *= b + 2 is read as a = a*b+2.
And the precedence of * is more than = . So it should be 12, 14.
Explain if anyone know this properly.
But the equation a *= b + 2 is read as a = a*b+2.
And the precedence of * is more than = . So it should be 12, 14.
Explain if anyone know this properly.
Hema said:
9 years ago
Anyone explain this properly, how additional (1,10)?
Hari said:
8 years ago
@Raghib.
Why did you expand a *= b + 2 to a = a*b+2 before applying the precedence rule?
Can you explain it?
Why did you expand a *= b + 2 to a = a*b+2 before applying the precedence rule?
Can you explain it?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers