Suppose there are two classes A and B:
class A {};
class B {};
In what aspects differ the two examples below?
Example 1:
class C : public A, public B {};
Example 2:
class C
{
//private
friend class A;
friend class B;
}
A friend
can touch the private
parts (pun only slightly intentional! ;) ) of whatever it is friend of, but nothing of A
and B
are part of C
- it just means that "A
and B
can touch C
's private bits"). Anything "less" than private
is of course also available to A
and B
, so if C
has protected
or public
members, that will also be available.
When you inherit, the A
and B
becomes part of C
. Any private
sections of A
and B
are not available to C
. In the "is-a" vs. "has-a" nomenclature, C
now is-a A
and is-a B
- in other words, it's inherited from A
, so it "behaves like A
from an interface perspective.