5

I'm beginning to study OOAD and I'm having difficulty finding a C++ code example that'd illustrate how Association, Aggregation and Composition are implemented programmatically. (There are several posts everywhere but they relate to C# or java). I did find an example or two, but they all conflict with my instructor's instructions and I'm confused.

My understanding is that in:

  • Association: Foo has a pointer to Bar object as a data member
  • Aggregation: Foo has a pointer to Bar object and data of Bar is deep copied in that pointer.
  • Composition: Foo has a Bar object as data member.

And this is how I've implemented it:

//ASSOCIATION
class Bar
{
    Baz baz;
};
class Foo
{
    Bar* bar;
    void setBar(Bar* _bar)
    {
        bar=_bar;
    }
};

//AGGREGATION
class Bar
{
    Baz baz;
};
class Foo
{
    Bar* bar;
    void setBar(Bar* _bar)
    {
        bar = new Bar;
        bar->baz=_bar->baz;
    }
};


//COMPOSTION
class Bar
{
    Baz baz;
};
class Foo
{
    Bar bar;
    Foo(Baz baz)
    {
        bar.baz=baz;
    }
};

Is this correct? If not, then how should it be done instead? It'd be appreciated if you also give me a reference of a code from a book (so that I can discuss with my instructor)

closed as too broad by gnat, user40980, GlenH7, Ampt, jwenting Sep 18 '14 at 12:42

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    You might want to talk with your instructor about how you perceive things to be in conflict. Yes, I realize you are looking for a book reference, but that doesn't work out well here. The core of the question touches on issues of How do I explain ${something} to ${someone}? Part of the challenge is you need to sit down with your prof and work with him or her the complete and full meaning of the topic, and then figure out where it is in conflict with other understandings. Trying to figure out with a partial understanding from each... – user40980 Sep 11 '14 at 14:23
  • source will only lead to more confusion. The resources you have discussing this with your prof (being able to sit down, with a whiteboard, and work through the confusion) are much beyond what we have here on the interwebs and will always be a poor substitute for the face to face communication that is possible in a college environment. – user40980 Sep 11 '14 at 14:24
5

There are multiple ways to map the OO concepts of association, aggregation and composition to C++ code. This is especially true for aggregation, because there is not even a consensus what it exactly means.

Your mapping suggests the following semantics, which are not wrong:

  • Association: Foo has a pointer to Bar object as a data member, without managing the Bar object -> Foo knows about Bar
  • Composition: Foo has a Bar object as data member -> Foo contains a Bar. It can't exist without it.
  • Aggregation: Foo has a pointer to Bar object and manages the lifetime of that object -> Foo contains a Bar, but can also exist without it.

As stated, aggregation is the difficult one here because, even within UML, the meaning of aggregation is not crystal clear. Another possible meaning of aggregation is "Foo contains a Bar object that is shared with other objects." This would typically be represented in C++ code by means of a std::shared_pointer or boost::shared_pointer.
Which meaning your instructor attaches to aggregation must be discussed with him.

  • In aggregation it manages the lifetime of that object, however it is shared with other objects ? Isn't it association where the object can be shared ? – Othman Benchekroun May 20 '15 at 12:38
  • @Othman: With association, objects can be shared, but aggregation is sometimes used to indicate a shared ownership of objects. – Bart van Ingen Schenau May 20 '15 at 13:20
  • @Schenau When it's a Shared ownership, who manages the lifetime of the object ? – Othman Benchekroun May 20 '15 at 13:51
  • @Othman: In shared ownership, the group of objects that owns another object is collectively responsible for managing the lifetime. Typically, this is done through reference counting (a count is kept how many references there are to an object and when the last reference is about to be removed, the object is destroyed). – Bart van Ingen Schenau May 20 '15 at 16:39
  • @Schenau What is it called when Foo has a pointer to Bar object as data member that is not shared with other objects, and manages its lifetime. I know it's stupid since composition is better in this case, but does it have a name ? – Othman Benchekroun May 21 '15 at 9:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.