C++ Programming - Objects and Classes - Discussion
Discussion Forum : Objects and Classes - Programs (Q.No. 2)
2.
Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
static int x;
public:
static void SetData(int xx)
{
x = xx;
}
void Display()
{
cout<< x ;
}
};
int IndiaBix::x = 0;
int main()
{
IndiaBix::SetData(33);
IndiaBix::Display();
return 0;
}
Discussion:
19 comments Page 1 of 2.
Abcd said:
9 years ago
There is no object to use class members.
(1)
Kajju said:
9 years ago
int IndiaBix::x = 0; How to interpret this?
(1)
Abhay fegade said:
1 decade ago
Non static method can also access the static data.
(1)
Komal said:
1 decade ago
We cannot use scope resolution operator in main function.
(1)
Dixit said:
1 decade ago
Can not call display() function without object! Because display() is not declared static.
(1)
Jitendra said:
5 years ago
No. Only static functions can access static members.
Nikita said:
1 decade ago
Why is it compile time error ?
Rao Waqar said:
5 years ago
@Pooja is right. Only static function can access static data members.
Kathiravan said:
1 decade ago
Because the display is just a member function of class. It can be called in main fn by creating object. Only the static fn or variable called by class name and scope resolution.
Pravin said:
1 decade ago
We can't call non static function without creating object. So D is the right option.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers