C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - Programs (Q.No. 13)
13.
Which of the following statement is correct about the program given below?
#include<iostream.h>
#include<process.h> 
class IndiaBix
{
    static int x; 
    public:
    IndiaBix()
    {
        if(x == 1)
            exit(0); 
        else
            x++;
    }
    void Display()
    {
        cout<< x << " ";
    }
};
int IndiaBix::x = 0; 
int main()
{
    IndiaBix objBix1; 
    objBix1.Display(); 
    IndiaBix objBix2; 
    objBix2.Display(); 
    return 0; 
}
The program will print the output 1 2.
The program will print the output 0 1.
The program will print the output 1 1.
The program will print the output 1.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
13 comments Page 2 of 2.

Sucheta said:   1 decade ago
Answer is the program will report compile time error as x is static variable which we can not use in display fun.

K SOWJANYA said:   1 decade ago
But how, only static functions can access static variables, x is static variable but display() is not.

Sheetal said:   1 decade ago
Here when objbix1 is constructed, the condition given in constructor fn is checked. Now x=0, so ++x will be executed and x value becomes 1 and the output comes as 1 when display fn is called. But when objbix2 is constructed then since x=1 so the program exits ie terminated and nothing is displayed afterwards. So the output is 1.


Post your comments here:

Your comments will be displayed after verification.