πŸ“Š C++
Q. What is the output of the following C++ code?
Code:
#include <iostream>
using namespace std;
 
class A
{
   int val = 55;
 
   public:
  void write(int i){
    val = i;
  }
  static void read(){
    cout << val;
  }
};

int main(int argc, char const *argv[])
{
  A a = A();
  a.write(10);
  a.read();
  return 0;
}
  • (A) 55
  • (B) 10
  • (C) Compilation error
  • (D) None of the above
πŸ’¬ Discuss
βœ… Correct Answer: (C) Compilation error

Explanation: Since the read() function is a static function and static members can only access static members, the program will display an error.

Explanation by: Dharmendra Sir
Since the read() function is a static function and static members can only access static members, the program will display an error.

πŸ’¬ Discussion


πŸ“Š Question Analytics

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