Relationships between classes
What you will learn on this page
Section titled “What you will learn on this page”- The five common relationships between classes, from weakest to strongest.
- What each relationship says about lifetime: who creates the other object, and who destroys it.
- How each relationship is written in C++: a parameter, a pointer or reference member, a container, a value member, or a base class.
- Why the same C++ code can express different relationships, and how to make the intent visible.
Why relationships matter
Section titled “Why relationships matter”The responsibilities lesson asked what each object knows and does. The next question is how objects are connected. Every connection answers questions that C++ makes you decide:
- Does one object own the other, or only refer to it?
- Can the other object be destroyed first? If so, what happens to the reference?
- Is the connection for one function call, or for as long as both objects exist?
Getting these wrong produces the bugs the C++ lessons warned about: a
dangling reference to an object that was destroyed, two objects that both think they own a third, or an object that is destroyed too early. Naming the relationship first makes the right C++ type much easier to choose.
The five relationships
Section titled “The five relationships”From weakest to strongest:
| Relationship | In words | Lifetime | Typical C++ |
|---|---|---|---|
| Dependency | A uses B, briefly. | Not connected. A keeps nothing. | A function parameter or local variable. |
| Association | A knows B, for as long as A exists. | Independent. B must outlive A, or A must cope. | A non-owning pointer or reference member, or B’s ID. |
| Aggregation | A groups B’s. The B’s exist without A. | Independent. B’s are owned elsewhere. | A container of non-owning pointers, or of IDs. |
| Composition | A is made of B’s. They belong to A alone. | B’s are created with or by A and die with A. | A value member, a container of values, or a std::unique_ptr. |
| Inheritance | B is a kind of A. | Not a link between objects, but between types. | A public base class, usually with virtual functions. |
The example below shows all five in one small course catalog.
The example: a course catalog
Section titled “The example: a course catalog”A university department runs courses. Each course has a lead instructor, weekly sessions, and assessments (exams and projects). The department publishes a timetable of its courses.
Composition: a course and its sessions
Section titled “Composition: a course and its sessions”VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// A value that is part of one course: a weekly time slot, such as "Mon at// 9 in room B12". It is a plain struct: three public fields and no rules,// because any weekday, hour, and room make a sensible slot here. A course// keeps its sessions by value, so each Session lives inside its course and// is destroyed with it. Callers build one with braces, in field order:// Session{"Mon", 9, "B12"}.struct Session { std::string weekday; // a short day name, such as "Mon" int start_hour = 9; // 24-hour clock; 9 if a caller leaves it out std::string room;};A Session (“Monday at 9, room B12”) is part of one course. It means nothing
without the course, it is created when the course’s timetable is set, and it is
destroyed with the course. That is composition, and the C++ is the simplest
possible: the course stores its sessions by value, in a
std::vector<Session>. No pointer is involved, so nothing can dangle, and the
course’s destructor cleans up the sessions with no code at all.
Inheritance and composition: assessments
Section titled “Inheritance and composition: assessments”VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// Inheritance: an exam and a project are both assessments. They differ in// what they store and how they describe themselves; the course treats them// alike.//// Assessment is an interface (an abstract base class): it says what every// assessment can do, and leaves how to the derived classes. `= 0` after a// virtual function makes it "pure": Assessment has no body for it, so no// plain Assessment object can be created, only an Exam or a Project.class Assessment {public: // Virtual, so that destroying an Exam through a pointer to Assessment // (as the course's unique_ptrs do) runs Exam's destructor too. virtual ~Assessment() = default; // How much this assessment counts towards the final mark, in percent. [[nodiscard]] virtual int weight_percent() const = 0; // One line of text for a person to read, such as "exam (60 minutes, 70%)". [[nodiscard]] virtual std::string describe() const = 0;
protected: // Protected, not public: only derived classes may construct, copy, or // assign the Assessment part. Outside code therefore cannot copy an // Exam through an Assessment&, which would copy only the base part // (object slicing). Assessment() = default; Assessment(const Assessment&) = default; Assessment& operator=(const Assessment&) = default;};
// An exam: how long it lasts and how much it counts. `final` means no class// may derive from Exam. `override` (below) asks the compiler to check that// each function really replaces a virtual function of Assessment.class Exam final : public Assessment {public: // Stores both numbers as given; this example does not check them. Exam(int minutes, int weight_percent) : minutes_(minutes), weight_(weight_percent) {} [[nodiscard]] int weight_percent() const override { return weight_; } [[nodiscard]] std::string describe() const override;
private: int minutes_; int weight_;};
// A project: the week it is due and how much it counts. Same shape as Exam,// different data, and a different describe().class Project final : public Assessment {public: Project(int due_week, int weight_percent) : due_week_(due_week), weight_(weight_percent) {} [[nodiscard]] int weight_percent() const override { return weight_; } [[nodiscard]] std::string describe() const override;
private: int due_week_; int weight_;};An exam and a project are both kinds of assessment: each has a weight, and
each can describe itself, but each stores different data. That is
inheritance: Exam and Project derive publicly from Assessment, whose
virtual functions let the course treat them alike. The base class has a public
virtual destructor, because assessments will be
destroyed through a pointer to the base. Its copy operations are protected, so that code outside
the hierarchy cannot copy or assign through an Assessment&, which would copy only the base part of
an Exam or a Project (object slicing).
Assessments are still composed into a course: each belongs to one course and
dies with it. Because they are of different types, the course cannot store them
by value in one vector. It stores std::unique_ptr<Assessment>, which keeps
the composition (one owner, destroyed with the course) and allows each element
to be a different derived type.
The course: composition and association
Section titled “The course: composition and association”VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// One course, such as CS101. It shows three relationships at once:// - composition: it owns its sessions (by value) and its assessments// (through unique_ptr), and destroys them when it is destroyed;// - association: it refers to its lead Instructor through a pointer it// does not own, so the Instructor must outlive the Course;// - it can be moved but not copied, because two courses must not own the// same assessments.// add_assessment throws std::invalid_argument for a null pointer.class Course {public: // Association: the course refers to its lead, who must outlive it. // code and title are taken by value and moved into the members (one // copy at most, made by the caller). `lead_(&lead)` stores the address // of the instructor: a reference cannot be null, so the pointer is not. Course(std::string code, std::string title, const Instructor& lead) : code_(std::move(code)), title_(std::move(title)), lead_(&lead) {}
// Composition through unique_ptr: two courses must never own the same // assessments, so copying is deleted; moving hands everything over. // (Deleted explicitly: std::vector<std::unique_ptr<T>> claims to be // copyable to type traits, and only fails when the copy is compiled.) Course(const Course&) = delete; Course& operator=(const Course&) = delete; // `= default` asks the compiler to write the usual move: each member is // moved, so the vectors (and the unique_ptrs in them) change hands // without copying. noexcept promises the move cannot throw. Course(Course&&) noexcept = default; Course& operator=(Course&&) noexcept = default; ~Course() = default;
[[nodiscard]] const std::string& code() const noexcept { return code_; } [[nodiscard]] const std::string& title() const noexcept { return title_; } // `*lead_` follows the pointer back to the Instructor object itself. [[nodiscard]] const Instructor& lead() const noexcept { return *lead_; } // Points the course at a different instructor. Neither instructor is // created, copied, or destroyed: only the stored address changes. void change_lead(const Instructor& lead) noexcept { lead_ = &lead; }
// Composition: sessions and assessments are created for this course, // stored inside it, and destroyed with it. // The Session is taken by value (the caller's braces build it right // here) and then moved into the vector, so no second copy is made. void add_session(Session session) { sessions_.push_back(std::move(session)); } // Takes ownership of one assessment. The caller passes a unique_ptr // (usually from std::make_unique), so after the call the course is its // only owner. Throws std::invalid_argument if the pointer is null. void add_assessment(std::unique_ptr<Assessment> assessment);
[[nodiscard]] const std::vector<Session>& sessions() const noexcept { return sessions_; } // The weights of all assessments added together (0 if there are none). [[nodiscard]] int total_weight_percent() const; // One describe() line per assessment, in the order they were added. [[nodiscard]] std::vector<std::string> assessment_descriptions() const;
private: std::string code_; std::string title_; const Instructor* lead_; // never null; not owned std::vector<Session> sessions_; // stored by value: owned, no pointers // Each element is a unique_ptr to a base class, so one vector can hold // Exams and Projects side by side. When the course is destroyed, the // vector destroys each unique_ptr, which deletes its assessment. std::vector<std::unique_ptr<Assessment>> assessments_;};VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// Takes ownership of one assessment. A unique_ptr cannot be copied, only// moved, so the caller must hand theirs over (std::make_unique gives a// temporary that moves in by itself). A null pointer is refused before// anything changes, so the course never stores an empty slot.void Course::add_assessment(std::unique_ptr<Assessment> assessment) { // `!assessment` is true when the unique_ptr holds nothing (nullptr). if (!assessment) { throw std::invalid_argument("Course::add_assessment: null assessment"); } // std::move passes ownership on again, from the parameter into the // vector. The parameter is left empty and is destroyed at the end of // the function without deleting anything. assessments_.push_back(std::move(assessment));}
// Adds up the weights. The loop sees each element as a unique_ptr, and// `const auto&` looks at it in place: a unique_ptr cannot be copied, so a// plain `auto` here would not compile.int Course::total_weight_percent() const { int total = 0; for (const auto& assessment : assessments_) { // `->` calls through the pointer. weight_percent is virtual, so the // call reaches Exam's or Project's version, whichever this one is. total += assessment->weight_percent(); // virtual call: Exam or Project } return total;}The course owns its sessions and assessments. It does not own its lead instructor. An instructor is hired before any course exists, may lead several courses, and stays employed when a course ends. That is an association: the course knows its instructor for as long as the course exists, but their lifetimes are independent.
In C++, the course holds a const Instructor*, a non-owning pointer:
- Non-owning means the course never deletes the instructor. The header comment states the rule that makes the pointer safe: the instructor must outlive the course.
- The constructor takes a reference (
const Instructor&), so a caller cannot pass a null pointer, and stores its address. The pointer is never null, which the member’s comment records. - It is a pointer rather than a reference member because a reference cannot
be changed to refer to something else.
change_leadneeds to point the course at a different instructor. A reference member would also stop the course from being assigned to.
Copying a composition
Section titled “Copying a composition”Composition through std::unique_ptr has a consequence: a course cannot be
copied, because two courses would then own the same assessments. The course
says so by deleting its copy operations and defaulting its moves. A move hands
the sessions and assessments to the new course. The old course is left valid,
but should not be used again except to assign it a new value or destroy it.
Aggregation and dependency: the department
Section titled “Aggregation and dependency: the department”VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// A department, such as "Computing", that publishes a timetable of the// courses it lists. It shows the two weakest relationships:// - aggregation: it keeps pointers to courses it does not own, so every// listed course must outlive the department;// - dependency: it uses an output stream during one call and keeps nothing.class Department {public: // explicit: a plain string cannot silently turn into a Department. explicit Department(std::string name) : name_(std::move(name)) {}
// Aggregation: the department groups courses, but they are created, // stored, and destroyed elsewhere. A course could be listed by two // departments, or by none. // `&course` is the address of the caller's Course: the department stores // where the course is, not a copy of it. Nothing stops the same course // being listed twice; this example does not check. void list(const Course& course) { courses_.push_back(&course); } [[nodiscard]] std::size_t course_count() const noexcept { return courses_.size(); }
// Dependency: the department uses an output stream for the length of // this call, and keeps nothing. // Writes a heading, then one line per session of each listed course, in // the order the courses were listed, for example // " Mon 9:00 CS101 in B12 (R. Patel)". Taking std::ostream& means the // caller chooses where the text goes: std::cout, or a string stream in // a test. void print_timetable(std::ostream& out) const;
private: std::string name_; std::vector<const Course*> courses_; // never null; not owned};A department groups courses, but it does not create or destroy them. The
same course could be listed by two departments (a joint course), or by none for
a while. That is aggregation: a whole made of parts that exist without it.
In C++, the department holds a std::vector<const Course*>. It looks exactly
like association, with many objects instead of one, and the same rule applies:
every listed course must outlive the department.
print_timetable takes a std::ostream& for the length of one call, writes to
it, and keeps nothing. That is a dependency, the weakest relationship. The
department needs std::ostream to compile, but no Department object is ever
connected to a stream.
Putting it together
Section titled “Putting it together”VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// Declared first, destroyed last: courses refer to instructors, and the// department refers to courses.const Instructor patel{"R. Patel"};const Instructor okafor{"J. Okafor"};
// `patel` is passed by reference: the course stores its address.Course cpp{"CS101", "Programming in C++", patel};// Each {...} builds a Session in place: weekday, start hour, room.cpp.add_session({"Mon", 9, "B12"});cpp.add_session({"Thu", 14, "B12"});// std::make_unique<Exam>(120, 60) creates an Exam (120 minutes, worth// 60%) on the heap and returns the unique_ptr that owns it; the course// then takes that ownership.cpp.add_assessment(std::make_unique<Exam>(120, 60));cpp.add_assessment(std::make_unique<Project>(10, 40));
Course data{"CS205", "Data Structures", okafor};data.add_session({"Tue", 11, "C3"});data.add_assessment(std::make_unique<Exam>(90, 100));
// The department only records where cpp and data are: it owns neither.Department computing{"Computing"};computing.list(cpp);computing.list(data);computing.print_timetable(std::cout);
// assessment_descriptions() returns a new vector; the range-for keeps it// alive until the loop ends.for (const auto& text : cpp.assessment_descriptions()) { std::cout << "CS101 assessment: " << text << '\n';}std::cout << "CS101 weights add up to " << cpp.total_weight_percent() << "%\n";
cpp.change_lead(okafor); // the association changes; neither object is destroyedstd::cout << "CS101 is now led by " << cpp.lead().name() << '\n';// When main returns, the locals are destroyed in reverse order:// computing (which deletes nothing it lists), then data and cpp (each// deleting its own assessments), then okafor and patel. No pointer is// ever left pointing at an object that is already gone.Computing timetable Mon 9:00 CS101 in B12 (R. Patel) Thu 14:00 CS101 in B12 (R. Patel) Tue 11:00 CS205 in C3 (J. Okafor)CS101 assessment: exam (120 minutes, 60%)CS101 assessment: project (due week 10, 40%)CS101 weights add up to 100%CS101 is now led by J. Okaformain owns everything, as local variables. The order of declaration is the
ownership plan: instructors first, because courses refer to them; courses next,
because the department refers to them; the department last. Local variables are
destroyed in reverse order, so each object is destroyed before anything it
refers to.
The relationships in one diagram
Section titled “The relationships in one diagram”Text description of the diagram
Eight boxes. A dashed arrow from Department to Course, labelled “lists 0..* (aggregation)”: a
department lists courses it does not own. A dashed arrow from Course to Instructor, labelled
“knows 1 lead (association)”: a course refers to its lead. Solid arrows from Course to Session and
from Course to Assessment, each labelled “owns 0..* (composition)”: a course owns them, and they
die with it. Thick arrows from Exam and from Project to Assessment, labelled “is a (inheritance)”.
A dashed arrow from Department to ostream, labelled “uses while printing (dependency)”: a
department uses a stream only during print_timetable.
Diagram source (Mermaid)
flowchart LR
Department -.->|"lists 0..* (aggregation)"| Course
Course -.->|"knows 1 lead (association)"| Instructor
Course -->|"owns 0..* (composition)"| Session
Course -->|"owns 0..* (composition)"| Assessment
Exam ==>|"is a (inheritance)"| Assessment
Project ==>|"is a (inheritance)"| Assessment
Department -.->|"uses while printing (dependency)"| ostreamHow to read the sketch: a solid arrow means the class at the tail owns
the one at the head (composition); a dashed arrow means it refers to or
uses the other without owning it (aggregation, association, or dependency,
named in the label); a thick arrow means “is a kind of” (inheritance).
The ostream box is drawn here only to show what a dependency looks like:
entity sketches usually leave out standard-library types.
The lesson on drawing class designs covers this notation fully, and shows the
same catalog as a class outline and in UML.
Testing relationships
Section titled “Testing relationships”VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).
// LLD_TEST(name) defines one test. LLD_CHECK_EQ(actual, expected) records a// failure unless the two are equal, and prints both.// LLD_CHECK_THROWS(expression, type) fails unless the expression throws that// type of exception. They come from the site's small test header,// lld_test.hpp.
// Composition through unique_ptr: a course cannot be copied, but it can be// moved.// static_assert is checked by the compiler, not when the tests run: if the// condition is false, this file does not compile. The std::is_..._v traits// ask "can a Course be copied?" and "can it be moved without throwing?".static_assert(!std::is_copy_constructible_v<Course>);static_assert(std::is_nothrow_move_constructible_v<Course>);
// Inheritance: an Exam worth 70% and a Project worth 30% go into one course,// which only sees them as Assessments. The total of 100 proves each virtual// weight_percent call reached the right class, and the second description// proves Project's own describe() ran.LLD_TEST(assessments_are_used_through_the_base_class) { const Instructor lead{"Lead"}; Course course{"C1", "Course", lead}; course.add_assessment(std::make_unique<Exam>(60, 70)); course.add_assessment(std::make_unique<Project>(5, 30)); LLD_CHECK_EQ(course.total_weight_percent(), 100); const auto descriptions = course.assessment_descriptions(); // .at(1) is the second element (counting from 0), with a bounds check. LLD_CHECK_EQ(descriptions.at(1), std::string{"project (due week 5, 30%)"});}
// The course refuses an empty unique_ptr: nullptr converts to one.LLD_TEST(a_course_refuses_a_null_assessment) { const Instructor lead{"Lead"}; Course course{"C1", "Course", lead}; LLD_CHECK_THROWS(course.add_assessment(nullptr), std::invalid_argument);}
// Association: re-pointing the course at a second instructor changes which// one it reports, and leaves the first instructor existing and unchanged.LLD_TEST(changing_the_lead_changes_only_the_association) { const Instructor first{"First"}; const Instructor second{"Second"}; Course course{"C1", "Course", first}; course.change_lead(second); LLD_CHECK_EQ(course.lead().name(), std::string{"Second"}); LLD_CHECK_EQ(first.name(), std::string{"First"}); // still exists, unchanged}
// Aggregation: one course is listed by two departments at once, which only// works because neither department owns it. The timetable is written into// a std::ostringstream (a stream that collects text in memory), so the// test can compare it, character for character, with the expected text.LLD_TEST(a_department_shows_courses_it_does_not_own) { const Instructor lead{"Lead"}; Course course{"C1", "Course", lead}; course.add_session({"Fri", 10, "A1"}); Department first{"First"}; Department second{"Second"}; first.list(course); second.list(course); // one course, two departments std::ostringstream out; second.print_timetable(out); LLD_CHECK_EQ(out.str(), std::string{"Second timetable\n Fri 10:00 C1 in A1 (Lead)\n"}); LLD_CHECK_EQ(first.course_count(), std::size_t{1});}Each test checks a promise that one relationship makes. The static_asserts
check composition: a course cannot be copied but can be moved, without throwing.
The assessment test checks inheritance: the course uses exams and projects
through the base class. The lead test checks that changing an association
destroys nothing. The department test checks aggregation: one course listed by
two departments.
One C++ shape, several meanings
Section titled “One C++ shape, several meanings”A const Course* member can mean association or aggregation; a
std::unique_ptr member is usually composition; a value member is usually
composition. The C++ type shows ownership. It does not always show
intent. Two things make the intent visible:
- A comment on the member that names the relationship and the lifetime rule,
as in
// never null; not owned, and in the header comments of this example. - The operations offered.
Course::change_leadsays the association can change. A course with no such function, and an instructor passed only to the constructor, would say “fixed for life”.
Many teams do not distinguish aggregation from association at all, and draw both as plain associations. That is a reasonable simplification. The question that matters in C++ is the same for both: who owns the other object, and what keeps it alive?
Other designs
Section titled “Other designs”IDs instead of pointers. The course could store an InstructorId, and look
the instructor up in a directory when needed. Nothing can dangle: if the
instructor leaves, the lookup fails and the code must handle that. That is the
better choice when lifetimes are long or unclear, when objects are stored in a
database, or when objects are moved around in containers. The cost is a lookup,
and a directory that every user of the course must reach. This example uses
pointers because every object lives in main and the lifetimes are obvious.
std::shared_ptr everywhere. Holding the instructor in a
std::shared_ptr would keep the instructor alive as
long as any course refers to it. That changes the meaning: the course now shares ownership of
the instructor, and an instructor who leaves the university stays in memory while a course still
points at them. Shared ownership is right when no single owner exists. It is rarely right for “knows
about”.
A std::variant instead of inheritance. The assessments could be
std::variant<Exam, Project>, stored by value with no unique_ptr and no
virtual functions. That fits a closed set of kinds that rarely changes. A base
class fits better when new kinds of assessment are expected, or when code
outside the course must add them. The composition-vs-inheritance lesson
compares the two.
Common mistakes
Section titled “Common mistakes”- Owning what should only be known. A course that holds its instructor by value has a private copy, which does not change when the real instructor’s details change.
std::shared_ptras a way to avoid thinking about lifetime. It hides the question instead of answering it, and can keep objects alive in cycles.- A raw owning pointer (
Assessment*created withnew) for composition. Use a value orstd::unique_ptr; then the destructor needs no code. - Inheritance for code reuse where there is no “is a kind of”. A
Coursethat inherited fromstd::vector<Session>to reusepush_backwould expose every vector operation as part of the course. - Unstated lifetime rules. A non-owning pointer or reference member with no comment saying what must outlive what.
When a requirement changes
Section titled “When a requirement changes”New requirement: “A course may be co-taught by several instructors, one of whom is the lead.”
The relationship kind does not change; it is still an association. Its
multiplicity changes, from exactly one instructor to one or more. The member
becomes a std::vector<const Instructor*>, the lead stays a separate pointer,
and the course gains a rule to protect: the lead must be one of the course’s
instructors. Removing an instructor who is the lead must be refused, or must
choose a new lead. The next lesson is about exactly this: how multiplicity
chooses the C++ type, and which rules come with it.
Exercise: Map relationships to C++ members
For each statement, name the relationship and write the member (or parameter) in C++. Say what must outlive what, if anything.
- A library branch has opening hours for each day of the week. The hours mean nothing without the branch.
- A loan records the member who borrowed the item. Members exist before the loan and long after it.
- A reading list collects titles from the catalog. Deleting the list does not delete any titles.
- A monthly report is produced from the loan ledger, and keeps no link to it.
- A notice is sent either by e-mail or by text message. The library expects to add other channels.
- A child’s membership may name one guardian, who is also a member.
Show the explained answer
- Composition.
std::array<OpeningHours, 7> hours_;as a value member, whereOpeningHoursis a small value type. The hours are created and destroyed with the branch. No lifetime rule is needed. - Association. A loan lasts weeks and is stored, so
MemberId member_;is the safest choice: nothing dangles if the member object is moved or reloaded. Aconst Member*would work only if every member object provably outlives every loan. - Aggregation.
std::vector<TitleId> titles_;, orstd::vector<const Title*>if the catalog’s titles stay in place for as long as the list exists. - Dependency. A function parameter:
MonthlyReport make_report(const LoanLedger& ledger, Month month);. The report needs the ledger only while it is being made. - Inheritance, with composition in whatever sends notices: a
Channelbase class with a virtualsend, derivedEmailChannelandSmsChannel, andstd::unique_ptr<Channel> channel_;in the sender. A base class fits because new channels are expected. (With a fixed set, astd::variantwould do.) - Association, zero or one.
std::optional<MemberId> guardian_;. “May name” means the guardian can be absent;std::optionalsays so in the type. A nullableconst Member*would also express “zero or one”, but would need the guardian’s object to outlive the child’s.
Notice how often the answer is an ID, not a pointer. Pointers are a good fit for objects that live together in one place, as in this page’s example. Stored, long-lived links between entities are usually IDs.
Summary
Section titled “Summary”- Dependency: uses briefly (a parameter). Association: knows (a non-owning
pointer, reference, or ID). Aggregation: groups parts that exist without it (a
container of pointers or IDs). Composition: is made of parts that die with it
(values or
std::unique_ptr). Inheritance: is a kind of (a base class). - Every relationship is a decision about ownership and lifetime. Choose the relationship first, then the C++ type.
- Write the lifetime rule next to every non-owning pointer or reference member.
- Prefer values for composition,
std::unique_ptrwhen the parts are polymorphic, and IDs for long-lived links between entities.