UML

Composition “owns-a”(C++)

Composition in C++ is defined as implementing complex objects using simpler or smaller ones.

Links

If A owns-a B, then generally:

  • B does not have an independent existence for A
  • If A dies, then B dies
  • If A is copied, B is copied as well (deep copy)

How is "owns-a" implemented?

Typically, this is implemented in C++ via object fields (or unique_ptr fields).

A Linked List is a “owns-a” relationship.

class List {
	struct Node; 
	Node* head = nullptr;
}

Inheritance is tightly coupled whereas composition is loosely coupled.

https://www.digitalocean.com/community/tutorials/composition-vs-inheritance