C++ Polymorphism and Virtual Member Functions [6]

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024

Комментарии • 29

  • @orionpluto1214
    @orionpluto1214 6 месяцев назад +8

    The best explanation I've found on this subject, thank you very much !

  • @saramomen2161
    @saramomen2161 11 месяцев назад +8

    You explained too well. Tomorrow is my exam and I understood the topic completely.
    Thank you.

  • @dwivedys
    @dwivedys 5 месяцев назад +1

    Absolutely wonderful!

  • @Johannes-vr3vc
    @Johannes-vr3vc 7 месяцев назад +1

    Some time ago, when I used g++, I could type something like this: Square *s = (class Square*)&r; I think this will probably still work and maybe it explains things better.

  • @AlexTrouman-oi1yp
    @AlexTrouman-oi1yp 10 месяцев назад +1

    Mr.Hank please what is the difference betwen ~square(){}; and virtuel ~square(){}; .Thanks for your explanation

    • @ProfessorHankStalica
      @ProfessorHankStalica  10 месяцев назад +2

      It's the difference between using static binding and dynamic binding. If you want C++ to decide which method to use at runtime, you use dynamic binding (virtual), otherwise you use static binding.
      Sometimes C++ has to decide which destructor to use at run time. Basically, if you expect your class will ever be inherited from, you should make the destructor virtual.

  • @manedurphy
    @manedurphy Год назад +1

    class Foo {
    void print() const { cout

    • @ProfessorHankStalica
      @ProfessorHankStalica  Год назад +4

      It means that the print() function can not contain any code that modifies class member variables. If print were to try, the compiler will give you an error at compile time.
      For example,
      class Foo {
      int x;
      void print() const { x = 0; cout

    • @manedurphy
      @manedurphy Год назад

      @@ProfessorHankStalica thank you for explaining. new subscriber 🤝

  • @lg7t
    @lg7t 3 месяца назад +2

    so why did we make all the destrcutors virtual should not that be just for the base class or we did it for readbilty of the code ?

    • @ProfessorHankStalica
      @ProfessorHankStalica  2 месяца назад

      Best practice to make them virtual in case the class is derived from in the future.

  • @GeeksPlayOfficial
    @GeeksPlayOfficial Месяц назад

    Prof. Hank can't we use delete[] squares instead of the for loop to delete the pointers?
    Thank you

    • @ProfessorHankStalica
      @ProfessorHankStalica  Месяц назад

      No, because you have to delete each of the objects whose memory addresses are stored in the array. It's an array of pointers that was statically allocated.
      delete [ ] squares;
      would attempt to delete the array which is nonsensical because it wasn't dynamically allocated in the first place.
      It's the Square, Rectangle, and Foo that were dynamically allocated, not squares.
      Remember that for each new that executes, a corresponding delete has to execute.
      3 new execution, 3 delete.
      new Square, new Rectangle, new Foo
      So the loop repeats three times, calling delete each time, and just delete.
      We didn't create 3 arrays dynamically, so we don't use delete [ ]

    • @GeeksPlayOfficial
      @GeeksPlayOfficial Месяц назад

      @@ProfessorHankStalica thanks a lot!

  • @CliveMashiloane
    @CliveMashiloane Месяц назад

    why do we have to include destructors in the classes , derived classes per se , since we don't have pointers inside them??

    • @ProfessorHankStalica
      @ProfessorHankStalica  4 дня назад

      In case the class with them gets extended in the future to ensure the correct destructor gets invoked. It's good to make them virtual as well. It's a best practice kind of thing to ensure extensibility

  • @soukainahanane-l4f
    @soukainahanane-l4f 9 месяцев назад

    what if we did r.print()

  • @menachemlevi
    @menachemlevi 8 месяцев назад

    amazing keep going

  • @egetan9247
    @egetan9247 10 месяцев назад

    Sir , I did not understand something , which is ; Why we put virtual for all destructors meanwhile put virtual for only base class's print function .
    Thank you for considering :)

    • @ProfessorHankStalica
      @ProfessorHankStalica  10 месяцев назад

      You use virtual on methods that you expect will be overridden in child classes. If there is a chance your class gets inherited from, then it's a good practice to make destructors virtual.
      That way, if the classes get used in a polymorphic context, you ensure the correct destructor is executed for each object.

    • @egetan9247
      @egetan9247 9 месяцев назад

      @@ProfessorHankStalica thank you so much.. :)

  • @kingpic
    @kingpic 28 дней назад

    Thank you for the video. Very informative. Sorry to be that guy but the relationship between square and rectangle threw me off. Every square is a rectangle. Every rectangle is NOT a square. so wouldn't it make sense to make rectangle the base class?

    • @ProfessorHankStalica
      @ProfessorHankStalica  25 дней назад +1

      Could be... I might have gotten it backwards in the heat of the moment...my brain doesn't work like it used to.. :-)

    • @kB-hg2ci
      @kB-hg2ci 20 дней назад

      @@ProfessorHankStalica Clearly it works very well. The tutorial was not about geometry and it is a fantastic tutorial. Thanks you very much.

  • @bashiraddean-mufarreh
    @bashiraddean-mufarreh Год назад +2

    im first watching 🤓first like 👍🏻

  • @massimoazzano
    @massimoazzano 8 месяцев назад

    I like you use a geometric example, people like me can visualize the proposed problem, meaning and scope!

  • @jesuschrist1501
    @jesuschrist1501 9 месяцев назад +1

    yea but why the hell would you ever need to use all this shyt, keep it simple and readable.