3

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
1
  • 1
    Note: Your program has undefined behavior since int base::pub_fun() does not return an int as declared. Commented Jan 14, 2021 at 7:06

2 Answers 2

3

It compiles since the method pub_fun is declared in the base class. That is the reason it has access to the private member priv_var. If you had created a method in derived and attempted to access priv_var you would have seen a compile time error.

Sign up to request clarification or add additional context in comments.

5 Comments

But I am using derived class object 'd.pub_fun();'. So, how this access is resolved. I can understand priv_var access using 'd.base ::pub_fun();' but not with 'd.pub_fun();.
Unless you are overiding a function it in the derived class the function call is done in the base class which has access to private variables of that base class.
The function is positioned in the base class, even if you use the derived class. The derived class can define its own methods, that will then live in the derived class, and these would not have access to priv_var. So in other words the part of your class instance that lives in base have access to the private space of base.
@moonshines Only virtual member functions can be overridden. A method in the derived class with the same name as method(s) in the base class is hiding the base class method(s) with that name.
If you followed your logic, @Mr.Bala, nothing from the baseclass would be available in the derived class. However, that's simply not how C++ operates. In C++, the derived class extends the base class, so it has everything from the base and whatever it provides on top.
1

1. Class derived has access to public (and protected) members of class base, by the rules of public inheritance.

2. Members of class base have access to all other base members, because they belong to the same class.

d.pub_fun(); is valid because of 1. and int base::pub_fun() { cout << priv_var << "\n"; } is valid because of 2. so everything compiles and works without a problem.

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().

You are conflating two separate steps into one here. The derived class does indeed not have access to base::priv_var, but derived never attempts to use base::priv_var. All that derived d; does is call the public member function pub_fun(). From there on it is a matter of pub_fun implementation, which derived needs not know or care about. In your example, the implementation of pub_fun happens to use a private base variable, which it is perfectly fine to do since it is a member function of the same base class.

In other cases, class derived could be your code, and base could be a library class to which you don't even have the source code. You could always call public member functions from base even though you would have no idea what their implementation uses internally.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.