buweilv
buweilv

Reputation: 581

Base class private member belongs to which part of Derived class?

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?

Upvotes: 1

Views: 1226

Answers (2)

SRIDHARAN
SRIDHARAN

Reputation: 1222

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

Upvotes: 0

Chandan Purbia
Chandan Purbia

Reputation: 285

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

Upvotes: 1

Related Questions