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; 
}
12 12
12 18
3 14
18 12
Compilation fails.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
14 comments Page 2 of 2.

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.
(1)

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.

Arjun said:   5 years ago
@Meet Sonani.

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.


Post your comments here:

Your comments will be displayed after verification.