473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

aggregation vs. composition

Hi

what is the difference "in code writing" between aggregation and
composition?
is the following correct?

aggregation (life time of both objects are independent).
class A{ /* ... */ };
class B{ A& a; /* ... */};

composition (b controls the life time of a)
class A{ /* ... */ };
class B{ A a; /* ... */};
I am not sure.
can some one please give some examples or even a link?

thanks

Nov 26 '06 #1
2 14788
Gary Wessle schrieb:
Hi

what is the difference "in code writing" between aggregation and
composition?
is the following correct?

aggregation (life time of both objects are independent).
class A{ /* ... */ };
class B{ A& a; /* ... */};

composition (b controls the life time of a)
class A{ /* ... */ };
class B{ A a; /* ... */};
I am not sure.
can some one please give some examples or even a link?

thanks
I think the differences lie in the behavior of the components when you
destroy B. Composition: a dies with B, aggregation: a may continue its
existance.
Therefore aggregations are normally references or pointers.
Compositions can be members or pointers, and if they're pointers you
have to delete them in B's destructor.

http://ootips.org/uml-hasa.html
Nov 26 '06 #2
Gary Wessle <ph****@yahoo.comwrote:
what is the difference "in code writing" between aggregation and
composition?
is the following correct?

aggregation (life time of both objects are independent).
class A{ /* ... */ };
class B{ A& a; /* ... */};

composition (b controls the life time of a)
class A{ /* ... */ };
class B{ A a; /* ... */};

I am not sure.
can some one please give some examples or even a link?
You could ask in the newsgroup comp.object.

When discussing the difference between aggregation and composition in
UML one has to keep in mind that UML is supposed to be independent of
any implementation language, so whatever the difference between the two,
it cannot be expressed in a manner that only one language can support.
C++ is unique among OO languages in its requirement to explicitly
destroy objects, so it's a safe bet that the difference between the two
associations has nothing to do with "who calls the destructor."

So, what is the difference? In a composition relationship, the whole has
sole responsibility for the disposition of its parts, or as you put it
above, the whole "controls the lifetime of" the part.

In other OO languages, if the whole shares its part with others, it
cannot guarantee that the part will be destroyed when the whole wants it
destroyed. In C++ if the whole shares its parts with others, it cannot
call the part's destructor without worrying that some object somewhere
has a reference/pointer to that part. Therefore, in order for the whole
to have "sole disposition" or "control the lifetime" of its parts, the
whole must be the only object that knows of the parts existence.

The only way to directly express this in C++ code, is to (a) not expose
the part object in the whole's interface or (b) if such exposure exists,
the part is passed using an auto_ptr. However, such expression isn't
necessary.

composition:
class Whole {
Part part;
public:
// no function returns 'part' as a Part& or Part*
// or if one does, the documentation explicitly states
// that the caller may not maintain a pointer/reference
// to the part.
};

or

class Whole {
Part* part;
public:
Whole(): part( 0 ) { }
~Whole() { delete part; }
void acceptPart( Part* part ) {
// documentation explicitly states that when the part
// is passed in, all other objects must relinquish any
// hold they have on the part.
}
};

--
To send me email, put "sheltie" in the subject.
Nov 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DKode | last post by:
I think i have a good handle on aggregation/composition. can someone tell me if i am headed in the right direction with my analysis below: We have the following objects: Employee...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
4
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
11
by: aaj | last post by:
Hi is it possible in VB.NET to aggregate two classes, if so could you tell me how its done? I have been looking for a while and can't find any examples that actually show the physical code....
23
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
7
by: Bruce One | last post by:
In C#, how would people implement a relationship between Customer class and Request class, considering a customer may have 0-n requests and a request must belong to 1 and only 1 customer...? ...
6
by: Jeff | last post by:
hey Can OO Aggregation be described as: - A system of objects that are built using each other any comments? Jeff
4
by: Salman | last post by:
The most confusing concept i have seen in OOP is the difference between Association, Composition and Aggregation. Anyone please tell me whats the actual difference between these three using...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.