0

What should be the relationship between class ABC and KLM ? Whether it should be composition, aggregation or association or any other relationship.

#include<iostream>
using namespace std;
class ABC
{
    int x;
public:
    ABC() { cout<<"1\t"; }
    ~ABC() { cout<<"2\t"; }
};
class KLM
{
    int  y;
    ABC *O1;
public:
    KLM() { cout<<"3\t"; }
    ~KLM() { cout<<"4\t"; }
};
int main()
{
    KLM *O4=new KLM();
    ABC a1;
    delete(O4);

    return 0;
}
  • In respect of above question, I am confused whether above code implements Association relationship or not. – Harshit Singh Mar 15 '21 at 11:21
  • 1
    While the distinction between association, aggregation and composition is certainly relevant for this question, the focus seems to be the mapping of these concepts with implementation. The duplicate chosen does not address this very well. I suggest either to reopen the question or propose another duplicate – Christophe Mar 15 '21 at 12:29
  • @Christophe the dupe question definitely asks explicitly to explain in term of implementation. If you feel like the answers aren't as good as they could be, maybe you can add a better one? – Geert Bellekens Mar 15 '21 at 13:40
  • 1
    @GeertBellekens Indeed, after a second reading, I see that implementation is addressed. Unfortunately the first answer (with an incredible number of votes) explains it plain wrong, only adding to the confusion and participating to the spread of unnecessary aggregation. – Christophe Mar 15 '21 at 14:34

1 Answers1

1

Yes, there is at least an association:

  • the ABC pointer in KLM shows that there can be a link between instances of KLM and instances of ABC. Moreover the association is navigable from KLM to ABC.
  • The multiplicity is 0..1 at the ABC end of the association, since the pointer can point to one object at most. We can however suppose it could be nullptr, i.e. pointing to no object.

Now there is an issue in the code: the pointer KLM::O1 is not initialized. You should make sure it is nullptr if you do not explicitly assign it a value in the constructor.

We cannot know more about this relation, since we do not know how this pointer is assigned to point to ABC objects, e.g.:

  • Could two different KLM objects point to the same ABC? THis would exclude composition.
  • Does a KLM object itself create the ABC object and prevent its address from being disclosed? In this case we could suspect an UML composition.

Hint 1: You have a class with a pointer member. You should implement the rule of 3 (or 5) to avoid nasty memory management errors. If you show the implementation of the rule, we could tell with more certainty about composition or not.

Hint 2: You use c++11 tag. In modern C++, the use of smart pointers is preferred to raw pointers. In particular, unique_ptr suggests an exclusive ownership typical for composition. A shared_ptr suggests a shared ownership quite common with aggregation (but we could not rule out a simple association). A weak_ptr suggests absence of ownership and hence necessarily a simple association.

Remark: There is no one-to-one relation between code and model. The same code can be used to implement different models. And the same model can be implemented with different code. Design intent is not always caught in a code snippet. So, it's more about suggesting or excluding more or less strongly some possibilities rather than finding a unique reality.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • In view of above question, If we doesn't initialise the pointer ABC *O1 that is present inside class KLM, then can we say that there is no relationship between the two classes ABC and KLM i.e. can we say that above code implements no relationship between classes ABC and KLM? – Harshit Singh Mar 15 '21 at 12:35
  • 1
    @HarshitSingh I assumed that the code was just a minimal example extracted of something more complete. If however we consider the code above to be complete and accurate, then O1 is not initialized, is never changed nor accessed. So it's a useless pointer and you could indeed argue that there is no association at all in practice. In this case, you'd still have a dependency relation between both classes: KLM needs to know about ABC. (It can be sufficient to have a forward declaration of ABC without any internal details just to compile, but the fact is that KLM depends on ABC. – Christophe Mar 15 '21 at 12:45
  • Can you please elaborate how the above classes implement dependency relationship. – Harshit Singh Mar 15 '21 at 13:13
  • @HarshitSingh remove ABC class and KLM won’t compile. – Christophe Mar 15 '21 at 13:46
  • Can you please explain a bit more about the dependency and association as I am little bit confused about these two terms, as dependency is considered to be subset of association. So, in the above code if there is no association, then dependency should also not be there in the above code. – Harshit Singh Mar 15 '21 at 15:13
  • It’s not a subset: it’s a different thing. The association requires a semantic relationship that is expressed with a structural link between instances of both classes. The usage dependency says that one class may use instances of the other class (eg in principle as parameter or return type). – Christophe Mar 15 '21 at 15:58
  • In the above example as class KLM is not using any of the instances of class ABC, so can we say this a dependency between class KLM and class ABC? – Harshit Singh Mar 15 '21 at 16:20
  • 1
    @HarshitSingh yes. Now keep in mind that we made this statement, ignoring ptr based on our knowledge of a specific implementation. In real life this generally does not happen. Members are generally not unused. Moreover from a design perspective, in real life we would probably look at the interface (e.g. the declared members) and consider that the implementation of the functions should be free to evolve. So whether O1 gets initialized to point to a valid object or not is an implementation detail. I’d therefore still feel more comfortable saying there’s a (potential) association. – Christophe Mar 15 '21 at 17:50