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! 😊😇
@@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
@@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
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.
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.
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 ....
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
@@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.
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....
This video is really very informative . Best video about vtables . Please make a video regarding some tricky questions & answers around virtual func , vptr,vtable
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.
same question asked in my oracle interview but at that time i did not know this concept so its really painfull for me why i have not watched your videos.
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
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 ?
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 ?
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 😇😇
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)?
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
@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?
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?
@KeertiPurswani, could not follow the part of the video from 7:48, "All the object instances point to same vtable"? which is referred here as same vtable? Can you please clarify? Thanks
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?
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 😅
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?
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
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
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?
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 ?
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()
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.....
"vtable are static arrays, that means all the object instances points to the same vtable" I am not able to get this line, please if someone can explain? How object are pionting to the vtable, isnt it class associated with it, and vptr changes the pointer to the corresponding vtable during runtime ( Late binding) ? Is it that object by default points to same vtable and during run time they change according to the object type?
object created from Engineer class will have Vptr to Vtable which is static array. if you invoke raise_salary method, using Vptr, Vtable will be found, and this table will have function pointer to the Engineer::raise_salary method. let s say you have Employee type pointer which points to an object which is Engineer type. if you invoke raise_salary method using this pointer, doest matter it is Employee type, it will invoke Engineer::raise_salary method. It is because the object pointed by this pointer is a Engineer type, and it has Vptr to the exactly same static Vtable which has function pointer to the Engineer::raise_salary method. as you can see, we have 2 objects of Engineer type, and both shares the same Vtable. I hope it helps.
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! 😊😇
I guess its "should not be", as promote is not a virtual method
@@veereshr She Didn't Mark Promote As Virtual Method .... Consider That Method As Virtual
@@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
@@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
@@ashutosh2933 Ohk Understood, thank you
What makes the audience more happy:
1. Great Explanation (that indeed it is :) )
2. Notes(not provided here)
(drive pe uploaded)
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.
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.
thanks for taking the effort
Good Summary, Helped me sum up!
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 !
I was looking for this concept all over the internet and i couldn't get it with much detailed explanation. Thanks Keerti
Best video on runtime polymorphism on yt, highly underrated.
Your example made it a lot clear.
Thank you for explanation. This is the only video from where we can understand clearly about VT and VP
Your way of teaching is good. Finally I can understand what virtual table and virtual pointer. Thanks
Like the way you say that Hi guys by waving that hand in certain fashion. Feels comedy but nice 😂🤣👍
It's my thing 🤭🤭
@@KeertiPurswani Keep this going 😂😀🔥👍🙏
Keerti never fails to make things easy.
Thank you so much 🥺🥺❤️❤️
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 ....
Thankyou ma'am, for clearing out the query that even G4G couldn't explain properly.
Explained in very easy language that a beginner can understand as well.
Please upload more videos on similar topics.
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
Quite a comprehensive treatment of the topic. This series might rival Yan Chernikov's C++ playlist and possibly outshine it. Well done!
Thanks. Definitely don't want to be someone's rival though. Just hoping the series helps many. Although, honoured to be mentioned with him 😊😇
@@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.
Thanks Vivek. Means a lot to me 😇😇 Cheers to spreading positivity ✌️✌️
I watched Yan Chernikov's C++ series and that too good.
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....
Can’t thank you enough for such kind words! 😇
thanku ma'am for the valuable information it helps us alot
Probably the most precise explanation I came across on the topic. Thank you 😊
Thanks! Hope you like rest of the videos as well 😇😇
I used to be always confused no matter how much I read. Thanks Keerti for clarifying it!!
Yaaaaaay. Now you are a pro ✌️✌️😇😇
Finally someone bring unique content...Thanks a ton!!!
I never used to get this 😭, but now you started a series thanks 😊.
The concept in the video is clear yeah? These are important points for interviews 😊
Ton of thanks to you Keerthi pls continue this series..awaiting for new videos
Thanks for explaining this difficult topic
This video is really very informative . Best video about vtables . Please make a video regarding some tricky questions & answers around virtual func , vptr,vtable
Great Video..continue the series
such a detailed and easy to understand explanation. loved it :)
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.
You are right. I missed marking promote as virtual. Had added a pinned comment about the same.
same question asked in my oracle interview but at that time i did not know this concept so its really painfull for me why i have not watched your videos.
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
Learnt something really cool today, looking forward to more videos in this series 😇
New video is out! Hope you like it! 😇
@@KeertiPurswani please continue this series keerti! Loved the content
Underated. U explained too well. Thanks
Can you please create videos on some other cpp concepts like
Special pointers,
Multithreading in cpp,
Casting,
Thanks in advance 😊
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 ?
You've earned my subscription.
thank you such a cool explanation !!!!!!!!
such a nice explanation!!! great job thanks for wonderful explanation
Hi @KeertiPurswani,
As far as i know vtable does not contain non virtual function. vtable is created only for virtual functions.
Best Explanation
awesome continue your series.
Thank you so much ma'am for this concept. It became crystal clear just before my interview. Thanks a lot.🥰🥰
explanation was really good thanks for sharing 🙏
Great Video
Watching again after 8 months 😀
great teaching skills. thank you,
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 ?
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 😇😇
@@KeertiPurswani Thanks for the clarification. Just want to ask you to consider "Virtual Inheritance" in your playlist
It will be there for sure. Already in the list✌️😊
very detailed explanation thank you so much
Thank you very much for the awesome explanation
subscribed after watching content's quality :)
Make more videos on c++ interview...u give a good job mam
Nice talk. where do length of v-table is store.
Key Idea: When "virtual" is used, function will be called based on object type not the pointer type.
Excellent explanation 😄
Sister, can you make a brief detailed video on pointers fully :) ?
Thank you
Will do!
More videos in C++, please.
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)?
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
Thankyou for starting this series. Very well explained 😊
Thank you! So glad you like it 😇😇
nicely explained.
Really great explanation. Keep going
Love this idea, please upload more!
Many coming up! 😇😇
Good content. Thank you!
Nicely explained ma'am. If possible can you make videos on other OOPS interview questions too?
You are great, thank you !
Used a very good example.
Great explanation, thanks. Unfortunately, audio quality is not so good (saturating), although you used a mic.
Super helpful thank you!
Awesome explanation.
Doubt : is vtable inside the class or not ?
@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?
Plz continue the course.
great explanation!
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?
@KeertiPurswani, could not follow the part of the video from 7:48, "All the object instances point to same vtable"? which is referred here as same vtable? Can you please clarify? Thanks
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?
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 😅
Thanks Needed This Series🔥🔥
Yaaay, hope you like the videos!!! 😇😇
Plzzz continue this series
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?
superbbbbbbbb
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?
Great explanation 🔥
Thank you Preeti! ❤️😇
vtable me sirf virtual functions jaate h
since promote is not a virtual function it will not go in the tables
If we have n number of class object, then how many virtual pointers will be there can you explain it. Thanks
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
VTABle will have pointers to the virtual functions only right ? not the pointers to the normal function
Hi, Can a poymorphic funtion be able to access child class variables when called using base class pointer?
internet says : static methods cannot be marked as virtual
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
it was a great tutorial.
please explain can virtual functions be static ? i am not able to understand by watching the vedio
Nice explanation
Thank you 🙏😇
how vtable are organized in multihineritance ??
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?
Will cover this in a separate video!😊
If vptr and vtable are part of class, then what are their type?
Why promote function pointer is part of vtable here. early binding is enough to call promote, as it is not a virtual function.
Make promote as virtual function
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 ?
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()
As Promote is not a virtual, why do we have entry for it in Vtable?
Commenting for youtube algo😁❤️
Thank you so much Lavesh 🙏🙏😇😇
what is the use of pure virtual function? void fun() =0
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.....
"vtable are static arrays, that means all the object instances points to the same vtable" I am not able to get this line, please if someone can explain? How object are pionting to the vtable, isnt it class associated with it, and vptr changes the pointer to the corresponding vtable during runtime ( Late binding) ?
Is it that object by default points to same vtable and during run time they change according to the object type?
object created from Engineer class will have Vptr to Vtable which is static array. if you invoke raise_salary method, using Vptr, Vtable will be found, and this table will have function pointer to the Engineer::raise_salary method. let s say you have Employee type pointer which points to an object which is Engineer type. if you invoke raise_salary method using this pointer, doest matter it is Employee type, it will invoke Engineer::raise_salary method. It is because the object pointed by this pointer is a Engineer type, and it has Vptr to the exactly same static Vtable which has function pointer to the Engineer::raise_salary method. as you can see, we have 2 objects of Engineer type, and both shares the same Vtable. I hope it helps.