1
class base {
    public:
        int getC() {return c;}
        int a;
    protected:
        int b;
    private:
        int c;
}

class derived: public base {
    public:
        int getD() {return d;}
    private:
        int d;
}

Now, class derived has public member:

int getC() {return c;}
int getD() {return d;}
int a;

protected member: int b; private member: int d; I can't comfirm if int c; is a private member of class derived. It's clear that any new member function of class derived can't access c. So, if c is a private member of class derived, the member function of class derived should have right to access c. So c is a what kind of member of class derived?

buweilv
  • 451
  • 1
  • 4
  • 18

2 Answers2

1

A derived class doesn't inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.

Have a look at this question

Chandan Purbia
  • 285
  • 4
  • 14
  • So, this means derived class inherit a full parent object, so is the private member in base class is also a private member of derived class, but this private member is special, because derived class doesn't inherit access to private data members? – buweilv Jul 29 '17 at 06:34
  • Listen, in simple words, I'll try to explain. If there is a `private` member in the base class which you want to access through a member of derived class, you make some `public` or `protected` member functions (getters and setters) in the base class. Here in your case, you cannot access the variable `a` of the base class from derived class, to do so, make some getters and setters in the base class in either `public` or `protected`. I hope this helps. – Chandan Purbia Jul 29 '17 at 06:42
  • Thanks for your explaining, I understand how to access private memeber of base class from derived class. My question is simple, Does private member of base class belong to derived class private member? From your answer, I see, yes, private member of base class belongs to derived class private member, it's just a special one which derived class can't directly access it. – buweilv Jul 29 '17 at 06:54
  • Yes, this is the case. It'd be better if you go through the link I have provided in my answer. – Chandan Purbia Jul 29 '17 at 07:21
0

I will clarify this with an example.

  • Consider a house where public variables and methods are outside your house. Anyone who knows your house(class object) can access them.
  • Protected variables and methods are like common areas in your house like hall, kitchen only members inside your house can access them.
  • Private members are like your parent's room (base class)(ie only your parent can go into their room and you don't know what is inside)

Now coming to the question each class access modifier has the following arrangement

base

private : int c;

protected : int b;

public : int getC() {return c;}

         int a;

derived

private : int d; (derived will never know c's existense)
protected : int b; (base class's copy)    
public : int getC() {return c;}    
         int a;
         int getD() {return d;}
SRIDHARAN
  • 1,196
  • 1
  • 15
  • 35
  • HI, I have the same understanding with you, the point is `derived` actually never know c's existense, but is `c` actually existing in class `derived`, just like this [answer](https://stackoverflow.com/questions/2676443/inheriting-private-members-in-c). – buweilv Jul 29 '17 at 07:05
  • 2
    of course "c" is still inside derived see this http://ideone.com/LPR7g2 – SRIDHARAN Jul 29 '17 at 07:30
  • @buweilv it is there but you dont have direct access to it, though you can access it through `getC` that `derived` inherited from `base` – 463035818_is_not_an_ai Jul 29 '17 at 08:02