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;
}
β
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.