Virtual Function In C++

SdĂ­let
VloĹžit
  • čas přidĂĄn 17. 07. 2024
  • JOIN ME
    -----
    CZcams 🎬 / @cppnuts
    Patreon 🚀 / cppnuts
    COMPLETE PLAYLIST
    ------------
    C++ Tutorial For Beginners: • Introduction To C++
    STL (Standard Template Library): • STL In C++
    ThreadIng In C++: • Multithreading In C++
    Data Structures: • Data Structure
    Algorithms: • Binary Search
    Design Patterns: • Factory Design Pattern...
    Smart Pointers: • Smart Pointer In C++
    C++14: • Digit Separator In C++
    C++17: • std string_view in C++...
    C++ All Type Casts: • static_cast In C++
    INTERVIEW PLAYLIST
    ------------
    C++ Interview Q&A: • Structural Padding & P...
    C++ Interview Q&A For Experienced: • How delete[] Knows How...
    Linked List Interview Questions: • Find Kth Node From Bac...
    BST Interview Questions: • Search Element In Bina...
    Array Interview Questions: • Reverse An Array
    String Interview Questions: • Check String Is Palind...
    Bit Manipulation Questions: • Find Set Bit In Intege...
    Binary Tree Interview Question: • Invert Binary Tree
    Sorting Algorithms: • Bubble Sort
    C++ MCQ: • Video
    C MCQ: • What printf returns af...
    C Interview Questions: • Designated Initializat...
    QUICK SHORT VIDEOS
    -------------
    C++ Short : • C++ Short Videos
    C Short : • Shorts C Programming MCQ
    In this video we will learn about what is virtual function and how to use it in C++.
    NOTES:
    1. WHY to use virtual function?
    a. To achieve dynamic polymorphism. Which is the ability to call Derived class function using Base class pointer or reference.
    2. HOW to use virtual function?
    a. By declaring function as virtual in Base class and overriding that function in Derived class.
    (Function signature should be same in Base and Dervied class)
    b. Declaring a function as virtual in Base class is enough, Derived class function need not to be declared virtual.
    c. Virtual functions should be accessed using pointer(*) or reference(&) of Base class type to achieve run time polymorphism.
    ADITIONAL NOTES:
    1. Virtual functions cannot be static and also cannot be a friend function of another class.
    2. A class can have virtual destructor but can't have virtual constructor.
    3. There are two types of virtual functions in C++.
    a. Virtual Function
    b. Pure Virtual Function
    #cpp #virtualfunction #programming #interviewquestions #softwareengineering #computerscience

Komentáře • 69

  • @pinakipb2
    @pinakipb2 Před 3 lety +8

    After a long time........... Really appreciate your videos.

    • @CppNuts
      @CppNuts  Před 3 lety +1

      Thank you so much 😀

  • @treyquattro
    @treyquattro Před 3 lety +6

    Always good to see new videos from Rupesh! Virtual is a very important topic in C++

    • @CppNuts
      @CppNuts  Před 3 lety +2

      Correct!!

    • @treyquattro
      @treyquattro Před 3 lety

      @Seth Trenton no, no-one gives a shit about your lies and malware

    • @treyquattro
      @treyquattro Před 3 lety

      @Eufemia Haeber seems to be sockpuppetry and trolling, I think you mean, you pathetic fake

  • @mockingbird3809
    @mockingbird3809 Před 3 lety +5

    Great video as always, Cppnuts! ;)

  • @mansokash2215
    @mansokash2215 Před 2 lety

    very good explanation, thank you, keep them like this detailed

  • @ManishPatel-dj7eb
    @ManishPatel-dj7eb Před 3 lety

    Nice informative video, It explains all details in easy way.
    thanks
    Q1. Which editor you are using ?

  • @bimalthapaliya7134
    @bimalthapaliya7134 Před 3 lety +1

    Sir, please make a video on pointer to object and object to pointer! We are getting lots of confusion in that portion!
    PS- always loved your work! Thank you for this series!

  • @raghuj7097
    @raghuj7097 Před 3 lety

    Super...after a long time...we are happy to see

  • @ravishsga
    @ravishsga Před 3 lety +2

    Good to see you back Rupesh :) Just one question in which scenario we need to call the derived function with the base class pointer? Why we are making the base class pointer point to the derived object?

    • @CppNuts
      @CppNuts  Před 3 lety +1

      If you want to create an array of different objects you can make their parent common and have array of pointers of those different object and do some operations on a single array.
      Let me know if this is not clear, actually I wanted to explain this also in video that why do we need that but I didn't actually find the good example for this.

    • @hritik4807
      @hritik4807 Před rokem

      @@CppNuts not clear , can you make a video :)

  • @ahmedajbna4804
    @ahmedajbna4804 Před 2 lety

    Thank you so much. I was very confused, now I got it.

  • @yashsaxena4634
    @yashsaxena4634 Před 3 lety +1

    Thanks for making it simple.

    • @CppNuts
      @CppNuts  Před 3 lety +1

      Glad it was helpful!

  • @amitpaunikar3408
    @amitpaunikar3408 Před 3 lety +1

    After long time.....good explanation

  • @rajeshsola1776
    @rajeshsola1776 Před 3 lety +3

    Hi, Thanks for sharing great videos, i think one statement can be fine tuned...Instead of saying virtual functions can be called with pointers and references only (which implies can't be called by objects), we can say that if we call with objects directly it'll go by static binding and won't get benefits of runtime polymorphism (or) to get the benefits of dynamic behavior call by pointer/reference..please see

    • @CppNuts
      @CppNuts  Před 3 lety +3

      Thanks man, but did i said this? I think i always added dynamic word with that line.

  • @Vicky-xi9ns
    @Vicky-xi9ns Před 3 lety

    Thanks for your c++ series

    • @CppNuts
      @CppNuts  Před 3 lety

      Glad you like them!

  • @mohammadjadidi233
    @mohammadjadidi233 Před 3 lety

    Really appreciate your videos....

  • @abhaybajpai9469
    @abhaybajpai9469 Před 2 lety

    Very Informative video....thanks a lot.....again!!!😁

  • @chitranshsaxena59
    @chitranshsaxena59 Před 3 lety

    Hey, nice video, maybe you can also explain about vtables and stuff

  • @Jam18123
    @Jam18123 Před 2 měsĂ­ci

    very informative, thank you

  • @abhinavkumar562
    @abhinavkumar562 Před 3 lety +3

    I hope that you will continue the dsa series...

  • @sureshonteru4609
    @sureshonteru4609 Před 3 lety +1

    Hi Rupesh Thanks a lot for the video , still I have a doubt why Base * b = derived ()
    It's calling base class method, what happens it's storing derived class address but it's calling base class method ,what happens inside and compile time . can you explain

  • @bhupeshpattanaik7150
    @bhupeshpattanaik7150 Před 3 lety +2

    Thanks ☺️ .... Please guide for competitive coding using CPP

    • @CppNuts
      @CppNuts  Před 3 lety +1

      Sure dude..
      Let's complete virtual topic first.

    • @bhupeshpattanaik7150
      @bhupeshpattanaik7150 Před 3 lety

      @@CppNuts ya please that soon ..... Very curious to start

  • @priyaldesai8290
    @priyaldesai8290 Před 2 lety

    Hi, Thank you for sharing the video. Can you please share the link for following vptr and vtable video as well.

    • @CppNuts
      @CppNuts  Před 2 lety

      I prepared notes to teach but i lost it somehow and still i didn't feel like redoing that notes, that was like so much hard work..
      Hope i may find courage to redo it and shoot the vid.

  • @ArjunArjun-vw5fq
    @ArjunArjun-vw5fq Před 3 lety +1

    congrats for 28k.

  • @hunnybunny4723
    @hunnybunny4723 Před 9 měsĂ­ci

    So what exactly is the use of this virtual functions concept?
    In what way is it useful in the real world scenario?
    We can call derived function by creating derived object ryt, what exactly the need of calling through the base class pointer?

  • @vvpChannel3112
    @vvpChannel3112 Před 3 lety +1

    What if we have a data member in base class lets say float f=1.2; So, can we access b->f iff Base *b = new Derived ?

    • @CppNuts
      @CppNuts  Před 3 lety +1

      Yes..

    • @sharath5796
      @sharath5796 Před 3 lety +1

      u have to declare that data member as protected ig

  • @AJAYKUMAR-gl1vx
    @AJAYKUMAR-gl1vx Před 2 lety

    Hi 😊
    Can you please explain, why pointer or reference variable is required for late binding?

    • @CppNuts
      @CppNuts  Před 2 lety

      Because late binding itself tells that I will be available later, and what ever comes later you have to work on that and this later thing is handled using pointer or reference, because later only these two make sense.
      In case of static (current, not the late) meaning you already have the object on which you will work.

    • @AJAYKUMAR-gl1vx
      @AJAYKUMAR-gl1vx Před 2 lety

      @@CppNuts Thank you 😊

  • @ranjanrajesh08
    @ranjanrajesh08 Před rokem

    Hi Rupesh, i tried coding using reference concept for virtual polymorphism…it is not working exactly the same way…i mean to say using reference if i try to call the fn…by removing virtual keyword in base class, then also last overridden fn gets called instead of base class fn

    • @CppNuts
      @CppNuts  Před rokem

      Give me the code plz.

    • @ranjanrajesh08
      @ranjanrajesh08 Před rokem

      @@CppNuts hey Rupesh…my mistake…i must have been doing some mistake…I tried to run the code again and i see it is working as expected…Thanku Rupesh😊

  • @shirishareddy2795
    @shirishareddy2795 Před 3 lety

    why can only achieve polymorphism(virtual function) through pointers or references,but not through object assignment. pl z solve my doubt sir

  • @AJAYKUMAR-gl1vx
    @AJAYKUMAR-gl1vx Před 2 lety

    class Base{public: virtual f(){}};
    class Derived:public Base{public: f(){}};
    int main(){Derived d; Base b=d; d.f(); return 0;}
    Here f() will be early binding or late binding??

  • @prishaphotography9063
    @prishaphotography9063 Před 3 lety

    Nice👍

  • @prasadsolleti3870
    @prasadsolleti3870 Před 4 měsĂ­ci

    Sir please explain one real time example code in virtual function and pure virtual function

  • @adityavaste3732
    @adityavaste3732 Před 2 lety

    That was so an amazing tutorial...
    But I have one doubt, that why we are saying it as "Run-Time Polymorphism",
    bcz compiler can easily decide which function he has to call, as we are using "virtual" keyword there, So, at a compile-time, he knows that, "We don't have to call the function in the base class bcz it is using virtual keyword, and we have to call the function from the derived class with the same name", that's simple...........
    he can easily decide at a compile-time, why he goes with run-time?

    • @CppNuts
      @CppNuts  Před 2 lety

      But it can call base class function also.. that's the point.
      Base pointer can contain either base object or derived object if it is containing derived object in that case it will call derived class function or if it is holding base class object then it will call base class function.
      That's why it is run time behaviour because base pointer can contain either base object or derived object so we really don't know no what object it is going have till run time.

  • @jilmon3498
    @jilmon3498 Před 2 lety

    Virtual table ?? Video?

  • @lifeisacelebration7233
    @lifeisacelebration7233 Před 3 lety +1

    Volume is bit less for all videos.

    • @CppNuts
      @CppNuts  Před 3 lety +1

      Correct, i try to avoid noise, so it become like that, i will work on this sound issue. Thanks..

    • @lifeisacelebration7233
      @lifeisacelebration7233 Před 3 lety

      @@CppNuts thank you for all the tutorials

    • @CppNuts
      @CppNuts  Před 3 lety

      You are welcome..

  • @vishalchoudhary2913
    @vishalchoudhary2913 Před rokem

    9:50 Please correct you can give body to pure virtual function.

    • @CppNuts
      @CppNuts  Před rokem

      Yes we can give body to pure virtual function.

  • @nikhilkhatri9721
    @nikhilkhatri9721 Před rokem

    *I don't want to become animal!!*

  • @Mani--ck3xc
    @Mani--ck3xc Před rokem

    hi @CppNuts i am getting this error cannot find virtual: No such file or directory , c:/users/mani.chaitanya.konka/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find virtual: No such file or directory
    c:/users/mani.chaitanya.konka/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find function: No such file or directory