3 Replies - 2907 Views - Last Post: 14 February 2016 - 04:35 PM

#1 MaverocK  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 60
  • Joined: 21-February 14

Inheritance explanation from cplusplus.com tutorials

Posted 03 January 2016 - 02:30 PM

Greetings,

I have been intensely studying inheritance in C++. It is still not clear in my mind yet.

I came across a sentence as follows:

Quote

This public keyword after the colon ":" denotes the most accessible level the members inherited from the class that follows it (in this case Polygon) will have from the derived class (in this case Rectangle).


Source: An article named "Inheritance and Friendship" from cplusplus.com/doc/tutorial/inheritance/

If you search a random part of the sentence, you will see the sentence that I did not understand.

Could you please break this sentence into smaller chunks so that I could clearly understand that sentence? I have been trying to understand what it means for a couple of days but still could not come up with a conclusive and definitive result.

My try:
1.) There is a public keyword.
2.) The keyword is placed after the colon ":" (Of course, without quotation marks)
3.) This keyword denotes the most accessible level.
4.) There are members.
5.) These members are inherited from a class.
6.) This class, whose members are inherited, is the class whose name is placed after the keyword which is placed after the colon ":".
7.) These members, which are discussed in (4), (5) and (6), have the most accessible level.

This is all that I could understand. I am not sure whether this understanding is correct. Also, I could NOT understand the last part "from the derived class".

Please illuminate me :)

Many thanks! Looking forward to your replies!

This post has been edited by MaverocK: 03 January 2016 - 02:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance explanation from cplusplus.com tutorials

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1265
  • View blog
  • Posts: 4,979
  • Joined: 09-June 09

Re: Inheritance explanation from cplusplus.com tutorials

Posted 04 January 2016 - 12:15 PM

It just means, if you inherit class X with access level Y, then all public members of class X will have access level Y within the derived class.

For example, if a base class (Base) has public member (x)

class Base {
   public:
   int x;
};



And derived class Derived, inherits class Base with public access

class Derived: public Base {
};



The derived class has public access to variable x

On the other hand, if Derived class inherits class Base with protected access

class Derived: protected Base {
};



Variable x is now restricted to protected access with the derived class (You can only access it within the Derived member functions)

This post has been edited by jjl: 05 January 2016 - 10:03 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Inheritance explanation from cplusplus.com tutorials

Posted 14 February 2016 - 02:44 PM

Just to expand on jjl's answer, and because I can't seem to find anywhere online which does a very good job of explaining the effect of inheritance access specifiers.

the public specifier after the : colon symbol is an access restriction for members inherited from the base class.

These access restrictions on inherited members are colloquially known as 'public inheritance', 'protected inheritance' and 'private inheritance' respectively.

when used in this context, public (inheritance) is the least restrictive, and essentially inherits everything in the base class "as is". so inherited public members remain public, and inherited protected members remain protected (they do not become public!). Public inheritance is typically your defacto go-to access level for inheritance (i.e. as a rule-of-thumb, use 'public inheritance' unless you have a compelling reason not to use it, and when you fully understand the implications).

coversely, both protected inheritance and private inheritance are more restrictive for the derived class, causing any public inherited members (from the base class) to be 'demoted' in accessibility terms for users of the derived class.

'private inheritance' is the most restrictive. Any public or protected members inherited from a base class will be treated as private as far as users of your derived class are concerned (i.e. everything will appear to be hidden).

Finally, and in all cases, private members (which were obviously hidden from the derived class already) will still be inherited, and still remain hidden from your derived class. The C++ language is very intentionally designed to prevent you from ever "removing" or "loosening" an inherited restriction, because that would break encapsulation.

This post has been edited by Bench: 15 February 2016 - 01:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5828
  • View blog
  • Posts: 19,868
  • Joined: 05-May 12

Re: Inheritance explanation from cplusplus.com tutorials

Posted 14 February 2016 - 04:35 PM

The protected and private inheritance has some interesting implications for the modern way object oriented programming is taught. Nowadays when we say "A inherits from B", we highlight the "is-a relationship" to say "A is a B". With private inheritance we say "A is implemented as B".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1