Re: publicly/privately derived types
On May 14, 8:57 am, Jess <w...@hotmail.com> wrote:
Can somebody please tell me the difference between the following?
class B : public A{...};
class B : A{....}
class B : private A{...}
I was told to use the first one, but I'm wondering what the other two
forms mean and when I should consider using them.
The last two are equivalent; I (like most programmers, I think)
prefer being explicit, and using the last.
Traditionally, one would say: use private inheritance for
inheritance of implementation, and public for inheritance of
interface. In the first case, for the users, B isA A, B
implements the interface (contract) of A, and a user can (or
should be able to) use a B where ever he could use an A. In the
case of private inheritance, the inheritance is an
implementation detail, which doesn't concern the user; a user of
B cannot use B where ever an A is expected.
Be aware, however, that this explination concerns inheritance
for the typical OO reasons. Derivation like the above is a C++
programming technique, which can be used for different reasons.
When it is used for OO inheritance (which is the most frequent
reason), the paragraph above holds. When it is used for some
other reason, it might not.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34