C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 25)
25.
What will be the output of the following program?
#include<iostream.h> 
class AreaFinder
{
    float l, b, h; 
    float result; 
    public:
    AreaFinder(float hh = 0, float ll = 0, float bb = 0) 
    {
        l = ll; 
        b = bb; 
        h = hh;
    }
    void Display(int ll)
    {
        if(l = 0)
            result = 3.14f * h * h; 
        else
            result = l * b; 
        cout<< result; 
    }
};
int main()
{
    AreaFinder objAF(10, 10, 20);
    objAF.Display(0); 
    return 0; 
}
0
314
314.0000
200.0000
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Chinmoy said:   3 years ago
But in the main function l=10;
How can we take l=0;? Anyone, explain please.

Harsha said:   4 years ago
If I=0
Then it will generate the error if we are correct it by;
I== 0 then output is 200.

And there is many compilers who consider the double = as single = like jit. But there is then it goes also in else part.
(1)

Adeq said:   6 years ago
We have (l = 0), not (l == 0). So what happens there:

if (l = 0) <--- we assign value zero to variable 'l' and return this variable (which is integer with value zero)

For example, it is equal to this statement:
l = 0;
if (l) { }

We will not enter inside if statement because value zero is equal to 'false'.

So we go to else statement and simply perform:
result = l * b = 0 * 20 = 0.
(12)

Prashant daharwal said:   6 years ago
But theere is passed 10,10,20 so h=10.

then 3.14*h*h
which will be 3.14*10*10;
how it is 0?

Please tell me the solution.
(2)

Aruna said:   7 years ago
Thank you all for explaining it.
(1)

Aritra Chakraborty said:   9 years ago
Assignment Operator returns the object or reference to the object. In this case, it returns the int value back.

Imagine this, int x,y; x=y=5; then we know that both x and y is 5.

In this case the returned value is 0. And if(0) goes to the else part.

l = 0, from the assignment. Result = 0*b = 0;

PRAFUL ANAND said:   1 decade ago
Answer = 0 absolutely correct if(l=0)statement will return 0 i.e false and value 0 will be assigned to l. So, else part will be executed then result = 0*20 = 0

Pavi said:   1 decade ago
If(l=0) gives warning and this warning is treated as error.
On correcting it as (l==0),
The output is 200.

Narendra said:   1 decade ago
Condition in if(l = 0) statement, is an assignment, not comparison.

Ash said:   1 decade ago
How answer = 0?
(1)

Post your comments here:

Your comments will be displayed after verification.