C++ Operator Overloading beginner to advanced (in-depth explanation)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to my channel i was in the middle of my work and i didn't plan to film this video right now but since i'm going to do something very interesting i figured why not show you so i am currently working on a very big project thousands of files millions lines of code and i was about to analyze my code so i know that when i run the analysis it is going to take 10 to 15 minutes in order to finish so i thought why not show you both how i analyze my code and then in the meantime while we wait for the analysis to finish i am going to teach you about operator overloading the analyzer that i use is called pds studio and if you are using it to learn or if you are working on open source projects or on closed projects you can get it for free using the link in my description so i'm going to show you now how i use pdf studio in order to find bugs and optimize my code so click the link in the description download pds studio and then you will need some code in order to analyze it so here i have the solution from my work and it has a lot of code a lot of different projects unfortunately i cannot show you the code itself because of my non-disclosure agreement but at the end of the video i am going to show you the result of the analysis so once you download pbs studio in order to analyze your code you will need to click on extensions and then pbs studio and here i'm going to check oh i'm going to check the entire solution so i want to check everything that my solution has so i will click that and the analysis should begin shortly after so because this is going to take 10 to 15 minutes because i have a lot of code to analyze we are going to leave this in the background and now i'm going to switch to our empty project and in the meantime while we wait i am going to teach you about operator overloading so what is operator overloading in c plus and what are operator functions in c plus plus we can create a special type of functions which are called operator functions and we use those operator functions in order to define how a certain operator will behave with a specific data type so in order to explain this let me give you the example if i say for example four plus five here we have this plus operator but in this situation our compiler already knows how the plus operator behaves when this is number and this is number as well so in this situation the plus operator will sum these two numbers but what is going to happen if we have a user-defined data type let's say for example a structure called car and if i try to do something like this if i say car one plus car two so what is going to happen in this situation and how will this plus operator behave so in this situation we can create our own overloaded function which is called operator plus and inside that function we can define how it will behave when we try to sum two cars and that is called operator overloading now a quick tip if you are not familiar with user-defined data types with classes and structures i am going to link a video here and i will also put it in the description so make sure to watch that video if you need additional help and then you can come back to this video so in this video i'm going to show you how you can overload operators that are used most often and then in the description i will leave the list of all of the operators that can be overloaded in c plus plus because there is a lot of operators but once you see the example that i will show you today it is going to be very easy to apply it on other operators as well so let's demonstrate how this works i am going to delete this code i don't need it anymore and let's create a user-defined data type i want to create a structure called youtube channel again if you're not familiar with structures the link will be in the description make sure to watch that video first so let's create a structure let's say struct youtube channel like this and let's add two attributes i will say string name also make sure to include string like this and then let's add another attribute let's say for example int subscribers count like this okay you can add additional attributes if you want of course but these two are going to be enough for me to demonstrate how operator overloading works now another thing is that you can also create a class but if you decide to create a class keep in mind that members of a class are private by default and then members of a structure are public by default and since i created a structure these two are public by default which means that they will be available outside of this structure so after i have created these two attributes let's also create a constructor again if you're not familiar with constructors the link will be in the description so let's say youtube channel and i will pass two parameters i will say string name and then int subscribers count let's copy this name i'll just use lowercase s so this is my constructor and inside this constructor i will initialize these two attributes so i will say that name is equal to my name parameter and then subscribers count is equal to subscribers count parameter so after i have created a constructor for this structure let's create an object of youtube channel type so let's create a youtube channel i will do it in my main function i will say youtube channel and let's call it youtube one like this and now i'm going to use this constructor in order to create this object so i will say youtube channel and it receives two parameters as you can see the first one is name and then the second one is subscribers count so i will pass the name here i will say code beauty like this and then subscribers count is i believe 75 000. i believe this is my current subscribers count and if you are not already subscribed please subscribe to my channel let's grow this community and let's learn programming together so after i have created this object of youtube channel type let me ask you a question and that question is following if i say for example c out and then i say number five what is going to happen in this situation the answer is pretty easy and that is that this number five will be printed to our console but what is going to happen if i do something like this if i say see out youtube channel 1 like this so what is going to happen in this situation write me in the comment section before you see the answer so if i try to run my program we are going to get an error and that error is following here it says no operator and then this operator here matches these two operands which means that now our compiler does not know how this operator here it is called insertion operator so how the insertion operator should behave with these two operands and these are two operands see out is one operand and then youtube channel one is second operand so now our compiler does not know how to print youtube channel it does not know how to print a user defined data type so what we need to do in order to fix this problem is we need to overload this insertion operator in order to be able to work with our youtube channel data type so now i'm going to show you how to do that so let's overload this insertion operator we already said that operator functions are just like regular functions but there is one rule when it comes to naming operator functions and that is that they need to be called operator and then you specify the operator that you want to overload so in this situation our function will be called operator and then this insertion sign here so let's create that function let's say void and then operator like this and then you specify the sign of the operator okay now one very important thing is that this insertion operator works with two operands the first operand is this c out object on the left side and then the second operand is youtube channel on the right side so these two operands this one here and then this one here will be passed as parameters to our operator function so if i hover over my c out object you will see that it is of type o stream so we will need to pass an object of type o stream as the first parameter so let's do that okay and let's call it c out and here i'm using capital letters so capital c out just to make the distinction between this object here and then this object here even though in the background they are the same object so that is the first argument and then second argument is this youtube channel one here so it is of type youtube channel so let's pass that as well like this and let's call it youtube channel okay one thing that i want you to notice is that here i am passing these two parameters by a reference and that means that we are going to pass the original so these two are not going to be copied but we are rather passing the original objects themselves now we use pass by reference when we have parameters that are not exactly cheap to copy now an example of parameters that are cheap to copy are numbers for example but a parameter like a string or a user-defined data type or a list a vector are not very cheap to copy so in that situation you can pass those by a reference and that is exactly what i'm doing here now what i want this function here to do is i want it to print the information about my youtube channel and i will use this see out object here and then this youtube channel object that i've passed to my function as well so let's say c out keep in mind that i am using this object here instead of this lowercase one okay so i will say c out and then let's say name and let's write out the name of our youtube channel so youtube channel dot name like this let's add end line so that is the first thing and then the second information that our youtube channel contains is the number of subscribers so i will say c out again i am using this capital c out so i will say c out and then subscribers like this and let's say youtube channel and then subscribers count like this so with this we have successfully overloaded this insertion operator and if i scroll down you will see that the error that we had here so the error with our insertion operator not being overloaded disappeared so if i try to run my program now write me in the comments what do you expect to happen as you can see we are successfully printing the information about our youtube channel so it says name code beauty and then s subscriber 75 so we need to remove one s okay so let's remove this s and what we have achieved by overloading this insertion operator for our youtube channel type is that now we can use see out command with a youtube channel type the same way that we are already used to using it with a number for example so that is what i wanted to show you now i want to show you something else and let's create another youtube channel so i will duplicate this line of code and i will create a youtube channel too and let's call this channel for example my second channel okay and let's say that on my second channel i have 80 000 subscribers and what i want to do is i want to say here so please print the information about my youtube channel one and then in the same line print the information about my youtube channel too and as you can see here we are getting the error again and in order to fix this error and in order to be able to use this c out command like this so in order to be able to print multiple objects in the same line what you need to do is two things first here you need to see that instead of returning void your operator function needs to return o stream object so o stream reference like this and then since you are saying that your function will return o stream object you need to return that object from your function so you need say here return c out oh like this and please notice that here i'm using this capital letter c out instead of this lowercase one so if i run my program again as you can see the error has disappeared and here we have the information about our first channel and then the second channel as well now let me show you something else since we said that operator functions are just like regular functions now your question might be can we invoke this operator function just like a regular function and the answer to that question is yes so let me demonstrate how that works so let's delete this second youtube channel from here now another way to do this same thing here is to say operator and then this insertion operator and then i will invoke this function here just like i would invoke any other global function so i will specify its name and here i will pass parameters so the first parameter is our c out object and then the second parameter is our youtube channel one okay so let's comment this line of code here and if i run my program as you can see we get the information about our first youtube channel so let's close this and as i said this is another way that you can use this operator function but the main reason why we overload operators is to be able to use them like this so this here is more common way to use operator functions than this approach here so that is the reason why i'm going to remove this second line but if your question is can we do that the answer is yes absolutely we can so this is how we overload the insertion operator and as you can see in this situation we created the operator function globally but what is going to happen if we want to overload an operator like plus or minus or operator greater than or less than or is equal to or not equal to and so on those operators are overloaded differently so now i'm going to show you how we can do that so let's collapse this code you can find it in the pen comment if you need it okay so what i want to do now is i want to create another structure another user defined data type so let's say struct and let's call it my collection like this and inside this my collection data type i want to have a list of youtube channels so let's say list and youtube channel so let's call it my channels like this also make sure to include lists here so i will say include list like this so after i created my collection structure let's do the following i'm going to remove this line of code and i will create an object of my collection type let's call it my collection like this let's just use capital letter c okay so what i want to be able to do in this situation is i want to say my collection plus equals to youtube channel one so i want to be able to add youtube channels to my collection by using this operator here and that is exactly what i want to show you now so how can we overload this operator here in order to work with this type here and this type here so the left operand is of type my collection which is data type that we created so in this situation we can create operator function plus equals as a member function of this structure here so in that situation we would need to pass only one parameter and that is the one on the right side and then this one is going to be available automatically so let me show you how that works and how can we overload this operator here so inside this my collection structure i will create operator function i will say void and let's call the function operator and then plus equals this here is the rule so this is how you name your operator functions you first say operator and then you specify the operator that you want to overload okay and then here we are going to pass parameters in this situation we need to pass only one parameter and that is the one on the right side and as i said this one is going to be available automatically so here i will say youtube channel and let's call it channel like this okay so what i want to do in this situation is i want to say the following this dot my channels okay and please keep in mind that you need to use this symbol here instead of a dot even though i say dot so this keyword implies this operand here so my collection and you can say this or you can just say my channels it will work the same way i will use this so that you know what is meant by this it is meant this first operand so my collection so please give me my channels from my collection and what i want to do is i want to say push back so please add to the list of my channels this channel here like this so that is how we overload this plus equals operator so that when we say my collection plus equals youtube one that means that youtube one will be added to this list of my channels and the behavior of this operator plus equals is very intuitive and that is the way that your operator functions should be they should be very intuitive because if someone needs to open the definition of your operator function in order to figure out what it is doing you are probably doing it wrong so let's explain something that you probably noticed by now and that is the difference between this operator function here and then this operator function here so as you can see first thing is that this operator function is created as member function of this structure here whereas this operator function is created as a global function and then second thing is that this function here receives only one parameter whereas this one here receives two parameters so why is that the case well in this situation here where we overloaded operator plus equals we have created it as a member class and we passed only one parameter because the first parameter so this first operand will be available automatically like this because it is a member function whereas in this function here we needed to pass both parameters so both operands because the first operand this c out object is of type o stream and there is a library that is already available for us to use and it would be very hard and almost impossible to define and overload this operator here for every single data type that we want to use this operator with so in this situation we are creating a global function and we are passing both parameters so both operands to that global function so since we are talking about operator overloading let's practice operator overloading some more so let's add to this collection here our second youtube channel as well let's say my collection plus equals to second youtube channel okay so what i want to be able to do now is the following i want to be able to say see out my collection like this but we will get a problem which says that this insertion operator is not overloaded for my collection object so it is not overloaded for my collection data type so let's overload this operator here for this type here as well so i am going to overload it as a global function like we did in this situation here okay so here i am going to say oh stream and then my reference and then i will call it operator insertion like this and since this is a global function it will need to receive both operands so both this one and this one so i will say that the first one is o stream object which is called c out again i'm using capital letters here okay and then the second will be my collection object and it will be called my collection like this so what i want to do inside this operator function is i want to print all of the elements of my collection and in order to do that i will use for each loop so i will say for so for each youtube channel let's call it youtube channel like this so for each youtube channel inside my collection dot my channels what i want to do is i want to print the information about that youtube channel so i will say see out youtube channel and then add endline and please pay attention that here i am using this parameter so capital c out instead of lowercase c out okay so after i have printed the information about my youtube channel the last thing that we need to do is since we said that this operator function will return o stream object we need to return that object from our function so i will say return c out because that is our o stream object that we need to return okay so what i will be able to do now is this line of code here so see out my collection and if i run this program let's see what is going to happen okay as you can see here we have the information about our first channel and then the information about our second channel perfect so let's collapse this function here because it is taking too much space and if you need the code it will be pinned in the comments as i already said so let me show you something else an interesting situation actually an error that can happen if you work on this on your own and the error is extremely hard for most beginners to figure it out on their own so let's demonstrate that error and let's fix it together so here i have created my collection and then i have added two youtube channels to that collection and i am printing the information about my collection so what i want to be able to do is the following so i want to be able to say my collection minus equals to youtube channel too so i want to be able to remove elements from my collection by using this minus equals operator so how will we do that you guessed it correctly we need to overload this operator here the same way almost identically as we did here but the behavior is going to be different of course so what i will do is inside this my collection i will copy this operator plus equals and then paste it below and i will change the name so that it is minus equals operator it will as well receive the channel that we want to remove and the behavior will be the following so inside this minus equals operator we want to remove this channel from this list here so i will say this my channels remove and then i will pass the channel that i want to be removed like this okay so as you can see the error has disappeared from here so what do you expect to happen in this situation if i run my program write me that in the comment section before you see my answer so let's run our program and we get the window which means that we have errors so let's see if we can get any help from this errors window here as you can see these errors are not very beginner friendly so it says binary is equal to does not define this operator or a conversion to a type acceptable to the predefined operator it's actually not clear at all what is happening in this situation and why we are having this error so let me show you what is happening let's move this window so that it does not take space and notice this remove function right click on it and click go to definition okay so here we have the definition of our remove function and as you can see that remove function internally is using this is equal to operator in order to compare the elements so that it can remove the right element from the list so it is very easy if that list contains for example numbers it is very easy to compare if one number is equal to another number but since we are using the list to store youtube channels the question is how will this function know that one youtube channel is equal to another youtube channel so how will this is equal to operator behave in that situation so the reason why we are having the error that we are having is because we have not overloaded this is equal to operator for our youtube channel data type so in order to fix the error that we have we need to do that so let's close this file here and i will go to my youtube channel structure and here i will meet to overload is equal to operator so i will say bool and then let's call it operator is equal to okay and here we are going to pass only one parameter of type youtube channel okay let's call it channel and here i will say return this dot name is equal to channel dot name like this now a better way to implement this is to create an additional attribute inside this youtube channel struct called id or identifier and then to compare the ids of two youtube channels instead of comparing the names but this is going to be enough for demonstration purposes i'm going to leave that part to you if you want to practice on your own so that is the first thing and then the second thing that i want to explain is why is the return type of this operator is equal to bull the answer is because that is the logical behavior of is equal to operator because it should return the information if two youtube channels are equal so true or false yes or no so that is the reason why the return type of this operator function is bull so after we have implemented this operator is equal to let's run our program and let's see what is going to happen now so if i run the program as you can see we have the error again so what is the error this time so let's see if this window here is of any help and from what i can read these errors these explanations are not helpful at all at least for beginners so let's collapse this again okay so what is happening in this situation in this operator is equal to function what we need to do is we need to say that this function here will not change its parameters so we need to basically swear to our compiler that we will not change the parameters that we have received so in order to do that you need to make that parameter a constant so you need to say const youtube channel and then the name of that parameter and here i will say that this operator is equal to is a const function which means that it will not change its parameters by accident because this function intuitively should not change the values and the parameters it should just compare them and tell you if they are equal or not so if i try to run my program again let's see what is going to happen and we don't have errors anymore and our program works as it should so now we have the information about only one youtube channel and the reason for that is because we added two youtube channels and then we removed one so here our collection contains only this first youtube channel so i will pin this code in the comments in case that you want to use it and i hope that this video was helpful for you to understand what is operator overloading if it was please give it a thumbs up because that means a lot to me and it is also very important for the youtube algorithm one thing that i promised at the beginning of this video is that i am going to show you the results of the analysis of my code for work so let's check that out here i have all of the potential issues and bugs that pbs studio has found and as you can see i have a lot of things that i need to optimize and fix and here are three different levels of priority low medium and high and currently i have 384 potential issues of high priority that i need to fix or optimize so i have a lot of work waiting for me to do and if you also want to analyze your code i am going to leave the link in the description that you can use to download pbs studio for free so i hope that this video was helpful if it was please give it a thumbs up and also share it with someone who would like to learn programming so thank you very much for watching and i am going to see you in some other video bye
Info
Channel: CodeBeauty
Views: 20,018
Rating: 4.9607491 out of 5
Keywords: c++ operator overloading, c++ operator overloading ostream, c++ operator overloading member function, c++ operator overloading not working, c++ operator overloading example, oop c++ operator overloading, c++ operator overloading tutorial, c++ operator overloading for beginners, c++ operator overloading advanced, operator overloading c++, operator overloading in oop, codebeauty, operator overloading, operator overloading error, operator function in c++, what is operator overloading
Id: BnMnozsSPmw
Channel Id: undefined
Length: 35min 18sec (2118 seconds)
Published: Wed Jun 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.