 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 videos on compile time polymorphism and operator overloading part 1. Let's see an example of overloading binary operator as a member function. The class name is distance. It has two data members, feet and inch of type integer. Operator plus is a member function which accepts an object by reference. It performs the addition of two objects and stores the result in third object d3. As we know, food comprises of 12 inches. So, if inch is greater than or equal to 12, we divide it by 12 and add the quotient to feet and remainder is stored in inch and then the object d3 is returned. The statement d3 equal to d1 plus d2 is equivalent to d1 dot operator plus d2. It calls operator plus function. 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 a this pointer. So, operands are explicitly passed to an overloaded friend operator function. Let's see an example of overloading binary operator plus as a friend function. It is declared with friend keyword and defined outside class without using scope resolution operator. The operator plus function accepts two objects by reference, adds the values and stores the result in third object d3 and returns d3. The statement d3 equal to d1 plus d2 is equivalent to operator plus d1 comma d2. It calls friend function and adds the values of d1 and d2. If overloading an operator by using a friend or member function makes no functional difference, then it is usually best to overload by using member function. 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. Further, 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 operator 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 an object ob is defined. Therefore, the compiler will not compile this expression. 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 case. 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 we overload an operator by using two friend functions, the object may appear on either the left or right side of the operator. Let's see an example of commutative friend function. A program has class distance. It has two data members, feet and inch. It has two constructors, default and parameterized constructor. A display function to display the values. Two overloaded plus operator functions. These are friend functions. First function accepts parameters distance by reference and integer. And the second friend function accepts parameters integer and distance by reference. D1 and D2 are objects. When D1 is constructed, a parameterized constructor gets called and values 8 and 9 are assigned to feet and inch respectively. When D2 is constructed, a default constructor gets called and it initializes the values of feet and inch to 0. The statement D2 equal to 100 plus D1 calls overloaded operator plus function with integer and distance parameters. T will have value 100 and X will refer to object D1. A temporary object D3 is constructed. D3 dot feet equal to X dot feet plus T. X dot feet is 8 because D1 and X both are same. X dot feet is 8, 8 plus 100, 108 and X dot inches 9. 9 plus 100 is 109. These values are stored in D3 dot feet and inch respectively. As we know, one foot is 12 inches. So here we are checking if D3 dot inch is greater than or equal to 12. And we are dividing total inches by 12 so that we will get the quotient and that quotient is added to feet. And the remainder is stored in inch and the object D3 is returned and that will be assigned to object D2. Now D2 dot display will call a display function and it will display the feet and inch of object D2. Now the statement D2 equal to D1 plus 200. It calls overloaded operator plus function with distance and integer parameter. Now X refers to D1 and T has value 200. Now again the same operation is performed. The object D3 is returned and it is assigned to object D2. D2 dot display will display feet and inches. Let's see the output of this program. Total feet and inches 117 feet 1 inch. Total feet and inches 225 feet 5 inches. Now let's see the concept of overloading stream insertion and stream extraction operators. These operators are overloaded to perform input output for user defined types. Left operand is of type O stream or I stream. I stream and O stream are classes. If an operator is overloaded as a member function, it must be a member of the class whose object is specified on the left side of the operator. For example, consider the statement object1 plus object2. Let object1 and object2 be objects of two different classes. To make this statement compile, we must overload plus in class of object1 or make operator plus a friend function. Operator insertion or extraction function must be a non-member function because left operand is not an object of user defined class. It must be a friend function to access private data members. Input and output operations can be performed on user defined types the same way as built-in types. D1 is an object of distance class. C in extraction D1 gets the input and C out insertion D1 displays it. We have used object D1 in input and output statements just like variables of built-in types. This can be done by overloading the operators, extraction and insertion. Let's see an example of overloading insertion and extraction operators. A distance class, data members feet and inch, two constructors and two operator functions. First operator function is operator insertion function which is a friend function. And the other operator function is operator extraction which is also a friend function. Now when object D1 is constructed, a parameterized constructor is called and it initializes feet and inches to 12 and 10 respectively. When object D2 is constructed, a default constructor gets called and it initializes 0 to both feet and inches. Then a statement enter the values of object D2 is displayed. C in extraction D2 calls operator extraction and the first parameter is I stream by reference input and the second parameter is distance by reference D. Here D refers to D2 and we are getting the input from user for feet and inches and return input. We are returning the reference of I stream class because we can combine the extractions like C in D1, extraction D2. Now a statement C out insertion D1 calls operator insertion function. The first parameter is O stream by reference output and the other parameter is distance by reference D. D refers to D1 and D dot feet and D dot inches is printed and the reference to O stream class is written because we can combine the insertions like C out D1 insertion D2. C out insertion D2 once again calls the operator insertion function and it displays the values for object D2. Now let's see the output. First I have to enter the values for object D2. I have entered 2 for feet and 4 for inches. So for object D1 the feet is 12 and inches 10 for object D2 feet is 2 and inches 4. Now pause the video for some time and overload operator equal to equal to in the given code to make the program error free. Once you have done this you may resume the video. The answer is shown here. The statement B1 equal to equal to B2 calls operator equal to equal to function. As operator equal to equal to is a member function object B1 is passed implicitly. Object B2 is passed by reference. The class has a data member capacity of type integer. If capacity of object B1 is equal to capacity of object B2 it returns 1 else returns 0. If return value is 1 equal is printed else not equal is printed. In this video we have seen the concept of operator overloading. Thank you.