Inheriting Private Members in C++

Inheriting private members in C++

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.

the protected and private member variables in C++ inheritance

All the member of the base class are part of the derived class. However, the derived class can only access members that are public or protected.

Declaring a member of the same name as a member of a Base class "shadows" the member of the Base class. That is the Derived class has its own independent variable that happens to have the same name as the base class version.

This is a personal choice, but I find using variables to communicate between base classes and derived classes leads to messier code so I tend to either make member variables private or use the PIMPL pattern.

Does the sub class inherit the private data members too in c++? But are accessed through the public methods of the super class?

Private data members of a class are not accessible from other classes when they are set as private.

But with the use of return functions, it is possible for the child classes to retrieve private data members from the parent class.

Example

class Parent{
int age = 35;

public int getAge(){
return age;
}
}

class Child : public Parent{

}

int main(){
Child c;
printf("%d", c.getAge());
return 0;
}

The output will be

35

access private members in inheritance

Quick answer: You don't. Thats what the protected key-word is for, which you want to use if you want to grant access to subclasses but no-one else.

private means that no-one has access to those variables, not even subclasses.

If you cannot change code in A at all, maybe there is a public/protected access method for that variable. Otherwise these variables are not meant to be accessed from subclasses and only hacks can help (which I don't encourage!).

Do Sub-Classes Really Inherit Private Member Variables?

Basically as far as I know, when you create a base class with a public, protected, and private section and variables/functions in each the public and protected sections will get inherited into the appropriate section of the sub-class (defined by class subclass : private base, which will take all public and private members of base and put them into public, changing the word private to public puts them all in public and changing it to protected puts them all into protected).

There's a bit of confusion in this statement.

Recall that inheritance is defined for classes and structs in C++. Individual objects (ie. instances) do not inherit from other objects. Constructing an object using other objects is called composition.

When a class inherits from another class, it gets everything from that class, but the access level of the inherited fields may inhibit their use within the inheritor.

Furthermore, there are 3 kinds of inheritance for classes: private (which is the default), protected, and public. Each of them changes the access level of a class properties and methods when inherited by a subclass.

If we order the access levels in this manner: public, protected, private, from the least protected to the most protected, then we can define the inheritance modifiers as raising the access levels of the inherited class fields to at least the level they designate, in the derived class (ie. the class inheriting).

For instance, if class B inherits from class A with the protected inheritance modifier:

  class B : protected A { /* ... */ };

then all the fields from A will have at least the protected level in B:

  • public fields become protected (public level is raised to protected),
  • protected fields stay protected (same access level, so no modification here),
  • private fields stay private (the access level is already above the modifier)

why does the derived class inherit the private members of the base class?

The derived class needs the private members even though it can't access them directly. Otherwise it's behavior would not build on the class it is deriving from.

For example, pretend the private stuff is:

int i;

and the class has a geti() and seti(). The value of i has to be put somewhere, even if it is private,

What is the difference between public, private, and protected inheritance in C++?

To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:".

There are three accessors that I'm aware of: public, protected and private.

Let:

class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};
  • Everything that is aware of Base is also aware that Base contains publicMember.
  • Only the children (and their children) are aware that Base contains protectedMember.
  • No one but Base is aware of privateMember.

By "is aware of", I mean "acknowledge the existence of, and thus be able to access".

next:

The same happens with public, private and protected inheritance. Let's consider a class Base and a class Child that inherits from Base.

  • If the inheritance is public, everything that is aware of Base and Child is also aware that Child inherits from Base.
  • If the inheritance is protected, only Child, and its children, are aware that they inherit from Base.
  • If the inheritance is private, no one other than Child is aware of the inheritance.


Related Topics



Leave a reply



Submit