publicly/privately derived types

J

Jess

Hello,

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.

Thanks,
Jess
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hello,

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.

Read in the FAQ http://www.parashift.com/c++-faq-lite/ sections 19 and
24.

Notice that if you don't specify public, private or protected (as in
class B : A {}) the type will be private for classes and public for
structs.
 
J

James Kanze

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.
 
G

Gennaro Prota

The last two are equivalent; I (like most programmers, I think)
prefer being explicit, and using the last.

In fact, I wonder why it is possible to omit the access specifier at
all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top