Vidyalelo
Computer Science · Q76

Object Oriented Programming Using C++

Engineering and GATE · Computer Science · question 76

Q76

A static data member is given a value

A.
within the class definition
B.
outside the class definition
Answer
C.
when the program is executed
D.
never

Answer: Option B

Solution

Answer: Option B
Solution:
Static data members in C++ are special variables associated with a class, not individual objects of that class.
This means there's only one copy of the static member shared by all objects of the class.
Because of this shared nature, you can't initialize them directly within the class declaration like regular member variables.
Instead, you need to give them a value outside the class definition.
This is done typically by providing an initialization outside the class, often in the same header file or a separate source file.
Therefore, the correct answer is that a static data member is given a value outside the class definition.