Composition vs dependency classes

Discussion in 'C++' started by dev_15, Oct 24, 2007.

  1. dev_15

    dev_15 Guest

    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
     
    dev_15, Oct 24, 2007
    #1
    1. Advertising

  2. dev_15

    anon Guest

    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.
     
    anon, Oct 24, 2007
    #2
    1. Advertising

  3. dev_15

    anon Guest

    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.
     
    anon, Oct 24, 2007
    #3
  4. dev_15

    Jim Langston Guest

    "dev_15" <> wrote in message
    news:...
    > 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?
     
    Jim Langston, Oct 24, 2007
    #4
  5. dev_15

    Guest

    On Oct 24, 9:03 am, 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;
    > 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)
     
    , Oct 25, 2007
    #5
  6. dev_15

    Guest

    On Oct 25, 8:43 am, wrote:
    > On Oct 24, 9:03 am, 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;
    > > 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)
     
    , Oct 25, 2007
    #6
  7. dev_15

    dev_15 Guest

    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
     
    dev_15, Oct 25, 2007
    #7
  8. 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
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 25, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Schutkeker

    Need Composition Editor Tutorial

    John Schutkeker, Oct 17, 2003, in forum: Java
    Replies:
    0
    Views:
    333
    John Schutkeker
    Oct 17, 2003
  2. gaurav v bagga

    composition and inner classes

    gaurav v bagga, Dec 20, 2006, in forum: Java
    Replies:
    3
    Views:
    1,081
    Thomas Hawtin
    Dec 20, 2006
  3. Elena

    Classes "Dependency Walker"?

    Elena, Apr 2, 2009, in forum: Java
    Replies:
    13
    Views:
    2,034
    Thomas Kellerer
    Apr 4, 2009
  4. Replies:
    3
    Views:
    146
    Jörg W Mittag
    Jan 25, 2008
  5. Kevin Prichard
    Replies:
    11
    Views:
    412
    Richard Cornford
    Nov 29, 2005
Loading...

Share This Page