The Basics Of Operator Overloading in C++
A free video tutorial from Anand Mahajan
Senior Programming Instructor
4.3 instructor rating • 10 courses • 17,639 students
Learn more from the full courseObject Oriented Programming in C++
Master the skills in OOPs step by step
15:53:21 of on-demand video • Updated August 2020
- able to write OOPs based programs in a confident and sleek manner.
English [Auto] Know we are going to start with a new topic noctis operator overloading husked we will go to the basics of operator overloading C operator overloading feature allows the programmers to use C++ operators with objects like all the automatic co-presidents relational operators are logical operators. They are not all of them but many of them they can be used with objects. Then we see that operator overloading has taken place a special member function has to be done to overload an operator whichever operator we want to overload we will have to write down a separate operator overloading function to accomplish this task. OK. Such a function is called an operator overloading function. The function that we'll be writing will be responsible for overloading direct particular operator and be card and open running the show. But I will give you an example. You'll be able to understand it fully. C operator overloading means and other use of an already existing C++ operator. For example if I'm going to overload plus operator plus operator has already been defined in C++. I'd drive to ordinary duck type values like I and the values float values are double decker type values but then I will be using the plus operator to add two objects. I would say that I have overloaded that OPERATOR. OK. Now let me give you an example. Suppose we want to add two objects that is a class and we have got two objects RBJ one and object to rather three Optix or be one object and all three. No what we want to accomplish this we want to add these two objects and put the result into a third object like this object are easier to OPG one plus object. Now one thing is very evident that when I would write or be given to the the concern clause would be having many instance variables vich instance variables of these two different objects to be added and how they are to be ordered what logic has to be applied. Now this is something which the compiler does not know. Compiler does not know how to add these two objects because they are not all variables. All four constants of simple are types. So what will happen is we will have to write down an operator overloading function that overloads plus operator. So this is an example of adding two objects so you'll have to write down an operator Roding function and that function will be responsible to overload plus operator. Again I repeat it when I will be giving you an example example program then to that Gordon you'd be able to understand it fully. No. In this case this is very important in this case what would happen is or you and will become the current object. What I want to say is the object on the left side of the operator becomes the object and the object on the right side becomes the passing object. This is a current object would be the object on the right side of the operator that we are going to load and operate on the object on the right side of the operator would be considered to be a passing object. So again I repeat it the first object will become the current object. The second object will become the buzzing object. No I'm going to give the same kind of example suppose we want to make the play two objects and put the result into a third object like this. So if I happen to write all three is equal to one in two will be two again. Compiler does not know which instance variables are they were not to be mighty proud vidoe but you do and what logic is going to be applied. So naturally what we will have to do is not to write down an operator function that overloads multiplication operator. And in this case as I have explained earlier the first object will become that is the object on the left side of the operator that we are going to overload. That will become an object and the object on the right side will become the passing object. And this is another example I'm going to use a relational operator. Suppose I want to compare two objects like this for RBJ one is greater than two. So inadvertently I have terminated the statement with semi-colon which is wrong. No problem if RBJ one is greater then two compiler does not know how to compare these two objects so you'll have to write don't and operator overloading function on this. Is this operator Roding function will be responsible to overlit overload greater than operator. So again in this case the first object will become the current object and second object will be the passing object. So in the next letter I will be giving you example through coding and it so that you are able to understand how to read I don't operate on nodding functions.