1

I know that when creating derived classes in C++ a structure like this:

class A {
    ...
}
class B: public A {
    ...
}

Would mean that there is a relation ship between A and B such that B "is a" A. I am wondering what if I wanted an A-B relationship such that B has a A not that B is a A.

My objective is to write a code that computes wave-function and corresponding properties of that wave-function for a particular molecule. I want my molecule to have a wave-function object. Can I i just create the molecule and wave-function classes separately, and then call the wave-function constructor in the molecule constructor?

class Molecule {
      Molecule(...);

      Molecule::Molecule(...)
      {
         wavefunction WF(...);
      }
};

IF i can do this is it the correct way to create this has-a relationship? How would I access the wave-function object? Like this:

mol.WF

assuming I had created a molecule with the name mol. This is my first whack at a program with more than one custom class and I am confusing myself very fast.

Ajay
  • 407
  • 4
  • 14
  • 2
    I think what you want to use is [composition](http://www.learncpp.com/cpp-tutorial/102-composition/) – Mariano D'Ascanio Jul 09 '14 at 19:14
  • Not use inheritance for this. Inheritance is only for `is as`, and is frowned upon because imposes tighter coupling between classes. Use composition instead (check for "inheritance vs composition" for a larger and better explanation). – SJuan76 Jul 09 '14 at 19:15
  • Private inheritance anyone? – Deduplicator Jul 09 '14 at 19:17
  • 2
    You may want to pick up an [introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) so you can get a handle on how various programming idioms are expressed in C++ and how you can use them to your advantage. – In silico Jul 09 '14 at 19:19
  • @Insilico I pickd up a copy of _C++_ _Primer_ from a coworker thank you for linking that post. I see that the post has been put on hold for being unclear. I was trying to ask if composition was possible, but I didn't know what that was So i had a hard time getting my point across. I marked cameron's post as the answer because It came with a detailed example that was very helpful for understanding the concept. – Ajay Jul 10 '14 at 15:18
  • @Deduplicator I was not sure if private inheritance was the best way to go here because there are elements of molecule that would not apply to wave-function. If I was to use private inheritance the wave-function would still inherit all of the molecule's elements, no? – Ajay Jul 10 '14 at 15:22

3 Answers3

4

I'm not sure I understand, but why don't simply declare a variable of type wavefunction in Molecule ?

class Molecule {
public:
      wavefunction wf;

      //...
};

Then, you can init it in constructor and manipulate it as a variable of Molecule. You can use it from outside the cass with mol.wf

This is called composition and this is what is used for "has a" relationship.

Edit: At the VERY last, you could use private inheritance, but composition by adding a field is much more practical and should be used instead almost always. And it would not be possible to use the wf fields outside of Molecule.

Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
  • I think the OP's question is how to express a "has-a" relationship instead of a "is-a" relationship in C++. As you've answered, this is done by declaring a member variable in the class. – In silico Jul 09 '14 at 19:16
3

Yes, this is possible. It looks like all you're missing is a simple example:

class Molecule {
public:
    wavefunction WF;    // This is a member variable; each Molecule will
                        // "have a" wavefunction named WF

    // When the constructor is called, the constructor of all member
    // variables is called too. We can explicitly specify arguments to
    // them using the 'initializer-list' (the part after ':')
    Molecule()
        : WF(/* constructor arguments... */)
    {
        // Here inside the constructor (and all methods of this class)
        // you can refer to WF directly or via 'this' (the implicit pointer
        // to the current molecule's instance):
        WF.foo();
        this->WF.foo();
    }
};

You can then refer to the WF through its Molecule's instance like so:

Molecule m;
m.WF.foo();
Cameron
  • 96,106
  • 25
  • 196
  • 225
2

You can implement a has a relationship by making the type a member of the type that has it.

class A {
    ...
}
class B {
    A a;
    ...
}
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466