Next:
Polymorphism
Up:
Inheritance and polymorphism
Previous:
Nested inheritance and hiding
Contents
Inheritance vs. embedding
Consider example of a salary object:
class Salary {
Salary();
Salary();
void raise(double newSalary);
...
};
Might think of deriving
Employee
from
Salary
so that we can say
theEmployee.raise();
to raise the employee's salary
Technically, nothing wrong
Architecturally, very bad decision!
Rule of thumb:
derive B from A only if B can be considered as an A
In this case, better embed a
Salary
object as a data field of the
Employee
class
Leo Liberti 2008-01-12