+ 16
In INHERITENCE ,the Private Members of Base Class are Inherited in Derived Class. ?YES or NO ?
Explain with Reason.?
9 Respuestas
+ 6
Private members are accessible only within the class in which they are declared.
So even if you inherit the class, you will have access only to the public and protected members of the base class but private members are not inherited(accessible).
+ 4
Yes, Private members of base class are inherited in derived class..But objects of derived class have no access to use or modify it..
For example you can check this code..
https://code.sololearn.com/c4mhw9m0uElL/?ref=app
In this code, ‘a' is private member of class A and class B inherits class A.. Plus Class B has an empty body..Now I am modifying member ‘a' using object of class B which shows that ‘a' is inherited in Class B..
Also if you don't understand this code..Just make object of class B with empty body and print the size of its object..you will see that there are no members in class B.. still it will print 4 because ‘a' is inherited..
+ 2
AnonymousGuy I completely understand what you said but the whole point of inheritance is to be able to access and modify something you received. But you rightly said that we cannot do that even through the object of subclass.
It is something like, your friend gifted you a bike but you cannot even touch it or ride it your whole life.
So there is no point in even having it.
Edit:I meant accessibility when I said inherited in my first answer, so now I have mentioned it. Forgive me if it was not clear.
+ 1
In C++, there are 3 access modifiers - private, protected and public
public members are accessible to all others
private are accessible to only the same class
protected are accessible to only the derived classes.
+ 1
The only way to use a private member of a class without being inside the class is the friend keyword. I'm not sure if it can be used with classes, though I think there was one c++ challenge question where it shows a friend keyword being used in another class.
+ 1
Avinesh Yeah, you are right..But I was just telling that private members are inherited in derived class, though this statement is not written in any book, still by practising I found out that they are inherited and can be accessed using pointer variable..
0
Shaili Shah Kindly read the above discussions that has taken place earlier.
AnonymousGuy I think you were talking about this code to check the inheritance has occurred or not.
https://code.sololearn.com/cwJQM7uH9rEc/?ref=app
0
Avinesh
Whole code shows that ‘a' is inherited in B..
I wrote that sizeof line bcoz there might be some people who finds this code confusing because of pointers..So to avoid confusion about the whole code..I wrote a simple sizeof line which also proves that private members are inherited..
- 1
private members of base class are not inherited.