I was recently asked to predict the output of the following program in an interview:
#include <bits/stdc++.h>
using namespace std;
class a {
public:
int x = 10;
void f() { cout << "hello" << endl; }
void g() { cout << x << endl; }
};
int main() {
a* ptr = new a();
a* ptr1;
a* ptr2 = NULL;
ptr->f();
ptr1->f();
ptr2->f();
ptr->g();
ptr1->g();
ptr2->g();
}
When function f() is called using Null and wild pointers, the output is hello, but when function g() is called using these two pointers, no output is seen on the console. What could be the reason of this behaviour?