Increment & Decrement Operator Overloading in C++ | Unary Operator Overloading Program Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] yo what's going on guys then Maya for simple snippets back with another video tutorial on C++ programming especially the operator overloading part so in this video tutorial we'll be performing operator overloading on a unary operator especially the increment and decrement operators so we already know that increment operator has two different types that is pre increment and post increment and similarly the decrement operator also has three and post parts so there are at four different cases over here so we are directly going to jump into the programming part now if you don't know what operator overloading is we've already covered extensively the theoretical aspect of operator overloading and we also overloaded the addition operator in the previous video tutorial so if you don't know the theoretical aspect and the concept of operator overloading you can check out this video on the top right corner and you can just take out the first few minutes so that you get a good idea about the theoretical aspect and then come back to this video but if you already know what it is then this program is or this video tutorial is going to be very easy because we are directly going to jump into the practical aspect also if you're new on this channel make sure you subscribe to this channel because there are a lot of IT video tutorials on this channel and more are going to be coming soon so you will get notified whenever I upload a new video tutorial so with that being said let's get started okay so quickly open up your day C++ ID and type out this program that I have already typed in so there's nothing great over here I just have created our own class so the class name is wait in the private part I have one data member that is int cagey in the public part I have a default constructor I have a parameterised constructor and I have a basic print message so that's about this class you can pause this video and type it out because I would recommend you that you type the program and that will give you the best practice now in the main function I don't have anything right now so that's where we actually will start off in the coding that's why I already typed this out so that we don't waste much of the time so now what we will do is we're going to perform operator overloading especially we are going to overload the unary operators so the unary operators as I mentioned to you that we are going to overload are the increment and decrement operators so in general let's let's assume we are performing we are not performing operator overloading we just have basic datatypes so I have int X is equal to 0 now if I say plus + X and if I say C out X so you know that X is going to be incremented by one so this works on primitive datatype perfectly that is the plus plus operator already knows that it has to add one value to this variable X of integer datatype right but what if I create a object of WEEI ght weight class so we know that weight is a user-defined class over here so when I create an object it is assigned to zero that is the data member kg is assigned to 0 because default constructor is called and what if I say plus plus obj now if I do this this is going to be throwing me an error because this plus plus doesn't know what to do with this object because this object is of type class and it is a user-defined type so it doesn't know that it has to add one value to the kg data member so this is something that we have to explicitly define once and this is essentially the hardware operator overloading comes into picture that is we are going to give this plus plus operator a new functionality so now this is pre increment operator similarly we also have X plus plus that is the portion increment operator again so we also can do obj plus plus over here so these both these things are different and the basic difference is the pre increment operator increments the value first and then the changes happen and the post increment operator increments the value later after an operation and to give you an example let me just first cut this out and say X is equal to 0 and Y is equal to 0 and if I see Y is equal to X plus plus soyou're what is going to happen is Y is going to remain 0 and X is going to be 1 in this case so what happened is 4 is the value of x that is the RHS side is first assigned to Y and then X value is incremented by 1 so initial value of X was 0 so that 0 was forced transferred to Y and then the plus plus operator that is the post increment operator operated on this x value to increment it by 1 but instead of this if we had put Y is equal to plus plus X what would have happened is X muda first incremented so its value would have been 1 and then it would be transferred to Y so go and you can check it out on your own and you can try this different combinations and you'll understand the basic difference between the two the post increment and pre increment and similarly we have three decrement and decrement operator so both are the same that is the working of the operators are similar so they're different in functionality so that's why we have to overload the operators for different times and this functionality is what we are going to achieve for our user-defined class object okay so let's let's start off with the overloading part so operator overloading is carried out by a special operator function now this is a unary operator so it is not going to perform operations on two operands it is just going to take one operand so I'm going to write void then use the keyword operator then say which operator you want to overload let's start off with increment pre increment operator and I'll tell you how to overload the post increment as well give the opening and closing round brackets of a function the body of the function and inside this what do you want to do is we want to increment the value of kg so kg is a data member of weight class so we just want to increment it by one okay so this this is pretty much about it and this should work let's let's try to create a object I'll say create obj and I'll say opj dot print so print waiter is a member function which prints the weight in kg so when the object is first created the value is assigned as zero by the default constructor so it should have zero let's try to save this in let's try to execute this so I'll say compile and run and there you can see weight in kg zero now what we are going to do is let's try to increment this object I'll say plus plus obj so I'm using the pre increment operator because we have overloaded the pre increment operator only and I'll tell you the difference between pre and post so let's just say this go to execute compile and run and there you go the function ran successfully but we forgot to print the message so let's let's just again see obj dot print wait see you this go to execute compile and run and there you go you can see the operator overloading worked successfully because the weight in kg was incremented by one okay so the pre increment operator is already successfully how do we overload post increment so the function is going to be the same for post increment as well just that in this case inside the bracket you have to pass a parameter of int so this is just to differentiate between the pre and post increment operator and insert this you can do the post increment operation on the kg remember just save this and now what we will do is we will try to say obj plus plus again say obj dot print wait let's try to see if post increment operator is already successfully let's see you this go to execute compile and run and there you go you can see even the post increment was executed successfully you can see we it in kg is equal to two so this is great right we just performed operator overloading on a unary operator and we overloaded the pre and post increment operators now the reason why we did not pass any object is because this is a unary operator and it operates on this object itself so when I say obj plus plus basically what I am doing is I am doing obj dot this function so this is essentially what the compiler sees okay so I'm just going to comment this out I'll just put it over here so when I say Oh DJ plus plus obj dot operator plus plus is going to be called behind-the-scenes now similarly I can just completely copy and paste these two functions for the pre-decrement and pull decrement operators and inside these overloaded operators I will just say - - over here and - - over here so again this should also work perfectly fine so I will say obj or I'll say - - obj again I'll say obj dot print message or print weight and I will say obj - - which is pushed decrement operator I'll say obj burg print message or print we'd see you this and I think it should run successfully let's start to run this and there you go you can see initially we increment in the value by one so kg was one then we again implemented using the post increment operator so the kg value was 2 then we decrement the value by using the pin decrement and we decremented it again by using post decrement so all the four operators are overloaded successfully so this is pretty much about the overloading now there is one more thing that you can do in this case so let's let's say we have in DJ variable as I say index is equal to zero I'll say in Y is equal to zero now we know that we can also do this kind of functionality over in the basic data type so I'll say Y is equal to plus plus X so what I am doing is I am incrementing the value of x and then assigning it directly to Y now let's try to do this with our objects as well let me just cut this out and I will say wait obj one comma obj - now what I will do is obj two is equal to plus plus obj one and if I try to run this I'm gonna get an error so let me just first it is this out because this is working perfectly fine but what we are doing now is we are incrementing the value of obj1 and directly trying to assign it to obj two so this is going to throw an error and I will tell you why it is going to throw an error let me just try to save this and try to run this and you can see it is showing an error at this line and it is saying no match for operator equal two so what exactly is happening over here is because the operator overloaded function has a return type of void okay so you can see all the four operators that we just overloaded over here these are the overloaded operators have a return type of void and at this sentence what is happening is obj one is on the RHS or the right hand side of equal to operator right so what essentially is going to happen is whenever plus plus is going to happen on obj one that is increment is going to happen in obj one it is going to be stored in obj - so the function being called over here is the pre increment operator we know this function is called but it is on the RHS so it has to return some value and then it has to be assigned to obj - so basically it has to return an object of weight type and assign it to obj - right so this is what it has to do but since the operator oh load function has a written by avoid this kind of scenario that is plus plus obj one on the RHS of equal to is not allowed so there is a workaround through this and what we can do is instead of wild we can say aw e IG HT wait the return type in cleared as weight inside this what we can do is we create an emperor e object of weight we will save temp dot kg is equal to plus plus kg and lastly we will say return temp so what this will do is now it has a return type of weight and we you can see that we have created a temporary weight object the temporary weight object has a data member kg and what we are doing is we are saying plus plus kg so this plus mesquite is of object one over here so even that value is incremented so object one is also in the kg data member of object one is also incremented and after returning tenth this new value is returned to object to as well so even that is achieved so let's try to see you this in let me see if this happens successfully friend say obj two dot print message or print wait and what it should print is it should print one so let me just go and compile and run and there you go this ran successfully this time it did not give us an error because this time the return type was wait and you can see we did in kg is 1 the reason why it printed 1 is because when we created the object it was assigned to 0 but then we sailed object 2 is equal to plus plus obj 1 similarly this kind of functionality can be applied to all the other three that is you can change the return type of all these 3 operator overloading shion's to wait and similar mechanism inside can be applied over here it can be applied over here and over here so what I will do is I will give this as a assignment to you guys what you in do is you can try this out and then let me know in the comments how it went and if you have any queries and this is specially then just if there is an assignment along with the increment or decrement so now I can also boot this as well so I can say plus plus obj 2 also it is not necessary that it has to be on the RHS only even this can be valid because ultimately even the kg value is also incremented yes that the return value over here is not being caught by any other object on the LHS but kg value of object 2 is also incremented so our purpose is also being solved over here so this serves as the 2 way solution and this serves as a one-way solution ok so this was about operator overloading and performing unary operator overloading especially the increment and decrement operators and we also saw all the four different types of increment and decrement operators that is the three increment and post increment and pre-decrement and posed agreement so that's it for this video guys I hope you understood how to perform operator overloading on unary operators and this was completely practical part so if you have any queries you can put them in the comment section if you like this video let me know in the comments and give it a thumbs up also don't forget to share this video and if you haven't subscribed make sure you subscribe to this channel and I'll talk to you guys in the next video tutorial peace
Info
Channel: Simple Snippets
Views: 36,916
Rating: undefined out of 5
Keywords: operator overloading simple snippets, operator, decrement operator overloading im c++, increment and decrement operator overloading in c++, overloading, increment operator overloading in c++, c++ increment operator overload, operator overloading in c++, c++, c++ decrement opertor overloading, operator overloading, c++ unary operator, increment and decrement operators in c++, pre increment operator overloading in c++, simple snippets, post increment operator overloading in c++
Id: b3rmSbqpevk
Channel Id: undefined
Length: 13min 16sec (796 seconds)
Published: Sat Feb 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.