I was expecting the following code snippet to give compile error since derived class will not have priv_var which I am trying to access in pub_fun(). But it compiled and I got below mentioned output. Can someone explain the theory behind this?
class base {
private:
int priv_var = 90;
public:
int pub_fun();
} b;
class derived : public base {
} d;
int base::pub_fun() { cout << priv_var << "\n"; }
int main() {
d.pub_fun();
return 0;
}
output:
90