 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
PPD Guest
|
Posted: Tue Dec 13, 2005 11:00 pm Post subject: What is protected inheritance? |
|
|
I understand that public inheritance is "is-a" and private inheritance
is "is implemented as". What is protected inheritance?
PPD
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Wild Guest
|
Posted: Wed Dec 14, 2005 3:13 am Post subject: Re: What is protected inheritance? |
|
|
PPD wrote:
| Quote: | I understand that public inheritance is "is-a" and private inheritance
is "is implemented as". What is protected inheritance?
|
I actually never thought of it this way, but then you might think of
protected members as the "family jewels" Only descendants can access
(and override) them.
public members are for all to see, access and use. protected members
allow this only for direct descendants of a class and private members
allow it only for the class itself.
There's an exception to this rule: You can define other classes to be a
"friend", and thus allow them access to private and protected members.
Hope this shed some light on the concept
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
araav Guest
|
Posted: Wed Dec 14, 2005 12:04 pm Post subject: Re: What is protected inheritance? |
|
|
i think he knows the concept pretty well.
it's the mix of the above.
"is-a for kids and friends" and "is implemented as" for the others:)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Dec 14, 2005 12:54 pm Post subject: Re: What is protected inheritance? |
|
|
PPD wrote:
| Quote: | I understand that public inheritance is "is-a" and private
inheritance is "is implemented as".
|
That's often true, but there are other uses as well.
| Quote: | What is protected inheritance?
|
Orthogonality? I've never found a particular use for it,
either, but it is logical to have it, since we have inheritance
for the other access controls as well.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
PPD Guest
|
Posted: Wed Dec 14, 2005 12:58 pm Post subject: Re: What is protected inheritance? |
|
|
Thank you for the response.
I am misunderstood here. I did not mean protected access. Protected has
a good use in terms of limiting member visibility between inherited
classes. I appreciate that.
My question is about protected inheritance. That is, in the context of:
class A {
// members
};
what is the significance of
class B: protected A {
....
}
Again, I understand how the access restrictions change under protected
inheritance. My question is - from design philosophy, what does the
protected inheritance mean? I have never had to use this in my code. So
I wondered if there is a design scenario that I have always missed out.
PPD
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
vord.fok@gmail.com Guest
|
Posted: Wed Dec 14, 2005 7:26 pm Post subject: Re: What is protected inheritance? |
|
|
Think when you design a project with your private code .
And your staff need certain values to use your code .
And if you designed your class body right you can implement there side
projects in your code properly . thats why you have the 3 types of
inherritance . public , private or protected .
these are the functions you can call out your base class .
( instance is your call out of your main program )
in private inheritance : memberfunctions private , protected , public
instance public
(child) memberfunctions protected public
(child) instance derrived nothing
(grandchild) memberfunction nothing
(grandchild) instance derrived nothing
in public inheritance : memberfunctions private , protected , public
instance public
(child) memberfunctions protected public
(child) instance derrived public
(grandchild) memberfunction protected public
(grandchild) instance derrived public
protected inheritance : memberfunctions private , protected , public
instance public
(child) memberfunctions protected public
(child) instance derrived nothing
(grandchild) memberfunction protected public
(grandchild) instance derrived nothing
Keeping this in mind you can design projects without ever giving away
your own personal code . they only need the memberfunctions and wich
tye of inherritance .
I hope this is answering your question .
Greetings,
Smets Jean-Paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Diego Martins Guest
|
Posted: Wed Dec 14, 2005 9:03 pm Post subject: Re: What is protected inheritance? |
|
|
I don't know what is the concept of protected inheritance, but I
already used this many times when I implemented mix-in classes in terms
of base and pass through the base methods to the subsequent
subclasses...
consider this:
class Matrix {...}
class TransformMatrix: protected Matrix {
// want to implement in terms of
};
class CrazyTransformMatrix: public TransformMatrix {
// want to be a TransformMatrix and use useful methods of Matrix
};
Cheers
Diego Martins
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Augusto KhaoticMind da Si Guest
|
Posted: Thu Dec 15, 2005 10:47 am Post subject: Re: What is protected inheritance? |
|
|
[email]vord.fok (AT) gmail (DOT) com[/email] wrote:
| Quote: | Think when you design a project with your private code .
And your staff need certain values to use your code .
And if you designed your class body right you can implement there side
projects in your code properly . thats why you have the 3 types of
inherritance . public , private or protected .
these are the functions you can call out your base class .
( instance is your call out of your main program )
in private inheritance : memberfunctions private , protected , public
instance public
(child) memberfunctions protected public
(child) instance derrived nothing
(grandchild) memberfunction nothing
(grandchild) instance derrived nothing
in public inheritance : memberfunctions private , protected , public
instance public
(child) memberfunctions protected public
(child) instance derrived public
(grandchild) memberfunction protected public
(grandchild) instance derrived public
protected inheritance : memberfunctions private , protected , public
instance public
(child) memberfunctions protected public
(child) instance derrived nothing
(grandchild) memberfunction protected public
(grandchild) instance derrived nothing
Keeping this in mind you can design projects without ever giving away
your own personal code . they only need the memberfunctions and wich
tye of inherritance .
I hope this is answering your question .
Greetings,
Smets Jean-Paul
|
Another way to put this is that when you do public inheritance, the
visibility of the members of your base class is maintained on the child
(public is still public, protected is still protected and private is
still private), but when you do protected inheritance all public
members of the base class become protected in the child while all the
rest is is left unchanged. For private inheritance everything becomes
private.
Now, what does this mean in terms of design i would really love to know
as well
--
KM
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Sat Dec 17, 2005 10:58 am Post subject: Re: What is protected inheritance? |
|
|
"kanze" <kanze (AT) gabi-soft (DOT) fr> schrieb im Newsbeitrag
news:1134555084.396549.213850 (AT) g44g2000cwa (DOT) googlegroups.com...
| Quote: | PPD wrote:
I understand that public inheritance is "is-a" and private
inheritance is "is implemented as".
That's often true, but there are other uses as well.
What is protected inheritance?
Orthogonality? I've never found a particular use for it,
either, but it is logical to have it, since we have inheritance
for the other access controls as well.
|
I think the answer given by araav comes pretty close:
"is-a for kids and friends" and "is implemented as" for the others
The interesting part about protected inheritance is that contrary to private
inheritance, there is a conversion from the derived class to the base class
if the conversion is done in the derived class. Please take a look at the
following example:
struct A {};
// Note protected inheritance.
struct B : protected A
{
void f( B& b );
};
void f( A& a ) {}
void B::f( B& b )
{
// Works because B
// has access to A.
::f( b );
}
int main()
{
B b;
// Does not work because A
// is not accessible.
f( b );
// Would not work if B::f()
// took an A& as its argument.
b.f( b );
return 0;
}
Now how does this fit into the philosophy of object oritented analysis and
design? I have no idea...
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Mon Dec 19, 2005 10:21 am Post subject: Re: What is protected inheritance? |
|
|
[email]hofmann (AT) anvil-soft (DOT) com[/email] (Matthias Hofmann) wrote (abridged):
| Quote: | The interesting part about protected inheritance is that contrary to
private inheritance, there is a conversion from the derived class to
the base class if the conversion is done in the derived class.
[...]
Now how does this fit into the philosophy of object oritented analysis
and design? I have no idea...
|
One way to think of it is that in order to re-implement a derived class
that does the same stuff as a base class, it needs to have the same access
rights as the base class. If the base class has a conversion to its base,
the derived class needs that conversion available too.
This is the philosophy followed by languages like Eiffel and Smalltalk. It
leads to everything being protected rather than private, and protected
inheritance is a natural consequence. C++ does not impose this philosophy,
but it does support it.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|