Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Composition vs dependency classes

Reply
Thread Tools

Composition vs dependency classes

 
 
dev_15
Guest
Posts: n/a
 
      10-24-2007
Hi, just a simple question in an OO design in C++ if i am using
composition
to model a relationship i would have the object as a member(or pointer
member) of the class

ie class Airport
{
private:
CAirplane* airplane;

}

in code use as airplane->...

however if i am just using a class, then i take it wouldn't be a
member.
In this case to use the class i take it you would just declare it
locally
wherver you wanted to use the class

somemethod()
{
CAirplane airplane;
airplane->

}

Is this is a dependency relationship. I am a bit confused whats the
difference? It just
seems to be the way they are declared. Can someone clarify

 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      10-24-2007
dev_15 wrote:
> Hi, just a simple question in an OO design in C++ if i am using
> composition
> to model a relationship i would have the object as a member(or pointer
> member) of the class
>
> ie class Airport
> {
> private:
> CAirplane* airplane;
>
> }
>
> in code use as airplane->...
>
> however if i am just using a class, then i take it wouldn't be a
> member.
> In this case to use the class i take it you would just declare it
> locally
> wherver you wanted to use the class
>
> somemethod()
> {
> CAirplane airplane;


This airplane is destroyed when somemethod() exits

> airplane->
>
> }
>
> Is this is a dependency relationship. I am a bit confused whats the
> difference? It just
> seems to be the way they are declared. Can someone clarify
>


I am confused with this paragraph. Interpunctions and question marks
seams to be mixed somehow.
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      10-24-2007
dev_15 wrote:
> Hi, just a simple question in an OO design in C++ if i am using
> composition
> to model a relationship i would have the object as a member(or pointer
> member) of the class
>
> ie class Airport
> {
> private:
> CAirplane* airplane;
>
> }
>
> in code use as airplane->...
>
> however if i am just using a class, then i take it wouldn't be a
> member.
> In this case to use the class i take it you would just declare it
> locally
> wherver you wanted to use the class
>
> somemethod()
> {
> CAirplane airplane;


This airplane is destroyed when somemethod() exits

> airplane->
>
> }
>
> Is this is a dependency relationship. I am a bit confused whats the
> difference? It just
> seems to be the way they are declared. Can someone clarify
>


I am confused with the question.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      10-24-2007
"dev_15" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi, just a simple question in an OO design in C++ if i am using
> composition
> to model a relationship i would have the object as a member(or pointer
> member) of the class
>
> ie class Airport
> {
> private:
> CAirplane* airplane;
>
> }
>
> in code use as airplane->...
>
> however if i am just using a class, then i take it wouldn't be a
> member.
> In this case to use the class i take it you would just declare it
> locally
> wherver you wanted to use the class
>
> somemethod()
> {
> CAirplane airplane;
> airplane->
>
> }
>
> Is this is a dependency relationship. I am a bit confused whats the
> difference? It just
> seems to be the way they are declared. Can someone clarify


Your question is confusing. I find it hard to ascertain what it is you are
actually trying to figure out.

When you have a pointer to a stucture or class, you need to use the arrow
operator -> to get access to the members. When you have an instance or
reference (not a pointer) then youuse the dot operator . to get to the
members of the class. What are you asking beyond this?


 
Reply With Quote
 
herschel1969@gmail.com
Guest
Posts: n/a
 
      10-25-2007
On Oct 24, 9:03 am, dev_15 <(E-Mail Removed)> wrote:
> Hi, just a simple question in an OO design in C++ if i am using
> composition
> to model a relationship i would have the object as a member(or pointer
> member) of the class
>
> ie class Airport
> {
> private:
> CAirplane* airplane;
>
> }
>
> in code use as airplane->...
>
> however if i am just using a class, then i take it wouldn't be a
> member.
> In this case to use the class i take it you would just declare it
> locally
> wherver you wanted to use the class
>
> somemethod()
> {
> CAirplane airplane;
> airplane->
>
> }
>
> Is this is a dependency relationship. I am a bit confused whats the
> difference? It just
> seems to be the way they are declared. Can someone clarify


As far as I have understood you are asking what's the difference
between declaring a member variable and declaring a local variable in
a member function.
A generic answer is that the difference is in the lifetime of the
component (airplane in you example) with respect to the lifetime of
the container.
I should add that I do not think it is the same to have a pointer to
the object or the object itself. In the first case the lifetime of the
component object is not supposed (in general) to be the same of the
lifetime of the container object: it is sometimes called association
if I remember correctly (but I can't relay on my memory for names). In
the second time of course the lifetime is exactly the same because the
component will be constructed when the container is constructed and it
will be destroyed when the container will be destroyed.

Cheers,
H
BTW: airplane-> in your somemethod is clearly an error (but I'm sure
you know it)

 
Reply With Quote
 
herschel1969@gmail.com
Guest
Posts: n/a
 
      10-25-2007
On Oct 25, 8:43 am, (E-Mail Removed) wrote:
> On Oct 24, 9:03 am, dev_15 <(E-Mail Removed)> wrote:
>
>
>
> > Hi, just a simple question in an OO design in C++ if i am using
> > composition
> > to model a relationship i would have the object as a member(or pointer
> > member) of the class

>
> > ie class Airport
> > {
> > private:
> > CAirplane* airplane;

>
> > }

>
> > in code use as airplane->...

>
> > however if i am just using a class, then i take it wouldn't be a
> > member.
> > In this case to use the class i take it you would just declare it
> > locally
> > wherver you wanted to use the class

>
> > somemethod()
> > {
> > CAirplane airplane;
> > airplane->

>
> > }

>
> > Is this is a dependency relationship. I am a bit confused whats the
> > difference? It just
> > seems to be the way they are declared. Can someone clarify

>
> As far as I have understood you are asking what's the difference
> between declaring a member variable and declaring a local variable in
> a member function.
> A generic answer is that the difference is in the lifetime of the
> component (airplane in you example) with respect to the lifetime of
> the container.
> I should add that I do not think it is the same to have a pointer to
> the object or the object itself. In the first case the lifetime of the
> component object is not supposed (in general) to be the same of the
> lifetime of the container object: it is sometimes called association
> if I remember correctly (but I can't relay on my memory for names). In
> the second time of course the lifetime is exactly the same because the
> component will be constructed when the container is constructed and it
> will be destroyed when the container will be destroyed.
>
> Cheers,
> H
> BTW: airplane-> in your somemethod is clearly an error (but I'm sure
> you know it)


More precisely (I checked the names now)
Composition: component with same lifetime of the container
Aggregation: a component with an independent lifetime
Association: a generic dependency (defined by specifying the roles of
the two objects)

 
Reply With Quote
 
dev_15
Guest
Posts: n/a
 
      10-25-2007
Hi, Sorry just to clarify, in C++ a 'HAS A' relationship is
implemented with the class having, as a member, another class.

How is a 'USING a' relationship implemented typically
(not composition or aggregation, say just a
class using another utility class etc) .

This is what i was trying to ask, hope its a bit clearer

 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      10-25-2007
On 2007-10-25 10:13, dev_15 wrote:
> Hi, Sorry just to clarify, in C++ a 'HAS A' relationship is
> implemented with the class having, as a member, another class.
>
> How is a 'USING a' relationship implemented typically
> (not composition or aggregation, say just a
> class using another utility class etc) .


There is no one to one mapping between UML and C++ code, though from a
piece of C++ code one should be able to generate UML diagrams (I am not
100% sure that you can not generate more than one set of diagrams
however). When going the other way (from UML to C++) there is usually a
number of different possible outputs. It is therefore impossible to say
how a UML relationship is "usually" implemented in C++.

Association can be used whenever none of the other relationships does
not fit and possible scenarios might be that an object of a type is
passed as a parameter to a function, or an object is temporarily created
in one of its functions and then discarded when the function returns.

For questions about UML a group discussing UML will give you much better
answers.

--
Erik Wikström
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes "Dependency Walker"? Elena Java 13 04-04-2009 08:50 PM
composition/inheritance/subclassing standard lib classes bobterwillinger@gmail.com Ruby 3 01-25-2008 12:23 PM
composition and inner classes gaurav v bagga Java 3 12-20-2006 01:58 PM
Protected static members, abstract classes, object composition vs. subclassing Kevin Prichard Javascript 11 11-29-2005 01:33 AM
Mozilla Mail composition spaces on screen quincey cavenaugh Firefox 8 02-14-2004 06:01 PM



Advertisments