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

  Рет қаралды 63,051

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

Пікірлер: 171
@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
@sauravchaudhary3064
@sauravchaudhary3064 3 жыл бұрын
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 10 ай бұрын
Good Summary, Helped me sum up!
@savanmatariya7494
@savanmatariya7494 3 жыл бұрын
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 !
@NikhilShrestha1
@NikhilShrestha1 2 жыл бұрын
I was looking for this concept all over the internet and i couldn't get it with much detailed explanation. Thanks Keerti
@shobhitgarg6897
@shobhitgarg6897 3 жыл бұрын
Finally someone bring unique content...Thanks a ton!!!
@eswarb2717
@eswarb2717 2 жыл бұрын
Your way of teaching is good. Finally I can understand what virtual table and virtual pointer. Thanks
@iWontFakeIt
@iWontFakeIt 3 жыл бұрын
such a nice explanation!!! great job thanks for wonderful explanation
@cumuIonimbus
@cumuIonimbus Жыл бұрын
I was looking for this for some time now, thanks!
@rajataggarwal743
@rajataggarwal743 2 жыл бұрын
What makes the audience more happy: 1. Great Explanation (that indeed it is :) ) 2. Notes(not provided here) (drive pe uploaded)
@ram9523
@ram9523 3 жыл бұрын
This video is really very informative . Best video about vtables . Please make a video regarding some tricky questions & answers around virtual func , vptr,vtable
@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.🥰🥰
@shiladityaghosh
@shiladityaghosh 3 жыл бұрын
Probably the most precise explanation I came across on the topic. Thank you 😊
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
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 3 ай бұрын
such a detailed and easy to understand explanation. loved it :)
@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
@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.
@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.
@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
@NagaVijayKumar
@NagaVijayKumar 3 жыл бұрын
Keerti never fails to make things easy.
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Thank you so much 🥺🥺❤️❤️
@tony_Stark_2024
@tony_Stark_2024 9 ай бұрын
thanku ma'am for the valuable information it helps us alot
@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 ✌️✌️😇😇
@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 😂😀🔥👍🙏
@aguy9836
@aguy9836 10 ай бұрын
Thank you very much for the awesome explanation
@drunkmadala
@drunkmadala 2 жыл бұрын
You've earned my subscription.
@SanatanihinduHinduRashtra
@SanatanihinduHinduRashtra 2 жыл бұрын
awesome continue your series.
@adityaojha2701
@adityaojha2701 2 жыл бұрын
Thanks for explaining this difficult topic
@Account-fi1cu
@Account-fi1cu 2 жыл бұрын
You are great, thank you !
@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 😊
@sanjayreddy3295
@sanjayreddy3295 2 жыл бұрын
Really great explanation. Keep going
@gmtube1555
@gmtube1555 Жыл бұрын
very detailed explanation thank you so much
@tomerlevi2640
@tomerlevi2640 Жыл бұрын
great teaching skills. thank you,
@jayeetasinha3965
@jayeetasinha3965 2 жыл бұрын
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 10 ай бұрын
Can’t thank you enough for such kind words! 😇
@jmscshipp
@jmscshipp 2 жыл бұрын
Super helpful thank you!
@divyagoyal455
@divyagoyal455 2 жыл бұрын
Good content. Thank you!
@kimjitchu3799
@kimjitchu3799 2 жыл бұрын
Underated. U explained too well. Thanks
@motivationsagar5121
@motivationsagar5121 Жыл бұрын
explanation was really good thanks for sharing 🙏
@premanshdewangan8695
@premanshdewangan8695 3 жыл бұрын
Thankyou for starting this series. Very well explained 😊
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Thank you! So glad you like it 😇😇
@vinamrajha1571
@vinamrajha1571 22 күн бұрын
u explained really well..!
@tianmingguo8271
@tianmingguo8271 2 жыл бұрын
great explanation!
@vasujain1970
@vasujain1970 3 жыл бұрын
Love this idea, please upload more!
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Many coming up! 😇😇
@rohitkumar-lj2ru
@rohitkumar-lj2ru 3 жыл бұрын
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?
@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
@dhineshkumars1991
@dhineshkumars1991 2 жыл бұрын
Excellent explanation 😄
@adityarathore8892
@adityarathore8892 3 жыл бұрын
subscribed after watching content's quality :)
@oyepuru
@oyepuru 3 жыл бұрын
Thanks Needed This Series🔥🔥
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Yaaay, hope you like the videos!!! 😇😇
@girikgarg8
@girikgarg8 2 жыл бұрын
Nicely explained ma'am. If possible can you make videos on other OOPS interview questions too?
@priyanshvatsal9791
@priyanshvatsal9791 11 ай бұрын
Used a very good example.
@ayushyadav4180
@ayushyadav4180 3 жыл бұрын
it was a great tutorial.
@ashvinimore8922
@ashvinimore8922 Жыл бұрын
Can you please create videos on some other cpp concepts like Special pointers, Multithreading in cpp, Casting, Thanks in advance 😊
@chayakumarsedutainment4799
@chayakumarsedutainment4799 8 ай бұрын
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?
@gaddesireesha25
@gaddesireesha25 2 жыл бұрын
Hi, Can a poymorphic funtion be able to access child class variables when called using base class pointer?
@dattatammisetti9324
@dattatammisetti9324 2 жыл бұрын
Thankyou 🙂
@mnbvcxz1621
@mnbvcxz1621 2 жыл бұрын
thank you.
@sauravgupta6166
@sauravgupta6166 3 жыл бұрын
Thanks 😊
@bhushannalawade4120
@bhushannalawade4120 2 жыл бұрын
Make more videos on c++ interview...u give a good job mam
@preetikushwaha8734
@preetikushwaha8734 3 жыл бұрын
Great explanation 🔥
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Thank you Preeti! ❤️😇
@vaibhaves
@vaibhaves 2 жыл бұрын
More videos in C++, please.
@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✌️😊
@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
@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.
@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?
@good_boy_1
@good_boy_1 3 жыл бұрын
superbbbbbbbb
@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
@adityasheth
@adityasheth Жыл бұрын
Awesome explanation. Doubt : is vtable inside the class or not ?
@ganapatibiswas5858
@ganapatibiswas5858 2 жыл бұрын
Great Video
@ganapatibiswas5858
@ganapatibiswas5858 2 жыл бұрын
Watching again after 8 months 😀
@lokesh0429
@lokesh0429 3 жыл бұрын
What if two functions have virtual in base class how many vptr will create ?
@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
@Km-pn3hf
@Km-pn3hf 2 жыл бұрын
thanks
@bhushannalawade3891
@bhushannalawade3891 2 жыл бұрын
Plzzz continue this series
@frezajoe5836
@frezajoe5836 2 жыл бұрын
how vtable are organized in multihineritance ??
@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 ?
@saurabhgupta2301
@saurabhgupta2301 9 ай бұрын
Plz continue the course.
@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.....
@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()
@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!😊
@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
@hareeshchandra6357
@hareeshchandra6357 3 жыл бұрын
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?
@rushikeshkamble6187
@rushikeshkamble6187 2 жыл бұрын
VTABle will have pointers to the virtual functions only right ? not the pointers to the normal function
@sraynitjsr
@sraynitjsr 3 жыл бұрын
Nice explanation
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Thank you 🙏😇
@kunalkumar-ic1dc
@kunalkumar-ic1dc Жыл бұрын
If vptr and vtable are part of class, then what are their type?
@kushagrabhardwaj4436
@kushagrabhardwaj4436 3 жыл бұрын
As Promote is not a virtual, why do we have entry for it in Vtable?
@47_samagrapathak50
@47_samagrapathak50 Жыл бұрын
please explain can virtual functions be static ? i am not able to understand by watching the vedio
@ShubhamKumar-et7gx
@ShubhamKumar-et7gx 2 жыл бұрын
why virtual fn cant be static??
@hareeshchandra6357
@hareeshchandra6357 3 жыл бұрын
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?
@kage-musha1702
@kage-musha1702 3 жыл бұрын
Sister, can you make a brief detailed video on pointers fully :) ? Thank you
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Will do!
@baqtronic7704
@baqtronic7704 2 жыл бұрын
Great explanation, thanks. Unfortunately, audio quality is not so good (saturating), although you used a mic.
@avdheshchauhan5310
@avdheshchauhan5310 6 ай бұрын
Hi @KeertiPurswani, As far as i know vtable does not contain non virtual function. vtable is created only for virtual functions.
@sakshisinghal1669
@sakshisinghal1669 2 жыл бұрын
I am unable to understand why virtual functions can't be static? Can anyone help me out with this.
@shaileshbharti1026
@shaileshbharti1026 3 жыл бұрын
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 😅
@user-oq1ct2ix2y
@user-oq1ct2ix2y Жыл бұрын
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 ....
@heyhello9336
@heyhello9336 2 жыл бұрын
what is the use of pure virtual function? void fun() =0
@ashutoshraj7171
@ashutoshraj7171 4 ай бұрын
why it is static array , not dynamic array for vTable?
@ishankbansal9239
@ishankbansal9239 3 жыл бұрын
Di Sorry for asking silly question But is this the part of OOPs?
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
No question is a silly question. We are all learning! And yes, it is 😊 Hope you like the videos!
@ishankbansal9239
@ishankbansal9239 3 жыл бұрын
@@KeertiPurswani thank you so much di☺️
@sucywang1795
@sucywang1795 3 жыл бұрын
Why is cc turned off? Could you add cc please!!!
@laveshgarg2189
@laveshgarg2189 3 жыл бұрын
Commenting for youtube algo😁❤️
@KeertiPurswani
@KeertiPurswani 3 жыл бұрын
Thank you so much Lavesh 🙏🙏😇😇
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 76 МЛН
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 41 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
Weak Pointers in C++ (std::weak_ptr)
17:25
The Cherno
Рет қаралды 45 М.
C++ vPointers and vTables
14:00
Jamie King
Рет қаралды 70 М.
Top 25 Microservice Interview Questions Answered - Java Brains
39:54
Virtual Function In C++
15:08
CppNuts
Рет қаралды 34 М.
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 76 МЛН