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;
}
β
Correct Answer: (A)
10
Explanation: 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.