C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - Programs (Q.No. 6)
6.
What will be the output of the following program?
#include<iostream.h> 
class India
{
    public:
    struct Bix
    {
        int   x;
        float y;
        void Function(void)
        {
            y = x = (x = 4*4); 
            y = --y * y;
        }
        void Display()
        {
            cout<< y << endl;
        } 
    }B; 
}I; 
int main()
{
    I.B.Display(); 
    return 0;
}
0
1
-1
Garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
25 comments Page 2 of 3.

Jeet said:   1 decade ago
Yes, but this is not structure please explain in detail.

Hai Nguyen said:   1 decade ago
It's all about logical assignments.

y = x = (x = 4*4);

// Successful assignment " x=4*4 " returns.
// value 1 to x, in turn successful assignment " x = (...) ".
// returns value 1 to y, so y = 1; next.

y = --y * y; // --y is operated 1st so y = 0 now, then 0*0 = 0.

Krish said:   1 decade ago
The void function was not called only display is invoked ? then how initialized y=x=(x=4*4).

How you say y=x=(x=4*4) is equal to 1?

Tejas said:   1 decade ago
In this example, we are not calling function Function(). So, value of y & x are not changed that's why it's displaying 0.

Srinivas said:   1 decade ago
In above example, immediate to classes declarations, objects I and B are also constructed in global space so that I.B.Display() will give value any of its member initialized to default i.e here y is 0.

Note 1: By keeping declaration in global space and try to create objects I and B inside main() then I.B.Display() will give any of its member value as Garbage.

Note 1: Inside main(), declare class as static and then create objects also, in this case I.B.Display() will give value any of its member initialized to default i.e here y is 0.


#include <iostream>
using namespace std;

class Bix
{
int x;
float y;
public:
void Display()
{
cout<< y << endl;
}
}B;

int main()
{
static class Bbb
{
int x;
float y;
public:
void Display()
{
cout<< y << endl;
}
}Bb;

Bix B1;
B.Display();
B1.Display();
Bb.Display();
return 0;
}

Priya said:   1 decade ago
Why answer is zero? How the function is evaluated?

Nishant sharma said:   10 years ago
Since argument type of function is void therefore it takes no arguments. Hence answer is zero.

Nuzhat said:   10 years ago
y = x = (x = 4*4).

Explain above in details.

x = 4*4 = 16?

How could it 1?

Scorpionsrk7 said:   9 years ago
What is the B and L in this program?

Nikhil said:   9 years ago
How the output will be zero?

Not understand please explain it.


Post your comments here:

Your comments will be displayed after verification.