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; 
}
The program will print the output 0.
The program will print the output 33.
The program will print the output Garbage.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
19 comments Page 1 of 2.

Jagadish said:   1 decade ago
This pointer doesn't works in static member functions since the static member functions doesn't require an object to invoke the member function.

And display member function function is not a static member function it is normal member function so if we call display we need to create a object then it call.

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.

Deepak Kumar said:   1 decade ago
Because Display() is non-static method and non-static methods are always called using object.

eg :-

IndiaBix obj;
obj.Display();

Correct way to call.

Kowsik said:   1 decade ago
Replace int main() part by following Code.

int main()
{
IndiaBix obj;
obj.SetData(33);
obj.Display();
return 0;
}

Output : 33

Dixit said:   1 decade ago
Can not call display() function without object! Because display() is not declared static.
(1)

Pravin said:   1 decade ago
We can't call non static function without creating object. So D is the right option.

Madhav said:   1 decade ago
Because display is not a static function, it cannot be accessed by class name.

Venkat said:   1 decade ago
The class Indiabix don't have an object for call a member function dispaly().

Rao Waqar said:   5 years ago
@Pooja is right. Only static function can access static data members.

Komal said:   1 decade ago
We cannot use scope resolution operator in main function.
(1)


Post your comments here:

Your comments will be displayed after verification.