Classes part 23 - Composition (and aggregation) versus Inheritance in C++ | Modern Cpp Series Ep. 60

preview_player
Показать описание

►Lesson Description: In this lesson I discuss the concepts of composition, aggregation, and how they differ from inheritance. Understanding when to use each is important when designing classes. The preference tends to be to avoid deep inheritance chains, and prefer composition when possible. Part of making that decision means understanding when an object should 'have-a' type as part of it, versus the type having a 'is-a' (inheritance) relationship to truly represent a subclass.

►Please like and subscribe to help the channel!
Рекомендации по теме
Комментарии
Автор

As far as I know, composition is a "owns-a" or "is-part-of" relation, while aggregation is "has-a" instead.

codygao
Автор

Very neatly explained as always. Thank you.

dhanushs
Автор

Hey Mike, thanks for the awesome tutorial! At 6:33, I found that it says that an aggregation can be an array or a class that has no user-declared or inherited constructors (since C++20) ... on cppreference. Is the example at 6:33 still considered to be an aggregation or is this concept still applicable to aggregations?
Thanks in advance!

The keyword I use in cppreference: Aggregate initialization
(It seems like my comment would be deleted by YouTube if I paste the link)

維仁陳-ck
Автор

Mike, question not specifically related to the topic of the lesson, rather on how you draw the parent-child relationship. In the first example with inheritance, you've got encircled Character and Point2D, with the arrow pointing towards the parent Point2D. I'm confused why the arrow is pointing this way, for me it looks more logical to point to Character being a child? To my mind, the characteristics of parent are being transferred to the child, not vice versa. What am I missing here? Great job with the series!

evgeniystakhovskiy
Автор

Composition over inheritance please make a session for this. 😊

im_another_you
Автор

Great video and topic too. But could not differentiate between the lifetime of composition against aggregation.
Would be appreciated if you could do a pictorial explanation to iron out the lifetime concept.
We did aggregation in class using pointer . Why is it called aggregation?

blaisofotso