+4  A: 

Yes, inheritance is exactly for "is-a" relationship. In the case of Name and Address it would be perhaps more elegant to have a composite class Person that has member variables of types Name and Address.

But for cases when indeed every entity of Address class also has the same properties as Name class inheritance is acceptable. Again it depends on how this will work in a real problem, it's not a general question.

sharptooth
+13  A: 

No, I do not think that address should inherit from Name. They have nothing in common, except for one string field. An address should not have a last name.

Inherritance should only be used when there is a strong and clear relationship where some behaviour is extended.

One should favour composition over inherritance, because it allows for loose coupling and dynamic change of behaviour.

Ikke
You can also use inheritance to reuse code, but that quickly generates deep hierarchies, which are a code smell. So I would NOT advise to do so.
Peter Kofler
+1 for composition in this case. Extending Name to become an Address is definitely wrong.
Joey
+5  A: 

In this case your book is abusing object orientation just to save having to redefine a few variables. Conceptually it just isn't true that Address "is-a" Name. It would have been more natural to have the Address object contain a reference to the Name object (the resident at the given Address).

Il-Bhima
+2  A: 

Yes. When your class B is-a class A, then you can use inheritance. That is, in such situations it is OK to let class B be a subclass of class A.

In situations where this relationship is not clear, then you should consider composition instead of inheritance. (has-a relationship instead of is-a relationship).

That is, the example in your book feels wrong. Adress is not a Name. Adress can have a name though, so composition should be used.

Frederik Gheysels
+2  A: 

If class B can be used where a class A is expected then you have "is-a" inheritance (see Liskov substitution principle). Otherwise, you just expand a class with new behaviors.

Address can inherit from Name class (I don't agree) but you shouldn't treat it as a Name.

Nick D
+2  A: 

In general: if class D inherits from class B, then D is a B.

In C++: if class D inherits privately from class B, then D is not a B since nobody knows. Stroustrup thinks of this as an alternative to composition. I would consider it one of the mistakes in the design of C++ precisely for the reason that in my understanding, "inherits" should be synonymous to "is a".

Tobias