Main Index


Inheritance, Aggregation and Composition

Picture showing inheritance and composition.

Inheritance uses a relationship that can be formed with words similar to "is a".
For example a car is a vehicle, a lorry is a vehicle.

Aggregation and composition uses relationships that can be formed with words similar to "has a".
For example a vehicle has an engine.


While looking at some old C and C++ programs, I found that the structs could be drawn as class diagrams in UML showing composition and aggregation as shown in the following paragraphs.


Picture showing aggregation.

struct Shopping_Basket
   Item *itemPtr;


The shopping basket does not need to have items in it for it to be a shopping basket. Both shopping basket and item can exist independantly. The item pointer is shown with an open diamond. Here the shopping basket is like an array, e.g. Shopping_Basket[] which even when empty is still an array.



picture showing composition.

struct OrderForm
   orderLine linePtr;


An order form is not a form without lines. The order form gives instances of order lines. If an order line does not exist, then there is no order. The line pointer is shown with black filled in diamond.


(c) B V Wood.