Relationship between Virtual Functions, Pure Virtual Functions and Abstract Classes in OOP explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone and welcome to my channel in this video i want to explain the purpose and relationship between virtual functions pure virtual functions and abstract classes i am so happy to continue this object-oriented programming playlist and if you have any questions or any topics that you would like to see in the future please write me in the comments section so that i can prepare and film those videos and as i said in this one i will talk about virtual functions pure virtual functions and abstract classes but before i start i would like to take a brief moment to introduce c plus plus builder it is full featured c plus ide that helps you create apps fast for all major platforms while writing less code it connects natively to almost 20 databases like mariadb oracle sql server postgres and many more the key value is c plus plus builders framework which are powerful libraries that do more than other c plus tools this includes the award winning vcl framework for high performance native windows apps and fire monkey framework for cross-platform uis smart developers write better code faster and you can start for free using the link in the description the first topic that i want to explain is the topic of virtual functions in c plus so what is a virtual function a virtual function is a function that is defined in base class and then it is redefined in derived class but a virtual function has a very specific purpose and the main purpose of virtual functions is to give you the ability of runtime polymorphism and i'm going to show the example of this at the end of the video now to understand this a little bit better imagine the following situation you have a base class and inside that base class you implement a function and then you decide to create another class which will inherit from this base class so a derived class and inside this derived class you also decide to make the implementation of that same function so now a base class has its own implementation and then a derived class has another implementation what virtual functions allow you to do is to execute the most derived version of that function so when you invoke that function using a base class pointer or a reference the most derived version of that function will be executed what that means is that if this derived class has its own implementation then that derived implementation will be executed but if a derived class does not have its own implementation then the implementation inside your base class will be executed and the best way for you to understand this is to show you on the example using code and visual studio that i have opened here so now i am going to do that so let's create a class called instrument and i will do it here i will say class instrument like this and don't forget to put this semicolon at the end so what i want to do inside this instrument class is i want to create a function that will be public so that it is accessible outside of this class and that function will be of return type void and i will call it for example make sound so make sound like this and what i want to do inside this function is just write out a message so i will say c out and then let's say that the instrument is playing like this okay so now in order to test this and invoke this function what we have to do is we need to create an object of this class here so let's say instrument and let's call that object for example i one so instrument one and i am going to invoke this function here using this object so i will say i one dot make sound okay so if i run this program as you can see it says that instrument is playing so nothing unusual you should already be familiar with this now what i want to do is i want to create another class that inherits from this class here so i want to create a specific type of instrument for example let's create an accordion so i will say here class accordion like this and then what i want to do for this class here is first of all i want to inherit from this class here so i will say public and then instrument now this class this accordion class has become derived class and then instrument so this class here is called base class okay and this here represents the inheritance now what i want to do inside this derived class called accordion is i want to make another implementation of this same function here this function called make sound so let's do that i am going to copy this code here and paste it inside my accordion class and here instead of instrument playing i want to say that an accordion is playing like this okay so now if you remember what i said at the beginning of this video i said that when this function here called make sound that is implemented both in base class and in derived class is invoked using a base class pointer the most derived version of that function should be executed so in order to test that what i want to do is first of all let's create a pointer to our base class so here instead of creating an object what i want to do is i want to create a pointer like this and if you are not familiar with pointers i will put a link to a playlist that i made related to pointer so you should watch that first so here i have created a pointer to an instrument and what i want to do is say that it is equal to new accordion like this and please pay attention that this here is derived class so this here is derived class and then this here is base class okay and then here as you can see we have an error so the error is that now you cannot use this dot symbol to access your make sound function but you have to use this symbol here so when this here is a pointer and in this situation it is as you can see you use this symbol to access members of your class and make sound function is member of your instrument class okay so let's run our program and as you can see it says instrument playing again and if you remember what i said at the beginning is that when you use virtual functions the most derived version of that function should be executed and in this situation the most derived version is this one so the version inside our derived class so what is the problem here well the problem is that this function is not virtual function this is just a function and in order to make it virtual you need to add virtual keywords so here i am going to say virtual like this and now after you have made this function virtual what it does is it says hey if there is implementation of this function in my derived class please execute that and then if there is no implementation inside my derived class then you can execute this version here so if i run this program as you can see now it says that accordion is playing okay so let's close this and let's go one more time over what i just did so we created a base class called instrument and then we inherited from that instrument class as you can see here we created another class called accordion and we inherited that instrument class and then also we created a function called make sound in both classes so inside our instrument class and then inside our accordion class but that function has different implementations here it says that instrument is playing and then here it says that accordion is playing and then by making this function inside our base class a virtual function what we have allowed to happen is to execute the most derived version of this function here when it is invoked using a base class pointer as we did here so now when we invoke this make sound function it goes here and it says hey do you have more derived version of this function here so thus your derived class has its own implementation and here it finds that implementation so it executes that and that is exactly what we have seen what i ran my program last time but in case that this class here does not have the implementation for this function then this version will be executed and in order to demonstrate that let's remove this function from our derived class so i am going to delete it and now if i run my program this version here should be executed so let's run our program okay and as you can see it says instrument playing because derived class does not have its implementation of this virtual function okay let's close this and then if i return the implementation to my derived class then again this derived version should be executed so if i run my program again as you can see now it says that accordion is playing okay so that is an example of how virtual functions work and for those of you who are wondering why i used the accordion as an example of instrument instead of some more common instruments like a piano or a guitar or something like that it is because i used to play the accordion so while i was in school for five or six years before university a long time before i got into it and programming i used to play the accordion and while i was preparing this example i got very nostalgic because i haven't played it in a million years and i found on youtube these two guys who are playing accordions and they are playing some of my favorite songs some of the songs that i used to play all the time so i will put a link in the description for those of you who want to check it out and let me know if you recognize any of the songs and also if you play any instrument or if you would like to play some instrument and my favorite composition of all times is called donovsky valve the the new waves i used to love that composition i used to play it all the time and i might even play it one day for you guys subscribe if you would like to see that but it makes me very sad that i don't have any videos of myself playing because when i was younger there were no cameras and smartphones or at least they were not affordable and cheap like they are right now but i will try to find a link on youtube and then put it in the description for those of you who want to hear how the new waves sound um i'm just getting very nostalgic right now this programming course got very emotional cut the next concept that i want to explain is the idea of pure virtual functions and abstract classes and in order to explain that we will have to look at this example here from a different perspective so let's say for example that someone asks you what kind of sound an instrument makes your answer to this question could be it depends on what kind of instrument it is if it is an accordion or a piano or a guitar or some other instrument they all have their specific sounds or if someone asks you what kind of sound an animal makes you are going to answer it depends on the type of animal is it a cat a dog a cow or some other animal so that is exactly what i want to do in this example here so what i want to do is i want to say hey this instrument class cannot have the implementation of this function here but what it should do instead is it should force all derived classes to make their own implementation of this function here and that is exactly what we use pure virtual functions for so what i want to do now is i want to say hey instrument class you will not have the implementation for this function you just need to make sure that every class that inherits from you implements their own make sound function so how do we do that well i am going to delete the implementation and then i am going to say that this virtual function is equal to zero and by doing this this function just became a pure virtual function and then this class here has become an abstract class because by definition an abstract class in c plus plus is a class that has at least one pure virtual function so we deleted the implementation and then we said that this virtual make sound function is equal to zero so what that means is that now every class that inherits from our instrument class will have to provide their own implementation for this function here so let's create another class that inherits from this class here so let's create a class called piano for example so class piano and then let's say that it inherits publicly from our instrument class like this okay and then the first thing that i want to see is let's see what will happen if we don't implement this function here so let me try to create an object of type piano or a base class pointer to a piano object like we did here with accordion and then let's see what will happen so i am going to copy this actually and then paste it here and here i will say instrument pointer which will be called instrument 2 will be a new piano like this and then here i will try to say instrument to make sound okay and as you can see immediately this here is underlined and we have an error which says that object of abstract class type piano is not allowed so you cannot do this and if we continue reading it says that pure virtual function makes sound has no overrider which means that we need to make our own implementation of this function here inside our piano class if we want to do this so that is how this function here forces all of the derived classes to implement it okay so that is what pure virtual function does okay so let's create an implementation of make sound function inside our piano class i am going to copy this and then paste it here and i will say that piano is playing like this and if i scroll as you can see the error has disappeared and if i try to run my program let's see what will happen okay it says that accordion is playing and then piano is playing so this line here and then this line here so that is exactly what we have achieved by making this function here a pure virtual function that means that it forces every derived class to make their own implementation of this function here so you add is equal to 0 and then there is no implementation inside your abstract class and this here is an abstract class because it has at least one pure virtual function one thing that i promised to show you at the beginning is the polymorphic behavior that we get when we use virtual functions so let's demonstrate that so i will go inside my main function and here i have two instrument pointers the first one is pointing to an object of accordion and then the second one to an object of piano class so what i want to do now is i want to create an array for example of type instrument pointer and let's call it instruments i'm going to copy this so that i don't make a typo okay so instruments like this and i will make it an array of two pointers like this okay and here i want to add two pointers the first one is going to be this instrument one and then instrument two like this and what i can do now is i can with very little code make all of my instruments play it doesn't matter if they are accordion or piano or if you decided to create any other type of instrument you use um the same code in order to make them all play so basically you can make your own concert in c plus so let's do that let's use a for loop in order to iterate through all of the elements of this array here so i will say inch i is equal to 0 and then i is less than 2 um i plus plus like this okay and what i want to do inside this for loop is i will say instruments of i make sound like this okay and let's comment this line of code and then this line of code as well because we don't have to invoke them separately anymore so now what you can do is you can add 10 instruments to this array here or 100 or 1000 instruments and then all of those instruments will start playing using just this code here and it doesn't matter if they are accordion or piano or any other derived class that you decided to create that will inherit from this class here called instrument so let's run our program okay and then as you can see accordion playing and then piano playing as well so now if you know how to play a sound in c plus you can basically create your own concert in c plus so i hope that this video was helpful for you to understand the relationship between virtual functions pure virtual functions and abstract classes and that you also understand the polymorphic behavior that we get when we use virtual functions and i will make more videos in the future related to abstract classes and abstraction and then also if you would like to see some other topics on my channel please put those in the comment section and i will make those videos and then if you liked this video give it a thumbs up for the youtube algorithm because that helps me a lot to reach more people and help them to learn programming thank you very much for watching and i will see you in some other video bye
Info
Channel: CodeBeauty
Views: 30,799
Rating: undefined out of 5
Keywords: virtual functions, pure virtual functions, abstract classes, difference between virtual function and pure virtual function, relationship between virtual functions and pure virtual functions, relationship between virtual functions and abstract classes, runtime polymorphism, c++, programming, oop, codebeauty, code beauty, object oriented programming, Pure Virtual Functions and Abstract Classes, Polymorphism example, programming tutorial, for beginners
Id: T8f4ajtFU9g
Channel Id: undefined
Length: 20min 52sec (1252 seconds)
Published: Thu Apr 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.