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

Ғылым және технология

►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►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.
►KZread Channel: / mikeshah
►Please like and subscribe to help the channel!

Пікірлер: 16

  • @GaryChike
    @GaryChike2 жыл бұрын

    Man, you are on a roll Mike!

  • @MikeShah

    @MikeShah

    2 жыл бұрын

    Thank you for the support! :)

  • @dhanushs1802
    @dhanushs1802 Жыл бұрын

    Very neatly explained as always. Thank you.

  • @MikeShah

    @MikeShah

    Жыл бұрын

    Cheers, thank you Dhanush!

  • @user-wc6zg7sz9l
    @user-wc6zg7sz9l2 ай бұрын

    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 KZread if I paste the link)

  • @MikeShah

    @MikeShah

    2 ай бұрын

    I think 'aggregate initialization' is more in reference to creating a new object through a mechanism like a list_initializer, versus the general idea of composition (which is about 'holding multiple components/fields/value' -- thus 'aggregating' or collecting one or more different or the same types together). I'll need to think about it a little more, but that's my initial understanding.

  • @im_another_you
    @im_another_you26 күн бұрын

    Composition over inheritance please make a session for this. 😊

  • @MikeShah

    @MikeShah

    26 күн бұрын

    Indeed, composition is in favor these days :)

  • @evgeniystakhovskiy5978
    @evgeniystakhovskiy59782 жыл бұрын

    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!

  • @MikeShah

    @MikeShah

    2 жыл бұрын

    In most of the literature that I have seen, the arrow points up from child to parent in regards to inheritance (because you want to know who you inherit from). This is opposite to how we usually draw when first learning about trees for example which is probably the confusion :) Hopefully I did not reverse this in any of the lessons, but again, arrow should point up from child towards parent :)

  • @blaisofotso3439
    @blaisofotso34392 жыл бұрын

    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?

  • @MikeShah

    @MikeShah

    2 жыл бұрын

    Aggregation is named because we're are 'collecting' or 'making up' a new data type consisting of other types. I agree, a picture may be useful to think about lifetimes. @4:30 I explain a little bit about the subtle difference between composition and aggregation. Composition think about as just having everything being destroyed (on a timeline) with the actual instance of that object. Aggregation could allow other objects to live further on a timeline (e.g. some data being pointed to)

  • @reptilicusrex4748
    @reptilicusrex4748 Жыл бұрын

    As usual, another great explanation.

  • @MikeShah

    @MikeShah

    Жыл бұрын

    Cheers!

  • @damondouglas
    @damondouglas Жыл бұрын

    This video reminded me of kzread.info/dash/bejne/n6OorrGin6-wlso.html use of composition to delegate methods to references of other class implementations. Your series is my personal prerequisite to reading Klaus's recent book. Thank you so much for this.

  • @MikeShah

    @MikeShah

    Жыл бұрын

    Cheers! Klaus gives wonderful talks, and his new book is wonderful -- definitely recommend!

Келесі