10.4 — Association

In the previous two lessons, we’ve looked at two types of object composition, composition and aggregation. Object composition is used to model relationships where a complex object is built from one or more simpler objects (parts).

In this lesson, we’ll take a look at a weaker type of relationship between two otherwise unrelated objects, called an association. Unlike object composition relationships, in an association, there is no implied whole/part relationship.


To qualify as an association, an object and another object must have the following relationship:

  • The associated object (member) is otherwise unrelated to the object (class)
  • The associated object (member) can belong to more than one object (class) at a time
  • The associated object (member) does not have its existence managed by the object (class)
  • The associated object (member) may or may not know about the existence of the object (class)

Unlike a composition or aggregation, where the part is a part of the whole object, in an association, the associated object is otherwise unrelated to the object. Just like an aggregation, the associated object can belong to multiple objects simultaneously, and isn’t managed by those objects. However, unlike an aggregation, where the relationship is always unidirectional, in an association, the relationship may be unidirectional or bidirectional (where the two objects are aware of each other).

The relationship between doctors and patients is a great example of an association. The doctor clearly has a relationship with his patients, but conceptually it’s not a part/whole (object composition) relationship. A doctor can see many patients in a day, and a patient can see many doctors (perhaps they want a second opinion, or they are visiting different types of doctors). Neither of the object’s lifespans are tied to the other.

We can say that association models as “uses-a” relationship. The doctor “uses” the patient (to earn income). The patient uses the doctor (for whatever health purposes they need).

Implementing associations

Because associations are a broad type of relationship, they can be implemented in many different ways.

In this example, we’ll implement a bi-directional Doctor/Patient relationship, since it makes sense for the Doctors to know who their Patients are, and vice-versa.

This prints:

James is seeing patients: Dave
Scott is seeing patients: Dave Betsy
Dave is seeing doctors: James Scott
Frank has no doctors right now
Betsy is seeing doctors: Scott

In general, you should avoid bidirectional associations if a unidirectional one will do, as they add complexity and tend to be harder to write without making errors.

Reflexive association

Sometimes objects may have a relationship with other objects of the same type. This is called a reflexive association. A good example of a reflexive association is the relationship between a university course and its prerequisites (which are also university courses).

Consider the simplified case where a Course can only have prerequisite. We can do something like this:

This can lead to a chain of associations (a course has a prerequisite, which has a prerequisite, etc…)

Associations can be indirect

In all of the above cases, we’ve used a pointer to directly link objects together. However, in an association, this is not strictly required. Any kind of data that allows you to link two objects together suffices. In the following example, we show how a Driver class can have a unidirectional association with a Car without actually including a Car pointer member:

In the above example, we have a CarLot holding our cars. The Driver, who needs a car, doesn’t have a pointer to his Car -- instead, he has the ID of the car, which we can use to get the Car from the CarLot when we need it.

In this particular example, doing things this way is kind of silly, since getting the Car out of the CarLot requires an inefficient lookup (a pointer connecting the two is much faster). However, there are advantages to referencing things by a unique ID instead of a pointer. For example, you can reference things that are not currently in memory (maybe they’re in a file, or in a database, and can be loaded on demand). Also, pointers can take 4 or 8 bytes -- if space is at a premium and the number of unique objects is fairly low, referencing them by an 8-bit or 16-bit integer can save lots of memory.

Composition vs aggregation vs association summary

Here’s a summary table to help you remember the difference between composition, aggregation, and association:

Property Composition Aggregation Association
Relationship type Whole/part Whole/part Otherwise unrelated
Members can belong to multiple classes No Yes Yes
Members existence managed by class Yes No No
Directionality Unidirectional Unidirectional Unidirectional or bidirectional
Relationship verb Part-of Has-a Uses-a
10.5 -- Dependencies
10.3 -- Aggregation

28 comments to 10.4 — Association

  • Omri

    "We’ll implement this function below Doctor since we need Doctor to be defined at that point."
    "We’ll *define* this function below Doctor *definition* since we need Doctor to be defined *already for this function to be successfully defined*."

  • Omri

    "They should use Doctor::addPatient() instead, which is publicly exposed"
    Accurate but does not explicitely expose to the "unseasoned"  the association scheme to be used.
    Consider something of the sort:
    "We plan the association patient-doctor to occur at the same place where the association doctor-patient occurs. Thus when Doctor::addPatient(…) will be launched, it will launch Patient::addDoctor(…) appropriately so that the two associations will be properly implemented. For this scheme to work Patient::addDoctor(…) needs to be visible to Doctor objects (only, thus not being public) as will be arranged for below through appropriate befriending."
    Is this correct?


    Alex Bro I love you. I learned a lot from this site. thank you senpai

  • Moj

    Alex would you please explain how we might use that Course/prerequisite example? I’m interested in its design… It has no string members for example!

    • Alex

      It probably makes more sense for each course to have a name (or numeric id) so we can differentiate them. I’ve updated the example to include a name member for each course.

  • Johnbosco

    please I need clarification on how the following lines of codes executes

    please I can’t move on without understanding the above. Thanks in advance

    • Alex

      The data looks like this:

      d1->m_patient = [“Dave”]
      d2->m_patient = [“Dave”, “Besty”]
      p1->m_doctor = [“James”, “Scott”]
      p2->m_doctor = []
      p3->m_doctor = [“Scott”]

      The code you pasted is from a Doctor member function, where doc is set to whichever doc we called (*d1 or *d2). It looks through all of the patients for that Doctor and prints their names.

      The Patient code works similarly, but iterates through each Patient’s Doctors.

  • Christopher

    In the examples above (and in the previous chapter), why do we need to use pointers for some of the objects?

  • Anonymous

    Hi, I have a question. On line 82 how come you do not include the Class Name Patient to the overloaded operator << like the way you did the

    Is it not required in this context - if not how come?

  • Daniel

    Grammar fix (plural doctors doesn’t quite make sense):

    Change "The relationship between a doctors and patients is a great example of an association."

    To: "The relationship between a doctor and its patients is a great example of an association."

  • Hugh Mungus

    Is there a reason you used a normal for loop rather than a for each loop?

  • Alex

    Typo fixed, and good idea. Lesson updated.

  • Tyler

    Under the "Reflexive Association" header, in the first sentence you accidentally called it "reflective" association. Great tutorials, thanks!

    Also, in the indirect association example (cars in a lot) it may be more clear to say:


    …rather than hard-coding the “17” again, to demonstrate how the Driver object is associated with the Car object.

  • Surya

    Hi Alex, I’m so lucky to find ur tutorials.
    I wanted to know if there is any difference between aggregation and association other than the direction.

    • Alex

      In terms of how aggregation and associations are implemented, there’s usually little difference in C++. The differences between the two are mostly conceptual.

  • Gajendra Gulgulia

    Hi Alex,

    Can you please explain the syntax in line 28 of the Doctor-Paitent code :
          std::string getName() const

    i cannot seem to understand the role of ‘const’ at the end of function getName()

    thanks in advance

    • Alex

      Const in this context means getName() is a const member function -- that is, getName() promises not to modify any of the member variables, or call any non-const functions.

      Const objects can only call const member functions (and constructors/destructors).

  • Bing

    Hello Alex

    First of all, thank you for your generosity on sharing your tremendous knowledge of c++. There is one thing, regarding the DOCTOR and PATIENT program, I am not sure about. I copied and pasted part that I don’t know

    void addPatient(Patient *pat)
            // Our doctor will add this patient
            // and the patient will also add this doctor

    how does that pointer-to-member operator, -> , work here? because later you write like following in your program



    Can you explain the mechanism behind there with this pointer-to-member operator? Thank you!

  • Matt

    Typo at the beginning: "quality" should be "qualify" I think.

  • Matt

    Typo at the begininng, after the "Association" sub-heading, first sentence… I think "and" should be "an".

    Also, your overloaded operator<< functions for both Doctor and Patient use "std:cout" instead of "out".

  • Ahmed

    Great explanation , thank you :))

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter