C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.55)

55. 

What is correct about the static data member of a class?

[A]. A static member function can access only static data members of a class.
[B]. A static data member is shared among all the object of the class.
[C]. A static data member can be accessed directly from main().
[D]. Both A and B.

Answer: Option D

Explanation:

No answer description available for this question.

Madison said: (Nov 11, 2014)  
The answer should be all of the above. You can have main access static data directly.

class a
{
public:
static int x;
};

int a::x = 5;

main (int argc, char** argv)
{
std::cout << a::x;
}

Roshan Bisoi said: (Nov 19, 2014)  
Can main access a static member if the class is declared protected?

Chandramohan said: (Apr 23, 2015)  
In java main method also a static method right. There we can access the static members directly then why it is wrong here.

Example:

public class A{
static void m1(){
//statements
}
public static void main(String ar[]){
m1();
}
}

Vijay said: (Dec 15, 2015)  
Answer is option D because both A & B are true.

Vikash said: (Nov 14, 2017)  
I think answer should be all of the above.

Hithes said: (Dec 3, 2017)  
Answer is all of the above,

We can access a static member function directly with the scope resolution operator (::).
ex:

class A
{
static void function() { ........ }
bla bla bla
}
int main()
{
cout << A::function();
return 0;
}

Gnanasundaram said: (Apr 10, 2018)  
Excellent explanation. Thanks all.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.