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.
Mahadev said:
2 years ago
Precedence of * is more than +.
But in this case *= is used as assignment operator whose precedence is lower than + or any arithmetical operator.
But in this case *= is used as assignment operator whose precedence is lower than + or any arithmetical operator.
Arjun said:
5 years ago
@Meet Sonani.
In C, structure can have variables only but in C++, structure can have both variables and functions.
In C, structure can have variables only but in C++, structure can have both variables and functions.
Pooja said:
6 years ago
a*=b+2 internally it means a=a*b+2 but in this case right hand side expression execute first i.e b=4 hence 4+2=6,
Now it will be a=a*6 i.e a=3*6 hence a=18.
Now it will be a=a*6 i.e a=3*6 hence a=18.
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)
Shriya Kar said:
8 years ago
Thank you for your explanation @Mayur.
(1)
Mayur Shankariya said:
8 years ago
The precedence level of *= is less than + operator so,
a *= b + 2;
3 *= 4+2;
3 *= 6;
a=3*6;
a=18;
a *= b + 2;
3 *= 4+2;
3 *= 6;
a=3*6;
a=18;
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?
Hema said:
9 years ago
Anyone explain this properly, how additional (1,10)?
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.
Vivek said:
1 decade ago
Because the precedence of "=" is less than "+" or "*" or any arithmetic operator.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers