http://qs321.pair.com?node_id=278382


in reply to Inheritance vs Delegation: pros and cons

It is a problem of semantics.

I use inheritance whenever the parent class is a generalization of the descending class. The example given in Class::Delegation is not well chosen. A car can't inherit from a wheel. A car contains wheels. In this case you use delegation.

The appropriate example would be a class vehicle, from which you inherit to create car, truck, tractor, and so on. In this case, it makes sense to use inheritance.

My rule of thumb is, if it makes sense to say derived class IS A parent class, you inherit, otherwise you include the other class inside yours.

It makes sense to say a car is a vehicle, but it does not make sense to say a car is a wheel. Somebody could say that "a car is a wheel, a brake, an engine, and so on," and inherit from all the components. But also a washing machine can have wheels, engines and brakes, but it is not a car. Thus, the inheritance paradigm doesn't apply, and forcing it in such a case can only create confusion.

 _  _ _  _  
(_|| | |(_|><
 _|