Skip to content

Commit bbfe587

Browse files
committed
Update object relationship learncpp
1 parent dd72d01 commit bbfe587

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

content/posts/cpp-learn.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3022,4 +3022,49 @@ int main()
30223022
30233023
return 0;
30243024
}
3025-
```
3025+
```
3026+
3027+
## Others
3028+
## 1. Object Relationships
3029+
- Learned about some different kinds of relationships between two objects.
3030+
3031+
- The process of building complex objects from simpler ones is called **object composition**.
3032+
- There are two types of object composition: `composition`, and `aggregation`.
3033+
- **Composition** exists when a member of a class has a part-of relationship with the class. In a composition relationship, the class manages the existence of the members.
3034+
- To qualify as a composition, an object and a part must have the following relationship:
3035+
- The part (member) is part of the object (class)
3036+
- The part (member) can only belong to one object (class) at a time
3037+
- The part (member) has its existence managed by the object (class)
3038+
- The part (member) does not know about the existence of the object (class)
3039+
- Typically implemented via normal member variables, or by pointers where the class manages all the memory allocation and deallocation. If you can implement a class as a composition, you should implement a class as a composition.
3040+
3041+
- **Aggregations** exists when a class has a has-a relationship with the member. In an aggregation relationship, the class does not manage the existence of the members.
3042+
- To qualify as an aggregation, an object and its parts must have the following relationship:
3043+
- The part (member) is part of the object (class)
3044+
- The part (member) can belong to more than one object (class) at a time
3045+
- The part (member) does not have its existence managed by the object (class)
3046+
- The part (member) does not know about the existence of the object (class)
3047+
- Typically implemented via pointer or reference.
3048+
3049+
- **Associations** are a looser type of relationship, where the class uses-an otherwise unrelated object.
3050+
- To qualify as an association, an object and an associated object must have the following relationship:
3051+
- The associated object (member) is otherwise unrelated to the object (class)
3052+
- associated object (member) can belong to more than one object (class) at a time
3053+
- The associated object (member) does not have its existence managed by the object (class)
3054+
- The associated object (member) may or may not know about the existence of the object (class)
3055+
- May be implemented via pointer or reference, or by a more indirect means (such as holding the index or key of the associated object).
3056+
3057+
- In a **dependency**, one class uses another class to perform a task. The dependent class typically is not a member of the class using it, but rather is temporarily created, used, and then destroyed, or passed into a member function from an external source.
3058+
3059+
- In a **container** class one class provides a container to hold multiple objects of another type. A value container is a composition that stores copies of the objects it is holding. A reference container is an aggregation that stores pointers or references to objects that live outside the container.
3060+
3061+
- **std::initializer_lis**t can be used to implement constructors, assignment operators, and other functions that accept a list initialization parameter.
3062+
- std::initializer_list lives in the <initializer_list> header.
3063+
3064+
|Property\Type |Composition |Aggregation |Association |Dependency|
3065+
|---|---|---|---|---|
3066+
|Relationship type |Whole/part |Whole/part |Otherwise unrelated |Otherwise unrelated|
3067+
|Members can belong to multiple classes |No |Yes |Yes |Yes|
3068+
|Members existence managed by class |Yes |No |No |No|
3069+
|Directionality |Unidirectional |Unidirectional |Unidirectional or bidirectional |Unidirectional|
3070+
|Relationship verb |Part-of |Has-a |Uses-a |Depends-on|

0 commit comments

Comments
 (0)