Home / Programming MCQs / C++ MCQs / Question

D

Dharmendra Sir • 3.53K Points
Extraordinary

Q. What is the output of the following code?

Code:
#include <iostream> 
using namespace std; 
  
class MyClass { 
private: 
  static const int val = 10; 
public: 
  static int getValue() { return val; } 
}; 
  
int main() { 
  cout << MyClass::getValue() << endl; 
  return 0; 
}
(A) 10
(B) 0
(C) Compilation error
(D) None of the above
Explanation by: Dharmendra Sir
As a general rule, initialization of data members in the class declaration in C++ is not allowed, but members with “static const” are handled differently and can be initialized in the declaration.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.