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 becomeprotected
(public
level is raised toprotected
),protected
fields stayprotected
(same access level, so no modification here),private
fields stayprivate
(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 thatBase
containspublicMember
. - Only the children (and their children) are aware that
Base
containsprotectedMember
. - No one but
Base
is aware ofprivateMember
.
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 ofBase
andChild
is also aware thatChild
inherits fromBase
. - If the inheritance is
protected
, onlyChild
, and its children, are aware that they inherit fromBase
. - If the inheritance is
private
, no one other thanChild
is aware of the inheritance.
Related Topics
Is It True That There Is No Need to Learn C Because C++ Contains Everything
Linear Index Upper Triangular Matrix
Qt: How to Handle the Event of the User Pressing the 'X' (Close) Button
Cannot Open Include File: 'Ctype.H': No Such File or Directory
Do Polymorphism or Conditionals Promote Better Design
Large 2D Array Gives Segmentation Fault
Std::Max - Expected an Identifier
What Is a Good Easy to Use Profiler for C++ on Linux
Writing Python Bindings for C++ Code That Use Opencv
C++ Objects: When Should I Use Pointer or Reference
Why Do Linked Lists Use Pointers Instead of Storing Nodes Inside of Nodes
Generating One Class Member Per Variadic Template Argument
Cross-Platform Iteration of Unicode String (Counting Graphemes Using Icu)