Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Has-a
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== === Entity–relationship model === [[File:ER Diagram MMORPG.png|right | thumb |[[Entity–relationship model]] ]] In databases has-a relationships are usually represented in an [[Entity–relationship model]]. As you can see by the diagram on the right an account can have multiple characters. This shows that account has a "has-a" relationship with character. === UML class diagram === [[File:AggregationAndComposition.svg|right | thumb |UML [[class diagram]]<br/>Composition and aggregation]] In [[object-oriented programming]] this relationship can be represented with a Unified Modeling Language [[Class diagram]]. This has-a relationship is also known as composition. As you can see from the Class Diagram on the right a car "has-a" [[carburetor]], or a car is "composed of" a carburetor. When the diamond is coloured black it signifies [[Object composition|composition]], i.e. the object on the side closest to the diamond is made up of or contains the other object. While the white diamond signifies [[Object composition#Aggregation|aggregation]], which means that the object closest to the diamond can have or possess the other object. === C++ === [[File:HasaExampleCar20240813.svg|thumb|Car, chassis and tires objects]] Another way to distinguish between [[Object composition|composition]] and [[Object composition#Aggregation|aggregation]] in modeling the real world, is to consider the relative lifetime of the contained object. For example, if a Car object contains a Chassis object, a Chassis will most likely not be replaced during the lifetime of the Car. It will have the same lifetime as the car itself; so the relationship is one of [[Object composition|composition]]. On the other hand, if the Car object contains a set of Tire objects, these Tire objects may wear out and get replaced several times. Or if the Car becomes unusable, some Tires may be salvaged and assigned to another Car. At any rate, the Tire objects have different lifetimes than the Car object; therefore the relationship is one of [[Object composition#Aggregation|aggregation]]. If one were to make a C++ software Class to implement the relationships described above, the Car object would contain a complete Chassis object in a data member. This Chassis object would be instantiated in the constructor of the Car class (or defined as the data type of the data member and its properties assigned in the constructor.) And since it would be a wholly contained data member of the Car class, the Chassis object would no longer exist if a Car class object was to be deleted. On the other hand, the Car class data members that point to Tire objects would most likely be C++ pointers. Tire objects could be instantiated and deleted externally, or even assigned to data members of a different Car object. Tire objects would have an independent lifetime separate from when the Car object was deleted.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)