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 1 of 2.

Sameer said:   3 years ago
No, the Static function of a class in C++ cannot access non-static variables, but, it can access static variable only. However, non-static member function can access static and non-static variable both.

Tejas B said:   4 years ago
Thanks @Rashmi.

Shradha said:   7 years ago
Here, x is static, then how display() can access it. The answer needs to be compile time error.

Rashmi said:   7 years ago
Here x=0 firstly,

Then constructor that is called by objBix1, x==1 is condition will be false, so x++ means x=1;
ext objBix2 calls constructor and x==1 is true, so the program will be terminated.

That's it..

Hasita said:   7 years ago
How x++ becomes ++x? Please explain.

Deepanshu tyagi said:   7 years ago
Constructor Cannot Access Static Variables. Static Variables Acesss By Only Static member Variable.

Sush said:   9 years ago
Static member functions can only use a static variable and not other variables. Static variables can be used by any member functions.

Aryan said:   1 decade ago
What is process.h even this is not work in given compiler?

Abhishek said:   1 decade ago
A static member variable can be accessed by both non-static and static member functions. Although that is not the issue here.

I think the output should be 1 1 cause obj2 is executing the display function.

Somebody said:   1 decade ago
Static variables can be used inside both static and nonstatic member function directly.


Post your comments here:

Your comments will be displayed after verification.