+ 16

In INHERITENCE ,the Private Members of Base Class are Inherited in Derived Class. ?YES or NO ?

Explain with Reason.?

24th Nov 2019, 8:20 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
9 odpowiedzi
+ 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).
24th Nov 2019, 8:27 AM
Avinesh
Avinesh - avatar
+ 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..
24th Nov 2019, 1:38 PM
Alaska
Alaska - avatar
+ 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.
25th Nov 2019, 5:24 PM
Avinesh
Avinesh - avatar
+ 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.
25th Nov 2019, 1:25 PM
Sohamkumar Chauhan
Sohamkumar Chauhan - avatar
+ 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.
26th Nov 2019, 2:56 AM
CeePlusPlus
CeePlusPlus - avatar
+ 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..
26th Nov 2019, 4:18 AM
Alaska
Alaska - avatar
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
26th Nov 2019, 4:43 AM
Avinesh
Avinesh - avatar
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..
26th Nov 2019, 4:47 AM
Alaska
Alaska - avatar
- 1
private members of base class are not inherited.
26th Nov 2019, 4:33 AM
Shaili Shah
Shaili Shah - avatar