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
πŸ’¬ Discuss
βœ… 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.
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
190
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Dharmendra Sir
Publisher
πŸ“ˆ
87%
Success Rate