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.
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;
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers