C++ Programming - References - Discussion

Discussion Forum : References - Programs (Q.No. 5)
5.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
enum bix
{
    a=1, b, c
};
int main()
{
    int x = c;
    int &y = x;
    int &z = x;
    y = b;
    cout<< z--;
    return 0; 
}
It will result in a compile time error.
The program will print the output 1.
The program will print the output 2.
The program will print the output 3.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
15 comments Page 1 of 2.

Prerak said:   1 decade ago
x=3
y=3
z=3

y=2 i.e.
x=2 i.e.
z=2

And decrement operator will effect only after the output !

Thus output = 2.

But z = 1.

Anonymous said:   1 decade ago
Why the value of x, y, z is 3?

Honey Garg said:   1 decade ago
Because here we used enum. In enum when we initialized variable the value will be in incremented order.

D.s.raghuram said:   1 decade ago
In enum here a=1,hence then b will be 2 and c will be 3.
x=3.
y=3.
z=3.

y=2 i.e.
x=2 i.e.
z=2.

And decrement operator will effect only after the output !

Thus output = 2.

Zubair Bhatti said:   1 decade ago
Why we use enum?

Vicky said:   1 decade ago
What is the use of enum ?

Abhishek said:   1 decade ago
Since we have use enum and we have declared 3 variables inside it.

1st one is initialized to 1, by enum property b=2, c=3.

All variables get increment by one thereafter.

For next part of answer see answer of D. S. Raghuram.

Puru said:   1 decade ago
I don't understand how the reference variable's value is allowed to change. I read that a reference variable can't change its reference.

Could anyone explain?

Mohammad Mohsin said:   10 years ago
Here, y = b then all x, y and z will point to b and b = 2 as enum used.

And z-- makes no change because post decrement operator is used.

Vikas said:   10 years ago
z worked here y=b does not affect previous and z=3 remains still then after decrements it become z=2.


Post your comments here:

Your comments will be displayed after verification.