Composition vs dependency classes

D

dev_15

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
 
A

anon

dev_15 said:
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.
 
A

anon

dev_15 said:
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.
 
J

Jim Langston

dev_15 said:
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?
 
H

herschel1969

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)
 
H

herschel1969

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)
 
D

dev_15

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
 
G

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) .

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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top