-5

what is the difference between public members and publicly inherited protected members? (as it is said that protected members can only be accessed by the base class and immediate next derived class. but if we inherit protected members publicly , we can inherited it further.)

6 Answers6

1
  • A public method is visible to everyone.
  • A publicly inherited protected method is still protected, i.e. visible to the class itself and any derived classes.
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Public member can be accessed from any class using object of that class and for accessing public members you do not have to inherit that class but to access protected members you have to inherit it. Mind it if a member is declared protected in the base class and you inherited publicly then also this member will be considered as protected member of derived class.

Naga
  • 1,931
  • 4
  • 25
  • 42
0

Public members can also be accessed by other classes (not just derived) and everywhere in general.

Detheroc
  • 1,833
  • 15
  • 19
0

Well, public inheritance is your "normal" inheritance, so the semantics of public and protected members in this case are exactly what you'd think they are.

That is, your public members are public, and your protected members are visible only to the current and more-derived classes.

It's basically only when you start using private/protected inheritance that things get dicey and confusing and strange.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0
  • 1:If protected members inherited by public then child class can access it as well as it will remains protected in child class so that we can access this member in next child class.
  • 2: If protected members inherited by private then child class can access it and it will make private in child class so that we can'not access this member in next child class.
rishikesh tadaka
  • 483
  • 1
  • 6
  • 18
0

A public member is public. A protected member is protected. It doesn't matter whether they got there by being defined directly or being inherited from a public base.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165