Operator Overloading in C++ Programming | C++ Programming for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] yo what's going on guys Stan Meyer for simple snippets back with another video tutorial on C++ programming so in this video tutorial we'll be covering up the topic of operator overloading now if you are new on this channel make sure to subscribe to this channel because there are a lot of computer science and information technology oriented video tutorials on this channel and you can check it out and you can turn on the notifications as well so that you will get notified whenever I upload the new video tutorial now if you're a subscriber and if you are already watching a lot of video tutorials and if you've been following this entire C++ programming playlist in the previous video tutorial we covered static data members and member functions so you can check it check it out and you can see a video tutorial card on the top right corner so now that you've subscribed to this channel let's get started with today's topic so we'll first go through a little bit of theory of what operator overloading is and then later on we'll also see an example because that will give you the best idea and you'll get a clearer understanding of how operator overloading helps us so make sure you watch this entire video because in the end we will cover the programming part and now let's first go through the theoretical aspect of operator overloading so C++ allows you to specify more than one definition for an operator in the same scope which is called operator overloading okay so this is one simple way to put what operator overloading is so basically what we are doing is we are enhancing the functionality of the operators in C++ so what that means is you can redefine an overload most of the built-in operators available in C++ so just like function overloading we can also order operators now we've already talked about function overloading extensively and you can see a card on the top right corner if you don't know what function overloading is so similarly we can overload operators as well which means that will enhance and add new functionalities to the predefined operators now it is again a type of polymorphism in which an operator is overloading and it gives a new definition and meaning to it so in C++ almost any operator can be overloaded but there are few exceptions and these are the few exceptions which cannot be overloading you can see we cannot overload scope operator or scope resolution operator which is the colon colon operator then we cannot overload sizeof operator we cannot overload member selector member pointer selector and ternary operator now apart from these all the operators can be overloaded so now you have a little of theoretical understanding of what operator overloading is participating as similar to function overloading but then here we are given even functionality to the operators and not the functions so now let's move on to the programming aspect and I would recommend you that you type along with me the code so that you better understand what the programming is and that's the best way to understand and learn programming okay so quickly open up your day C++ ID and you can see I have already typed in a little bit of code so what I've done is I've created a class over here you can see class complex and let me just first tell you what exactly are we going to achieve in this program so this is the main function you can see I haven't typed in anything else so what you can do is you can pause this video and type type out the code along with me so now that we have typed in this code let me just explain to you what exactly are we going to do now in the int main function you can see that we can create basic or primitive or predefined data type variable so say int X is equal to 5 and Y is equal to 4 and now what I can do is I can say int Z is equal to X plus y so when I say see out result value print Z you know that Z will have 5 plus 4 that is 9 so in this case this operator plus already knows what to do and basically what it is doing is it is taking the value of X and it is adding up with Y ok now this is pretty much clear and pretty much understandable but what if I want to create my own object of a class so you can see I have created a class complex over here so I will say complex c1 now inside the parametrized constructor you can see I've created a parameterised constructor or I'm passing two values so let me just first it is this out ok so I have two data members in this class that is real an image that is imaginary so a complex number is something which has a real part and an imaginary part and it is a concept of mathematics so not going to go in too much of a detail but here what we are doing is in the parameterize concept and I'm passing 5 comma 4 ok so I have created object c1 which is a complex object and I have created these and pass these values again I go ahead and say C 2 is equal to 2 comma 3 and now what I'm going to do is and sing complex C 3 I'm not passing anything so by default it would have some random values in fact I can create a default constructor over here and assign these values as zero so in the default constructor on say the DL is equal to zero and they're equal to zero so for C the default constructor is called so you've already talked about constructors and destructors if you don't know what they are you can check out this video on the top right corner I have just posted a card so here what if I want to do C 3 is equal to C 1 plus C 2 so in this case you can see I have created an object which is a user-defined class so this class is created by me so I have C 1 which has 5 & 4 and I have C 2 is just 2 & 3 now I've created C 3 and what I'm expecting is I want C 3 is equal to C 1 plus C 2 so what I'm expecting is 5 that is the real part of C 1 should be added with the real part of C 2 so this is 2 so the C 3 should have 7 and again 4 plus 3 would be 7 so let's change this let's do 5 so 4 plus 5 should be 9 so what c3 should have is C 3 should have 7 comma 9 this is what I'm expecting because I've just inserted a plus operator over here but the problem here is let me just comment this out so the problem here is this plus operator does not know anything about C 1 and C 2 and the data type of C 1 and C 2 basically it is a user-defined data type so it does not know that we have to have the real part with the real part and imaginary part with the imaginary part so if I save this and if I say compile and run it will give me an error so you can see it is showing me error no match for operator plus you didn't see this error exactly over your operator plus so this means that the operators the C++ operators it is plus minus plus plus and greater than equal to and so on do not directly operate on the objects the way they operate on primitive data types so if I were to comment this part out and uncomment this part so your what is happening is this these are the primitive data types so your the plus would work let me just see you this in FAC compile and run here you can see this works perfectly fine but it does not work for our user-defined objects so what my operator overloading will help me do is I will overload this operator plus in a way that it will understand what to do with C 1 and C 2 because their user-defined objects of class so now we are going to perform operator overloading and I'll show you how to go about it let me just cut this part so in order to perform operator overloading we have to overload or use of special function so let me just first type it all down and explain to you what it is now this function has to be inside the class because it is going to be applicable only for this class so here I'll write complex operator so this is a keyword that you have to use and then we have to specify which operator are you going to overload so you just give a space and the operator to be overloaded is binary operator binary means it is going to perform operation on two operands so you can see there are two operands over here c1 and c2 so else your operator plus inside opening and closing brackets I'm going to pass complex C so I'm going to pass a variable or pass the argument whose data type is also same as that of the class and I'll explain you why we have to do something like this let me just first create the function inside the function I'm going to create one more complex object complex temp now what I'm going to do is and when we see them but DL is equal to DL plus C dot real similarly implicit them not images equal to real dot is equal to image plus C dot image ok and lastly I'm going to say return temp ok so I just created a function now this function is a special function because we are using the keyword operator over here so this is how we overload operators so this is the operator overloading syntax and the way the way we go about the syntax is we first write down the return type in this case we are returning a complex object that is the reason why we have this written type it can be void also or any other data type or user-defined data type of class name but this can vary but this part the operator keyword has to be always there in operator overloading so if you are up or loading any other operator say for example increment operator operator that is plus plus or decrement operator that is minus minus or any other operator for that matter you have to use this keyword operator and then followed by the operator itself so if you were overloading minus operator it would be minus over here and then since this is a function okay operator overloading basically is a function but it is a special function so we have opening and closing around brackets like what functions have and then again the data type or any argument to be passed is or over here now in this case we are passing an object of the same class and I'll explain you why exactly is being are we doing this so inside this function what we are doing is we are creating a temporary object of the same complex type that is this class that we've created we say temp dot real that is the data member of this object is equal to real now this real corresponds to a real data member of c10 drienne corresponds to Delta member of data member of c2 so I will tell you what exactly is happening let me just remove the comments over here this line you can see line number 39 the way this works is or the way the compiler or program sees this is c1 dot add c2 so what exactly is this so this can be equated to this something like this so your c2 is being passed into the function c1 is the object that is calling this function so the really the just real data member is for c1 the just image function is for c1 the C dot real and the C dot image is because we have passing c2 so the C 2 is passed over here and that is acting as this part and then the temp is what we are creating inside of the function just to hold the values just to hold the addition values and then we are returning them and since temp return type is complex that is the data type of temp is complex that is where the return type is over here complex and what exactly is will happen is the addition of c1 and c2 will return a complex object which will be stored in C 3 so essentially C 3 would be equal to C 1 dot add so this is something which is being interpreted by the compiler or understood by the compiler or the program so we are writing C 1 plus C 2 that is for our ease of use and understandability so that that is what operator overloading is giving us so we don't have to write C 1 dot we don't have to create multiple functions now if I create one more object let's create C 3 only I will say 1 comma 1 and if I say complex C 4 and if I say C 4 is equal to C 1 plus C 2 plus C 3 so what will happen is addition of these two will happen then the result of this would be added with C 1 and then lastly it will be stored in C 4 otherwise I would have had to call the add function two times if I would have created a separate add function so this advantage is what operator overloading gives and let me just save this and let's try to run this but before that let's create a print function you can see we have already only have a print function over here which is printing the real and imaginary part so I'm saying C 4 is equal to C 1 plus C 2 plus C 3 so I will just say C 4 dot print so let's see watered prints it should print 5 plus 2 plus 1 which is 8 plus 10 I it should print 8 plus 10 I so it would be the real part and then would be the imaginary part let's save this let's try to execute this I will say compile and run and there you go you can see the output 8 plus 10 I which means that the operator was overloaded successfully initially when I did not have this operator overloading function you remember that it was throwing us an error when I was trying to add the objects C 1 C 2 but now that I've created this special function which is an operator overloading function now the plus operator knows that when a object of complex data type is to be added it knows that it has to add the imaginary part of C 3 with the imaginary part of C 2 and real part of C C 3 with real part of C 2 and same goes one with C 1 and C 2 so that's how it works and this was just an example of overloading the plus operator which is a binary operator in this case because it is operating on two operands at a time so we have binary operator then we have unary operator now unary operator would be something like increment operator which is plus plus or minus minus so it would require only one operand right then I would say C 1 plus plus which means that it is only needing one operand so this type is known as unary operator then we also have ternary operator which operates on three operators but we cannot overload that ternary operator you can see or you've seen just that just now in the slide show anyways this was just an example of how operator overloading works and you if you are still wondering what exactly is the advantage of operator overloading is is that it gives us more flexibility of using operators with our user-defined data types so instead of creating functions for adding and subtracting you can directly use these operators with our user-defined objects but then you have to perform operator overloading and this gives us more intuitive way and this is more easy to understand right C 1 plus C 2 and then behind the scenes the compiler or the program already knows that it has to add 5 with 2 and 4 with 5 so it gives us more understandability and more flexibility by during these operators so yeah that's it for this video guys I hope you understood what operator overloading is and how it works and what are the benefits and we also saw an example so it must be very clear both theoretical and practically so if you liked this video give it a thumbs up share it with your friends and make sure you subscribe to this channel peace
Info
Channel: Simple Snippets
Views: 160,289
Rating: undefined out of 5
Keywords: operator overloading, overloading, c++, operator, operator overloading in c++, operator overloading in c++ tutorial, c++ programming, operator overloading in c++ in english, overloading operators c++ example, overloading operators c++, overloading operators c++ tutorial, overloading binary operators c++, c++ operator overloading, c++ operator overloading explained, overloading + operator c++, simple snippets
Id: BO2KagRMS3M
Channel Id: undefined
Length: 13min 32sec (812 seconds)
Published: Fri Feb 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.