C++ Polymorphism and Virtual Member Functions [6]

Sdílet
Vložit
  • čas přidán 22. 04. 2023
  • In object-oriented programming, polymorphism enables object reference variables or pointers to reference objects of different types, and to invoke the appropriate member functions based on the type of object being referenced.
    Learn how through writing sample classes in this C++ tutorial for beginners written with Visual Studio 2022 C++ .
    // Join the new Programming for Everyone Community Forum:
    professorhank.freeforums.net/
    // Learn More //
    The Inheritance, Polymorphism,and Virtual Functions play list:
    • CH15: Inheritance, Pol...
    // Consider supporting this channel in multiple ways
    paypal.me/hankstalica1
    Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB

Komentáře • 19

  • @orionpluto1214
    @orionpluto1214 Před 2 měsíci +4

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

  • @saramomen2161
    @saramomen2161 Před 7 měsíci +5

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

  • @dwivedys
    @dwivedys Před měsícem +1

    Absolutely wonderful!

  • @Johannes-vr3vc
    @Johannes-vr3vc Před 3 měsíci +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.

  • @menachemlevi
    @menachemlevi Před 4 měsíci

    amazing keep going

  • @AlexTrouman-oi1yp
    @AlexTrouman-oi1yp Před 6 měsíci +1

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

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 6 měsíci +1

      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.

  • @bashiraddean-mufarreh
    @bashiraddean-mufarreh Před rokem +2

    im first watching 🤓first like 👍🏻

  • @user-hq2pc5yz6p
    @user-hq2pc5yz6p Před 6 měsíci

    what if we did r.print()

  • @manedurphy
    @manedurphy Před 10 měsíci +1

    class Foo {
    void print() const { cout

    • @ProfessorHankStalica
      @ProfessorHankStalica  Před 10 měsíci +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 Před 10 měsíci

      @@ProfessorHankStalica thank you for explaining. new subscriber 🤝

  • @massimoazzano
    @massimoazzano Před 5 měsíci

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

  • @egetan9247
    @egetan9247 Před 6 měsíci

    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  Před 6 měsíci

      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 Před 5 měsíci

      @@ProfessorHankStalica thank you so much.. :)

  • @jesuschrist1501
    @jesuschrist1501 Před 5 měsíci +1

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