collection, aggregate and composition

J

Jarek Blakarz

Hi

Could you please explain me the difference between collection, aggregate and
composition. For me all of them are the same.

thanks in advance
 
R

Rui Maciel

Jarek said:
Hi

Could you please explain me the difference between collection, aggregate
and composition. For me all of them are the same.

Collection refers to a set of data types, which don't necessarily have a
relationship among them or with the collection data type. An example would
be a container data type.

The meaning of the terms "aggregate" and "composition" depend on the
context. When used to express associations between classes, their meaning
differs essentially on how the life of the associated objects are managed.
More specifically, if an object of type A is composed by an object of type B
then if A's life ends then B's life will also end, whereas with an
association relationship A's life may end and B's life cycle won't be
affected by it.


Rui Maciel
 
J

Jarek Blakarz

Jarek Blakarz wrote:







Collection refers to a set of data types, which don't necessarily have a

relationship among them or with the collection data type. An example would

be a container data type.

Is an ordinary C++ array "int elems[10]" a collection ?
 
R

Rui Maciel

Jarek said:
Is an ordinary C++ array "int elems[10]" a collection ?

Yes, it is. Or at least it is supposed to be. It appears that some people
argue that a fixed-size container is not a collection, and instead it is
supposed to be a composition. That would mean that data structures such as
tuples, arrays, fixed-size sets and lists, and even some aggregate types
wouldn't be collections. Nevertheless, the C++ standard defines tuples as
"heterogeneous, fixed-size collections of values", which backs up the
assertion that tuples, and by induction the other data types I've mentioned,
are collections.


Rui Maciel
 
R

ralph

Is an ordinary C++ array "int elems[10]" a collection ?

As Rui points out, there are those that like more complex definitons,
but in general - a collection 'is a' bag.

No matter how complex the rules might be for placing items into or
removing, nor no matter how complex the construction or interfaces
might appear, if an object fits the simple analogy of a "bag" - it is
a collection. <bg>

-ralph
 
Ö

Öö Tiib

Is an ordinary C++ array "int elems[10]" a collection ?

"int elems[10]" is called C array or raw array. Equal weight C++ array
is "std::array<int,10> elems". C++ programmers usually name such things
with common term "containers".

Java programmers call similar things in their language "collections".
Probably they would also call a C array as "collection" if they
ever have to talk about one. So in that sense it is indeed a collection.
 
S

Saeed Amrollahi

Hi



Could you please explain me the difference between collection, aggregate and

composition. For me all of them are the same.



thanks in advance

Hi
In addition to other mentioned, you should consider
in which context we are talking. In standard C++, the
term 'aggregate' is used for arrays and classes without
user-defined constructors and class related facilities like private or protected members, or base classes and so on. Roughly, it means C-style structswith implicitly declared special member functions. Based on this definition, standard array container is an aggregate too.

The words collection and composition aren't standard C++ terms. I searched the whole last C++ standard draft, collection is used as a general word andcomposition (a couple of times) is used for function object composition.

In the context of general object-oriented programming
In general object-oriented context or UML, there is a class relationship called 'Has a' or 'whole/part'.
Composition is a strong and Aggregate is a week kind of
such relationship. Consider the following piece of code
class Point {
int x, y;
public:
Point() : x{0}, y{0} {}
};
class Circle1 {
Point center; // composition
public:
Circle1() : center{} {}
} C1;
Point Cent;
class Circle2 {
Point* center; // aggregate
public:
Circle2() : center{&Cent} {}
} C2;
In C1, by constructing object, center is
created and by calling destructor, it's center
is destructed. But in C2, after destruction,
Cent is present.

Java/C# use the word collection like container in C++.

HTH,
-- Saeed Amrollahi Boyouki
 
J

Jorgen Grahn

Hi



Could you please explain me the difference between collection, aggregate and

composition. For me all of them are the same.



thanks in advance

Hi
In addition to other mentioned, you should consider
in which context we are talking. In standard C++, the
term 'aggregate' is used for arrays and classes without
user-defined constructors and class related facilities like priv[...]

I was going to complain, but I see it's in Stroustrup's glossary:
http://www.stroustrup.com/glossary.html

Still, I don't think I've ever heard it used like that. If it's a
term in the standard, I don't think it has caught on very well
outside it.

/Jorgen
 
R

Rui Maciel

Jorgen said:
I was going to complain, but I see it's in Stroustrup's glossary:
http://www.stroustrup.com/glossary.html

Still, I don't think I've ever heard it used like that. If it's a
term in the standard, I don't think it has caught on very well
outside it.

From ISO/IEC 14882:2011:

8.5.1 Aggregates
1 An aggregate is an array or a class (Clause 9) with no user-provided
constructors (12.1), no brace-or-equal-initializers for non-static data
members (9.2), no private or protected non-static data members (Clause 11),
no base classes (Clause 10), and no virtual functions (10.3).

The same text is included in ISO/IEC 14882:1998, without the brace-or-equal-
initializers bit.


Rui Maciel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top