Aggregation
Pointer Uses Objects need pointers for: Dynamic Memory Sharing Objects Need to create data on heap and have pointers to it Sharing Objects Want two things to know about same "Bob"
A riddle… Bob, Candace, Ernie and Sue are People Ernie and Sue are Childs Both are children of Bob and Candace How many people are there named Bob?
3 Child Versions Plain Old Variable Version Father/mother are Person objects
Plain Old Explored Child has their own Person as parent
3 Child Versions Plain Old Variable Version Father/mother are objects that exist within Child Each Child has own copies of father/mother
3 Children Versions Pointer Version father/mother pointers to Person
Plain Old Explored Child points to a shared parent
3 Children Versions Pointer Version Father/mother are pointers to external objects Parent's shared… probably not owned
3 Child Versions References Version Can share with reference vars:
3 Children Versions ReferenceVersion Fussy No way to change parent later Definitely not owned
Ownership General rules of thumb: Plain Variable Owns memory it labels
Ownership General rules of thumb: Plain Variable Owns memory it labels Reference Variable Does not own memory of thing it references
Ownership General rules of thumb: Plain Variable Owns memory it labels Reference Variable Does not own memory of thing it references Pointer May or may not own memory Only one pointer can
UML Aggregation vs Compostion Composition : has-a built using other object, object only exists as part of this one C++ standard variable
UML Aggregation vs Compostion Aggregation : has-a built using other object, but not necessarily owned C++ pointer/reference
Class vs Object Diagrams Class Diagram: Object Diagram: 1 box per class 1 box per object
Self-Reference C++ does not allow self-composition
Self-Reference Self-Aggregation is perfectly fine:
Self-Reference Class: Objects: