Object Relationships - RJAE5/2143-OOP GitHub Wiki

Object Relationships

In C++ Object-Oriented Programming, object relationships refer to how different objects of classes interact with one another. The key types of relationships are:

Association

A broad relationship where one object is connected to another, but both can exist independently. For example, a Student class and a Course class can have an association, where a student is enrolled in a course, but both objects can exist independently.

This type of relationship typically does not always relate to the other object.

Aggregation

This is a special form of association that represents a "whole-part" relationship, where the part can exist independently of the whole. For instance, a Team class can aggregate Player objects, but players can exist without the team.

This can be described as the "Member-Of" relationship.

Dependency

This is a relationship where one object depends on another for functionality, often seen in function parameters or temporary relationships during method calls. For instance, a Printer class might depend on a Document object to print it.

This can be described as the "Depends-On" relationship.

Composition & Inheritance

See Composition.

See Inheritance.

Important Notes

  • Other than the widely used composition and inheritance, there are 3 main object relationships:
    • Association
    • Aggregation
    • Dependency
  • Object relationships help when designing robust and flexible systems where objects collaborate

Sources:

  1. https://www.learncpp.com/cpp-tutorial/object-relationships/
  2. https://medium.com/@humzakhalid94/understanding-object-oriented-relationships-inheritance-association-composition-and-aggregation-4d298494ac1c