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;
}
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers