Virtual functions are great for implementing interfaces. Think of a game class called GameObject that has two functions: virtual void Initialize() and virtual void Update(). You'll be creating many different types of GameObjects such as people, bullets, cars, chess pieces, etc. Instead of writing custom code to initialize and update every object, you'd like to be able to iterate over an array of GameObject pointers. Each derived GameObject implements its own Initialization and Update code.
need urgent answer ,i still don't understand virtual functions ,but i don't see whats the use of them ? ,like whats the use of making a class with pure virtual functions that we can't use ,why not just make the classes we want with what we want ,same for the virtual functions ,why not just overload the functions ?
the purpose of virtual function is to have the priority. i.e let say your base class has function and derived class has a same function as well. If you created a base class pointer and point it to derived class object and call that one function due to the type of the pointer base class function will be executed unless the function is virtual and at that point derived class object function will be able to override the base class function.
basically having virtual functions in base class let the derived class object use their own functions and if derived class don't have the function defined the base class will provide the function. As far as the use u never know when u can run into issue where u might need this type of functionality it is just a type of tool available for disposal :)
this is very good for student
hemantkumar78 1 month ago
Great instruction, the only critique is that it would be nice to be able to view the entire code versus the zooming in on a section some times.
karlmarxx999 2 months ago in playlist C++ Beginner Tutorials
I hate the word pointer
phillebeau 4 months ago
in 5:38
he says we "only" need to change the base destructor to virtual BUT
he has also changed the derived destructor to virtual, without mentioning why!
so why does he do that?
tronulu 1 year ago
@tronulu if the a base destructor/method is virtual all methods that are derived from it will be virtual
someone9031 8 months ago
Virtual functions are great for implementing interfaces. Think of a game class called GameObject that has two functions: virtual void Initialize() and virtual void Update(). You'll be creating many different types of GameObjects such as people, bullets, cars, chess pieces, etc. Instead of writing custom code to initialize and update every object, you'd like to be able to iterate over an array of GameObject pointers. Each derived GameObject implements its own Initialization and Update code.
CasualTS 1 year ago
thanx , it realy helpd me to understand the logic very easily and very quickly.
nehavashisht 1 year ago
thanks a bunch.Awesome for noobs as well as refreshing concepts.
abhishekpareek1983 1 year ago
i don't understand virtual functions still ,why do we need them ? why not just overload functions ?
hemagoku 1 year ago
need urgent answer ,i still don't understand virtual functions ,but i don't see whats the use of them ? ,like whats the use of making a class with pure virtual functions that we can't use ,why not just make the classes we want with what we want ,same for the virtual functions ,why not just overload the functions ?
hemagoku 1 year ago
@hemagoku
the purpose of virtual function is to have the priority. i.e let say your base class has function and derived class has a same function as well. If you created a base class pointer and point it to derived class object and call that one function due to the type of the pointer base class function will be executed unless the function is virtual and at that point derived class object function will be able to override the base class function.
atifplus 1 year ago
@atifplus
basically having virtual functions in base class let the derived class object use their own functions and if derived class don't have the function defined the base class will provide the function. As far as the use u never know when u can run into issue where u might need this type of functionality it is just a type of tool available for disposal :)
atifplus 1 year ago
Thanks,
Needed a quick refresher.
laserbeak43 1 year ago
I havent really needed to use virtual function (although i have used them) but i have had to use other object orientated "things" like overloaing
0121ryanh117 2 years ago
thanks, great set of tutorials
rhillner 2 years ago 12
0.0!
thanks for these vids, i've watched them all now
PiratesVersusNinjas 2 years ago 7
cool
avion85 2 years ago 3
O.O
Muratarh 2 years ago