 Welcome to the session on operator overloading. At the end of this session student will be able to write C++ program to overload operator. This video is in continuation with my previous video on compile time polymorphism. Let's start the session with the types of operators. The types of operators are unary, binary and ternary. A unary operator is one that takes a single operand and performs an operation. Examples of unary operators are pre increment, post increment, pre decrement, post decrement, unary plus, negation, etc. A binary operator operates on two operands. Examples are equal to, not equal to, less than, greater than, etc. Operator function can be either member or non-member function. A member function used to overload unary operator doesn't accept any argument. A member function to overload binary operator accepts one argument. A friend function that overloads a unary operator has one argument and friend function that overloads a binary operator has two arguments. When overloading a binary operator using a friend function, the left operand is passed as the first argument and the right operand is passed as the second argument. When we overload a binary operator by using a member function, leftmost operand must be an object or reference to an object of the class. The object on the left side of the operator generates the call to the operator function. For example, object3 equal to object1 plus object2 is equivalent to object1.operator plus object2 is passed as a parameter. C++ defines array subscripting, function calling and class member access as operations. One important restriction applies to overloading these operators. That is, they must be non-static member functions and cannot be friends. We can overload an operator by using a non-member function, which is usually a friend of the class. Since a friend function is not a member of the class, it does not have this pointer. So operands are explicitly passed to the overloaded friend operator function. When overloading the increment or decrement operators, we need to use a reference parameter when using a friend function. If overloading an operator by using a friend or a member function makes no functional difference, then it is usually best to overload by using member functions. However, there is a situation in which overloading by using a friend function increases the flexibility of an overloaded operator. Let's examine this case now. As we know, when we overload a binary operator by using a member function, the object on the left side of the operator generates the call to the operator function and a pointer to that object is passed as this pointer. Now assume a class called cl that defines a member operator plus function that adds an object of the class to an integer. Given an object of the class called ob, the expression ob plus 100 is valid. In this case, ob generates the call to the overloaded plus function and the addition is performed. But what happens if the expression is written as 100 plus ob? In this case, an integer appears on the left. Since an integer is a built-in type, no operation between an integer and object ob is defined. Therefore, the compiler will not compile this expression. But to always position the object on the left could be a significant burden. The solution to the preceding problem is to overload addition using a friend. Do not use a member function in such cases. Friend functions permit operators to be commutative. An operator is commutative if the result is same regardless of the order of its operands. Examples are addition, multiplication, etc. Both arguments are explicitly passed to the friend operator function. As we know, object plus integer has the same meaning as integer plus object. Therefore, to allow both object plus integer and integer plus object, simply overload the function twice. One version for each situation. Thus, when you overload an operator by using two friend functions, the object may appear on either the left or right side of the operator. Now pause the video for some time and find the answer. Once you have done this, you may resume the video. While overloading binary operator using member function, it requires dash arguments a0, b1, c2, d3. The answer is option b1. Now we will discuss overloading unary operator with member function. Unary operator acts on one operand only. If overloaded operator function is a member function, then it will act on the object with which it is called. The example has a class unary operator. CNT is a data member. Operator minus function negates the value of CNT. The function can be called as minus a which is equivalent to a.operator minus function. If object is CNT is 5, then the function will convert it to minus 5. Let's see this program in code blocks IDE. A class name is unary operator, data member CNT, a default constructor, this is a parameterized constructor, and operator minus function. It doesn't accept any argument. And this is a show function which displays CNT. A is an object of unary operator class. When object A is constructed, a parameterized constructor gets called and value 5 is assigned to data member CNT. Minus a, this is a function call. It calls operator minus function. And the value of CNT is 5. That value is negated by using this minus sign. And that value is stored in data member CNT. This operation is performed on object A. The function doesn't return anything. This statement is printed after calling operator minus function a equal to a dot show show function is called. Now current value of CNT is minus 5, that will be displayed. Let's see the output of this program after calling operator minus function a equal to minus 5. Here we'll see overloading of unary minus operator with friend function. Operator minus is a friend of unary operator class. As we know, a friend function that overloads a unary operator accepts one argument. We need to pass the object as an argument. The function is declared inside the class with a keyword friend and defined outside the class. The function can be called as minus a which is equivalent to operator minus and a is passed as a parameter. Let's see the program in code blocks IDE. In the same class unary operator we have declared a friend function operator minus a is an object of unary operator class minus a calls the operator minus function and a is passed by reference. Now we will refer to a. So v dot CNT is 5. It is negated so it becomes minus 5 and the value is stored in v dot CNT. Now a and v both refer to the same memory location. Now the statement is displayed as after calling operator minus function a equal to now a.show function is called now a CNT is minus 5 that will be displayed. This is the output. Let's see the prefix and postfix forms of the increment and decrement operators. Standard C++ allows us to explicitly create separate prefix and postfix versions of the increment or decrement operators. To accomplish this we must define two versions of the operator plus plus function. If the plus plus precedes its operand the operator plus plus function without argument is called. If the plus plus follows its operand the operator plus plus function with integer parameter is called. If we consider integer parameter as x, x will have value 0. The general forms of the prefix and postfix plus plus and minus minus operators operator functions are shown here. Let's see a program with prefix and postfix forms of the increment and decrement operators. A class is LOC. It has two data members latitude and longitude, a parameterized constructor, a function show to display the values and four operator functions. Object one is an object of LOC class. When object one is constructed a parameterized constructor gets called and latitude and longitude are assigned values 10 and 20 respectively. Object one dot show will show the values of latitude and longitude. Now plus plus object one this calls operator plus plus without any argument and it increments the value of latitude and longitude by one. So the values will become 11 and 21. Now object one dot show it will display the values 11 and 21. Now object one plus plus it will call the postfix version with integer parameter and the values of latitude and longitude are incremented by one. So it will become 12, 22. Now minus minus object one this will call the prefix form of the operator function. Operator minus minus function will decrement the values of latitude and longitude by one. So the values will become 11 and 21. Object one minus minus this will call the postfix version of the operator minus minus function and the values of latitude and longitude are decremented by one. So the values will become 10 and 20 and the values are displayed. Let's see the output of this program. In this video we have seen the concept of operator overloading. In the next video I am going to cover overloading few more operators. Thank you.