Composition is a [[Programming]] Method in [[Object Oriented Programming|OOP]] in which an object is made up of common properties instead of inheriting those properties. It describes *"has a"* instead of *is a*.
A good example would be a Car. A car has many components, such as an Engine, Wheels, Fuel Tank, etc.
In Inheritance, you may try to model this like
```cpp
class car : public wheeled<4>, engine, fuel_tank {
// Code that glues this all together
};
```
This... doesn't make much sense. A car isn't *engined*, it *has an engine*.
Instead, you would represent this like so:
```cpp
class car {
engine m_engine;
fuel_tank m_tank;
wheel m_wheels[4];
};
```
## Enforcing a contract
Generally, classes are inherited to enforce a contract via [[Interface|interfaces]]. This is possible with composition, simply inherit from the interface and implement the contract by returning / using the components.
# References