What is the difference between holding a class object and aggregating it?

I can't tell the difference between aggregation and retention. What does this mean in terms of, say, C ++?
I suppose that when an object of class A stores (or instantiates) objects of class B, it uses it to perform some functions by itself. For example:

class A {
 int state;
 public: 
  A(int s): state(s) {}
  int inc() { return state++; }
};
class B {
  int app;
  string s;
public:
  B(): app(0), s("") {}
  B(int A, const string& str): app(A), s(str) {}
  void f(int p);
  ~B() { app = 0; s = ""; }
};
void B::f(int p)
{
   A mA(p);
   app = mA.inc();
}

      

And the aggregation of an object of class A will be like this:

class B{
 A t;
 //...
}

      

Please give me a link to a website or a place in a book where I can find clear definitions in OO terms of what exactly each kind of relationship between classes is.

0


source to share


3 answers


Holding on to another class means that the class is related to another class through a parent child relationship. For example, Path has a list of points. The path is the parent of the point list, which is the parent of the individual points.

Aggregation means using different classes and carrying their interface, so they appear as one class. For example, FileDialog will have multiple button classes, text input class, listview / treeview class, etc. But for the rest of the system, it just had methods to activate, perhaps assign a default filename and get the rest.



The fact that it is made up of all the other classes is irrelevant to other classes, using it like filedialog. However, it works by aggregating all classes to perform the expected behavior.

+1


source


IMO if I understood correctly you are asking for a composition definition versus aggregation. An aggregation or holding is a collection of objects. Composition has a tighter limitation. Think about the mean marks awarded to the student: when we calculate the mean, we cannot exclude any element. The composition is like this. No element can be skipped. On the other hand, aggregation is more loosely defined.

An interesting analogy would be pushing a quiver full of arrows and a car. Quiver is an Arrow Aggregation, a quiver can exist without arrows; A car is a composition (the sum of its parts). And let's not argue that a car without a wheel is still a car :)



As for the links: http://www.bletchleypark.net/algorithms/software/oop.html

+1


source


This is very much about ownership and visibility. There are different names for these relationships, but you should consider these two points:

  • Does the parent have strict ownership over the child, so that when the parent dies, the child must die too?

  • Is the child visible to objects other than the parent?

Answering these questions will help you clarify this relationship.

0


source







All Articles