VTable & VPointers - Virtual functions, Runtime Polymorphism | The most imp C++ interview Question!

  Переглядів 57,407

Keerti Purswani

Keerti Purswani

День тому

Finally starting the #C++ series, this series is for C++ specific Interviews and these are very common questions asked in many companies like Intuit, Adobe, Tower Research etc.
This is the first video which covers vtables and vpointers and how run time polymorphism happens.
Hope the video helps. Let me know!!
The video has following parts-
0:00-0:35 - Introduction
0:35-3:55 - Example to understand why
3:55-5:45 - VTable intro
5:45-7:25 - Fill VTable
7:25-7:55 - VTable is Static Array
7:55-9:10 - VPointers
9:10-11:40 - Revision & RunTime Polymorphism flow
For more content like this, please Subscribe to my channel - / keertipurswani
LinkedIn - / keertipurswani
Instagram - keerti.purs...
#Interview #Preparation #Coding #Questions #growwithkeertipurswani

КОМЕНТАРІ: 170
@KeertiPurswani
@KeertiPurswani 3 роки тому
Hi guys, one more thing I should have mentioned is that of methods are virtual, only then they need to be added in VTable and so the promote method should also be marked virtual in the example. Thanks for pointing this out in comments! 😊😇
@veereshr
@veereshr 2 роки тому
I guess its "should not be", as promote is not a virtual method
@ashutosh2933
@ashutosh2933 2 роки тому
@@veereshr She Didn't Mark Promote As Virtual Method .... Consider That Method As Virtual
@veereshr
@veereshr 2 роки тому
@@ashutosh2933 do you mean we need to write virtaul infront of Promote or it is assumed virtual , as one of method of that class is given a virtual keyword
@ashutosh2933
@ashutosh2933 2 роки тому
@@veereshr Only Virtual Methods Need To Be Added In VTable But She Added Promote Method Too In VTable Which Is Non - Virtual .... To Correct This Mistake , She Said That Promote Method Should Have Been Virtual .... So Assume There Is Virtual Keyword Before Promote Just Like The Other Method
@veereshr
@veereshr 2 роки тому
@@ashutosh2933 Ohk Understood, thank you
@SumanKalyanGiri
@SumanKalyanGiri 2 роки тому
Ma'am please continue your C++ series, it's really helpful and unique. There are very limited tech creators talking about C++ at the level you are explaining the concepts.
@sarveshhiwase193
@sarveshhiwase193 2 роки тому
Best video on runtime polymorphism on yt, highly underrated. Your example made it a lot clear.
@interupachandu38
@interupachandu38 2 роки тому
Thank you for explanation. This is the only video from where we can understand clearly about VT and VP
@NikhilShrestha1
@NikhilShrestha1 Рік тому
I was looking for this concept all over the internet and i couldn't get it with much detailed explanation. Thanks Keerti
@shobhitgarg6897
@shobhitgarg6897 2 роки тому
Finally someone bring unique content...Thanks a ton!!!
@sandhyav4674
@sandhyav4674 Рік тому
Ton of thanks to you Keerthi pls continue this series..awaiting for new videos
@anupamdubey5736
@anupamdubey5736 2 роки тому
Thank you so much ma'am for this concept. It became crystal clear just before my interview. Thanks a lot.🥰🥰
@iWontFakeIt
@iWontFakeIt 2 роки тому
such a nice explanation!!! great job thanks for wonderful explanation
@savanmatariya7494
@savanmatariya7494 2 роки тому
I tried to understand VTable from gfg and other sites. Finally got the video which explained it simply. Your efforts are worth it. Great Job !
@cumuIonimbus
@cumuIonimbus Рік тому
I was looking for this for some time now, thanks!
@VaishnaviNigam
@VaishnaviNigam 2 роки тому
DII please continue this SERIES naa.. its really very helpful.. bcz theses are some underlying concepts which you know that are imp for us to know but we as students are unaware and hence miss them will studying....plz continue this series of interview ques
@sauravchaudhary3064
@sauravchaudhary3064 2 роки тому
Summary Virtual Function IMP: A virtual function is used to replace the implementation provided by the base class. The replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. 1. A virtual function is a member function which is present in the base class and redefined by the derived class. 2. When we use the same function name in both base and derived class, the function in base class is declared with a keyword virtual. 3. When the function is made virtual, then C++ determines at run-time which function is to be called based on the type of the object pointed by the base class pointer. Thus, by making the base class pointer to point to different objects, we can execute different versions of the virtual functions. Key Points : 1. Virtual functions cannot be static. 2. A class may have a virtual destructor but it cannot have a virtual constructor.
@abhiavasthi624
@abhiavasthi624 2 роки тому
thanks for taking the effort
@yashgaur1716
@yashgaur1716 7 місяців тому
Good Summary, Helped me sum up!
@eswarb2717
@eswarb2717 2 роки тому
Your way of teaching is good. Finally I can understand what virtual table and virtual pointer. Thanks
@rajataggarwal743
@rajataggarwal743 Рік тому
What makes the audience more happy: 1. Great Explanation (that indeed it is :) ) 2. Notes(not provided here) (drive pe uploaded)
@shiladityaghosh
@shiladityaghosh 2 роки тому
Probably the most precise explanation I came across on the topic. Thank you 😊
@KeertiPurswani
@KeertiPurswani 2 роки тому
Thanks! Hope you like rest of the videos as well 😇😇
@DeepikaSingh-dp3bi
@DeepikaSingh-dp3bi 2 роки тому
Explained in very easy language that a beginner can understand as well. Please upload more videos on similar topics.
@shristiprakash8026
@shristiprakash8026 28 днів тому
such a detailed and easy to understand explanation. loved it :)
@anmolkhandelwal4036
@anmolkhandelwal4036 2 роки тому
Great Video..continue the series
@abhaybajpai9469
@abhaybajpai9469 2 роки тому
Thankyou ma'am, for clearing out the query that even G4G couldn't explain properly.
@Account-fi1cu
@Account-fi1cu 2 роки тому
You are great, thank you !
@adityaojha2701
@adityaojha2701 Рік тому
Thanks for explaining this difficult topic
@theindianinvestor3240
@theindianinvestor3240 3 роки тому
Like the way you say that Hi guys by waving that hand in certain fashion. Feels comedy but nice 😂🤣👍
@KeertiPurswani
@KeertiPurswani 3 роки тому
It's my thing 🤭🤭
@theindianinvestor3240
@theindianinvestor3240 3 роки тому
@@KeertiPurswani Keep this going 😂😀🔥👍🙏
@ram9523
@ram9523 2 роки тому
This video is really very informative . Best video about vtables . Please make a video regarding some tricky questions & answers around virtual func , vptr,vtable
@sanjayreddy3295
@sanjayreddy3295 2 роки тому
Really great explanation. Keep going
@SanatanihinduHinduRashtra
@SanatanihinduHinduRashtra 2 роки тому
awesome continue your series.
@premanshdewangan8695
@premanshdewangan8695 3 роки тому
Thankyou for starting this series. Very well explained 😊
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thank you! So glad you like it 😇😇
@tony_Stark_2024
@tony_Stark_2024 6 місяців тому
thanku ma'am for the valuable information it helps us alot
@vivekshenoy6335
@vivekshenoy6335 3 роки тому
Quite a comprehensive treatment of the topic. This series might rival Yan Chernikov's C++ playlist and possibly outshine it. Well done!
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thanks. Definitely don't want to be someone's rival though. Just hoping the series helps many. Although, honoured to be mentioned with him 😊😇
@vivekshenoy6335
@vivekshenoy6335 3 роки тому
@@KeertiPurswani Appreciate your eagerness to contribute and make a difference. Just to clarify, I used the word 'rival' in a constructive sense and not in a belligerent one.
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thanks Vivek. Means a lot to me 😇😇 Cheers to spreading positivity ✌️✌️
@evilprince2009
@evilprince2009 3 роки тому
I watched Yan Chernikov's C++ series and that too good.
@motivationsagar5121
@motivationsagar5121 Рік тому
explanation was really good thanks for sharing 🙏
@jmscshipp
@jmscshipp 2 роки тому
Super helpful thank you!
@shehzadahmed693
@shehzadahmed693 3 роки тому
I used to be always confused no matter how much I read. Thanks Keerti for clarifying it!!
@KeertiPurswani
@KeertiPurswani 3 роки тому
Yaaaaaay. Now you are a pro ✌️✌️😇😇
@dhineshkumars1991
@dhineshkumars1991 Рік тому
Excellent explanation 😄
@m.vineeth9724
@m.vineeth9724 3 роки тому
Learnt something really cool today, looking forward to more videos in this series 😇
@KeertiPurswani
@KeertiPurswani 3 роки тому
New video is out! Hope you like it! 😇
@shreyasingh1258
@shreyasingh1258 2 роки тому
@@KeertiPurswani please continue this series keerti! Loved the content
@gmtube1555
@gmtube1555 Рік тому
very detailed explanation thank you so much
@aguy9836
@aguy9836 7 місяців тому
Thank you very much for the awesome explanation
@tomerlevi2640
@tomerlevi2640 Рік тому
great teaching skills. thank you,
@jayeetasinha3965
@jayeetasinha3965 Рік тому
Keerti, could not explain that how much your videos and your presentation affects a lot of people. Just sending you best wishes and loads of love !!! Keep up good work dear.... may all your wishes come true... because it is full of honesty and dedication....
@KeertiPurswani
@KeertiPurswani 7 місяців тому
Can’t thank you enough for such kind words! 😇
@drunkmadala
@drunkmadala 2 роки тому
You've earned my subscription.
@vasujain1970
@vasujain1970 3 роки тому
Love this idea, please upload more!
@KeertiPurswani
@KeertiPurswani 3 роки тому
Many coming up! 😇😇
@kimjitchu3799
@kimjitchu3799 2 роки тому
Underated. U explained too well. Thanks
@oyepuru
@oyepuru 3 роки тому
Thanks Needed This Series🔥🔥
@KeertiPurswani
@KeertiPurswani 3 роки тому
Yaaay, hope you like the videos!!! 😇😇
@tianmingguo8271
@tianmingguo8271 2 роки тому
great explanation!
@TheMkools
@TheMkools 3 роки тому
I never used to get this 😭, but now you started a series thanks 😊.
@KeertiPurswani
@KeertiPurswani 3 роки тому
The concept in the video is clear yeah? These are important points for interviews 😊
@divyagoyal455
@divyagoyal455 2 роки тому
Good content. Thank you!
@NagaVijayKumar
@NagaVijayKumar 3 роки тому
Keerti never fails to make things easy.
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thank you so much 🥺🥺❤️❤️
@adityarathore8892
@adityarathore8892 2 роки тому
subscribed after watching content's quality :)
@bhushannalawade4120
@bhushannalawade4120 2 роки тому
Make more videos on c++ interview...u give a good job mam
@vaibhaves
@vaibhaves 2 роки тому
More videos in C++, please.
@girikgarg8
@girikgarg8 Рік тому
Nicely explained ma'am. If possible can you make videos on other OOPS interview questions too?
@priyanshvatsal9791
@priyanshvatsal9791 8 місяців тому
Used a very good example.
@preetikushwaha8734
@preetikushwaha8734 3 роки тому
Great explanation 🔥
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thank you Preeti! ❤️😇
@ayushyadav4180
@ayushyadav4180 2 роки тому
it was a great tutorial.
@dattatammisetti9324
@dattatammisetti9324 Рік тому
Thankyou 🙂
@bhushannalawade3891
@bhushannalawade3891 2 роки тому
Plzzz continue this series
@ashvinimore8922
@ashvinimore8922 Рік тому
Can you please create videos on some other cpp concepts like Special pointers, Multithreading in cpp, Casting, Thanks in advance 😊
@mnbvcxz1621
@mnbvcxz1621 2 роки тому
thank you.
@saurabhgupta2301
@saurabhgupta2301 6 місяців тому
Plz continue the course.
@ganapatibiswas5858
@ganapatibiswas5858 2 роки тому
Great Video
@ganapatibiswas5858
@ganapatibiswas5858 Рік тому
Watching again after 8 months 😀
@adityasheth
@adityasheth Рік тому
Awesome explanation. Doubt : is vtable inside the class or not ?
@sauravgupta6166
@sauravgupta6166 2 роки тому
Thanks 😊
@sraynitjsr
@sraynitjsr 3 роки тому
Nice explanation
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thank you 🙏😇
@rohitkumar-lj2ru
@rohitkumar-lj2ru 2 роки тому
Hi , The lecture is awesome. Just need a confirmation, The vtable only contains virtual function pointers (as i see promote is not a virtual function but still added in vtable) ? Can you please confirm on this one?
@DJ-pz9re
@DJ-pz9re 3 роки тому
Hi mam, your lectures are really awesome with crisp and clear explanation. Can you suggest some book which we should follow to study some of topics like these in depth in C++. (I don't want to learn syntactical things. But I'm interested in how things work internally)?
@geeksentertainmentmedia4827
@geeksentertainmentmedia4827 3 роки тому
Effective modern cpp by scott is a good book to know depth if you already have idea about cpp. Otherwise for starters can start with cpp programming language by bjarne stroustrup
@good_boy_1
@good_boy_1 2 роки тому
superbbbbbbbb
@VagaDuo
@VagaDuo 3 роки тому
5:57 The vtable stores pointers for virtual functions only. In this case, Emp::vtable will only store Emp::RaiseSal(). Emp::Promote() will undergo compile time binding. Please correct me if I am wrong.
@KeertiPurswani
@KeertiPurswani 3 роки тому
You are right. I missed marking promote as virtual. Had added a pinned comment about the same.
@varunoyal87
@varunoyal87 Рік тому
Thanks for the clarification about VPtr and Vtable.I have some queries below: 1. when we say late binding is happening at run time. How it is happening actually? 2. If one derived class e.g child is derived from two base classes e.g. Dad and Mom . In this case both vptr will be derived in child class if yes, then how it will resolved at run time. Thanks
@ayushthakur2896
@ayushthakur2896 2 роки тому
Ma'am does vtable of a given class has function poiters to all the functions(virtual/non virtual) present in the class or vtable has gives memory to only to function pointers of virtual functions
@JardaniJovonovich192
@JardaniJovonovich192 3 роки тому
Thanks for the explanation!! I have a question. AFAIK, VTable would comprise of pointers to Virtual functions only. In the Video, In Emp class, you marked "raise salary" method as Virtual but not "Promote" method, so I think Emp class's VTable would only contain a pointer to "raise salary" method ? Could you please clarify on this ? As you started with Virtual keyword, in the next video could you please cover Virtual Inheritance along with Virtual Constructors and Destructors ?
@KeertiPurswani
@KeertiPurswani 3 роки тому
Yes!!! You are right. Promote will be there in VTable only if it's marked virtual. No point adding non virtual methods in VTable. Should have pointed that out in the video. Thanks for telling. I will add in the comments and pin it. The second video on virtual constructors and destructors is already recorded. Will be up on Friday. Hope you like it 😇😇
@JardaniJovonovich192
@JardaniJovonovich192 3 роки тому
@@KeertiPurswani Thanks for the clarification. Just want to ask you to consider "Virtual Inheritance" in your playlist
@KeertiPurswani
@KeertiPurswani 3 роки тому
It will be there for sure. Already in the list✌️😊
@chayakumarsedutainment4799
@chayakumarsedutainment4799 5 місяців тому
I have question since I'm new to the C++. Does vtable gets created by default by the compiler even without virtual keyword declared either in base or derived class for a member function?
@alessandropantano9863
@alessandropantano9863 2 роки тому
@Keerti Purswani The concept is clear but why the virtual table pointer is not affected when copy or assigning operation is performed? Example: Emp* emp = new Engineer(); // is valid Emp emp = Engineer(); // virtual table pointer is not affected Usually the address of the pointer is copied. So, it is the virtual table pointer an exception?
@Km-pn3hf
@Km-pn3hf 2 роки тому
thanks
@ketanlalcheta4558
@ketanlalcheta4558 2 роки тому
Thanks for good explanation... I got the idea that vtable is static array and it gets created at compile time... Also Vtable gets created per class involving one virtual method directly or indirectly Question is about what happens at run time ? Is it like vptr of object gets associated at run time ?
@gaddesireesha25
@gaddesireesha25 2 роки тому
Hi, Can a poymorphic funtion be able to access child class variables when called using base class pointer?
@kage-musha1702
@kage-musha1702 3 роки тому
Sister, can you make a brief detailed video on pointers fully :) ? Thank you
@KeertiPurswani
@KeertiPurswani 3 роки тому
Will do!
@avdheshchauhan5310
@avdheshchauhan5310 3 місяці тому
Hi @KeertiPurswani, As far as i know vtable does not contain non virtual function. vtable is created only for virtual functions.
@shaileshbharti1026
@shaileshbharti1026 2 роки тому
A Short an easy crash course video on VTFs 💯. Let's say there are 2 classes A and B and they are inherited, and we are making some functions virtual bcz we are doing function overriding . so how many vptr will be Assigned by compiler ? I meant will both v tables will be Assigned by two respective two vptr !! OR only 1 vptr is made and that only is inherited and so on... Actually got confused a bit that's why 😅
@madhavikr8074
@madhavikr8074 2 роки тому
Hii i want to understand why we need to use base class pointer to call derived methods instead i can have dervied object for calling derived methods please help me with this
@MichaelYoussry
@MichaelYoussry 3 роки тому
How would this work with multiple inheritance? Will the derived class have multiple vptrs? And if so, how does the compiler know which one to dereference?
@KeertiPurswani
@KeertiPurswani 3 роки тому
Will cover this in a separate video!😊
@vaibhavtandon7821
@vaibhavtandon7821 2 роки тому
How does VTable handles situation if we have a non virtual function in base and a same name function in a Derived class as well ? Will VTable of derived class still point to base function ?
@lowearthsurfer
@lowearthsurfer 2 роки тому
Nope. The Vtable only points to virtual functions/pure virtual functions. The nonvirtual base function will be called by it's derived class, unless you namespace the function, like Derived::function()
@hareeshchandra6357
@hareeshchandra6357 2 роки тому
Vptr secretly inserts a pointer in constructor of base class that points to v table of base class.. Derived class also gets vptr in constructor of derived class because of inheritance.. So now derived vptr pointing to vtable.. How exactly compiler calling the derived class function only.. Both vptr pointing to V tables right?
@baqtronic7704
@baqtronic7704 2 роки тому
Great explanation, thanks. Unfortunately, audio quality is not so good (saturating), although you used a mic.
@user-oq1ct2ix2y
@user-oq1ct2ix2y 10 місяців тому
I think still many things are left in this Video...I would say please do some coding and try to create an object and do some dissection on object and try to locate vptr inside object memory and then try to locate vtable address inside object memory and then try to locate virtual fuctions address...then full clearity will come ....
@frezajoe5836
@frezajoe5836 2 роки тому
how vtable are organized in multihineritance ??
@hareeshchandra6357
@hareeshchandra6357 2 роки тому
V table is a static array..When u say all objects are pointing to the same vtable.. Means it will point to base vtable or derived class vtable?
@lokesh0429
@lokesh0429 2 роки тому
What if two functions have virtual in base class how many vptr will create ?
@NeverGiveUp_oo7
@NeverGiveUp_oo7 Рік тому
If we have n number of class object, then how many virtual pointers will be there can you explain it. Thanks
@rushikeshkamble6187
@rushikeshkamble6187 Рік тому
VTABle will have pointers to the virtual functions only right ? not the pointers to the normal function
@47_samagrapathak50
@47_samagrapathak50 10 місяців тому
please explain can virtual functions be static ? i am not able to understand by watching the vedio
@kunalkumar-ic1dc
@kunalkumar-ic1dc 10 місяців тому
If vptr and vtable are part of class, then what are their type?
@ameynaik2743
@ameynaik2743 2 роки тому
Key Idea: When "virtual" is used, function will be called based on object type not the pointer type.
@tanmaysinghal8370
@tanmaysinghal8370 2 роки тому
Ma'am i'm preparing for cognizant GenC next interview, can you suggest me something please, I'm nervous af. Also, I'm not that good in logic building... so m just unable to solve problem, RECURSION DP DSA are way too difficult for me. Also Do i need to revise OS, networking etc for it.....
@laveshgarg2189
@laveshgarg2189 3 роки тому
Commenting for youtube algo😁❤️
@KeertiPurswani
@KeertiPurswani 3 роки тому
Thank you so much Lavesh 🙏🙏😇😇
@heyhello9336
@heyhello9336 2 роки тому
what is the use of pure virtual function? void fun() =0
@raht9578
@raht9578 3 місяці тому
vtable me sirf virtual functions jaate h since promote is not a virtual function it will not go in the tables
@ShubhamKumar-et7gx
@ShubhamKumar-et7gx 2 роки тому
why virtual fn cant be static??
@kushagrabhardwaj4436
@kushagrabhardwaj4436 2 роки тому
As Promote is not a virtual, why do we have entry for it in Vtable?
@ashutoshraj7171
@ashutoshraj7171 Місяць тому
why it is static array , not dynamic array for vTable?
@soccer7901
@soccer7901 3 роки тому
so for an engineer variable, I would do Eng* eng = new Eng(); instead of Emp* eng = new Eng(); so I can access the engineer class's Raise salary method. so i guess what i am asking what is the good use of a virtual function?
@KeertiPurswani
@KeertiPurswani 3 роки тому
So, in real scenario - when you have so many employees, you will have to keep separate lists for engineers, managers, directors etc and perform separate operations for them. But there will be so many things you would want to do for employees like declare a holiday, pay salary etc. If you keep just one list of employees using the emp pointer, it's very easy to do all this. Getting my point?
@srivaishnav2319
@srivaishnav2319 3 роки тому
I had this doubt but then my professor gave me an excellent example, her basically Every employee have many things in common just some differences and these differences can be made virtual. Now comming to your doubt, apart from these properties we want all of these employees to be of same type so that we can do operations, for example take comparison, if you have eng object and a manager object then you have to write comparison functions for every object so you have to make nc2 functions for this, but if all of them are of employee objects then it would make your life a lot simpler to manage. It's hard to explain here but remember this we need all the objects to be of same class so that processing is easy.
@soccer7901
@soccer7901 3 роки тому
@@KeertiPurswani That makes sense. Thank you!
@sakshisinghal1669
@sakshisinghal1669 2 роки тому
I am unable to understand why virtual functions can't be static? Can anyone help me out with this.
Історія загиблого Назара Небожинського
00:54
Суспільне Рівне
Переглядів 621 тис.
Eurovision Song Contest 2024: First Semi-Final (Live Stream) | Malmö 2024 🇸🇪
2:23:45
C++ vPointers and vTables
14:00
Jamie King
Переглядів 70 тис.
why does inheritance suck?
8:05
Low Level Learning
Переглядів 199 тис.
Top 5 C++ Interview Questions to Test Your Programming Prowess
13:39
Technocraft
Переглядів 79 тис.
Virtual Function In C++
15:08
CppNuts
Переглядів 33 тис.
Історія загиблого Назара Небожинського
00:54
Суспільне Рівне
Переглядів 621 тис.