What is the difference between inheritance and composition?
Inheritance expresses a is-a
relationship, while composition expresses a has-a
relationship between the two classes.
An example for composition is a polygon. It has a ordered sequence of Points. In C++ terms:
struct Polygon {
std::vector<Point> points;
};
While an logic_error
is a exception
:
struct logic_error : public exception {
};