title: C++ : Association, Aggregation and Composition
body: <p>I'm beginning to study OOAD and I'm having difficulty finding a <code>C++</code> code example that'd illustrate how <code>Association</code>, <code>Aggregation</code> and <code>Composition</code> 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.</p>
  <p>My understanding is that in:</p>
  <ul>
  <li><strong>Association:</strong> Foo has a pointer to Bar object as a data member</li>
  <li><strong>Aggregation:</strong> Foo has a pointer to Bar object and data of Bar is deep copied in that pointer.</li>
  <li><strong>Composition:</strong> Foo has a Bar object as data member.</li>
  </ul>
  <p>And this is how I've implemented it:</p>
  <pre><code>class Bar
  {
      Baz baz;
  };
  
  //ASSOCIATION (with Bar)
  class Foo
  {
      Bar* bar;
      void setBar(Bar* _bar)
      {
          bar = _bar;
      }
  };
  
  //AGGREGATION (with Bar)
  class Foo
  {
      Bar* bar;
      void setBar(Bar* _bar)
      {
          bar = new Bar;
          bar-&gt;baz = _bar-&gt;baz;
      }
  };
  
  
  //COMPOSTION (with Bar)
  class Foo
  {
      Bar bar;
      Foo(Baz baz)
      {
          bar.baz = baz;
      }
  };
  </code></pre>
  <p>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)</p>