Coupon Accepted Successfully!


Overloading Binary Operators

Overloading with a single parameter is called binary operator overloading. Similar to unary operators, binary operators can also be overloaded. Binary operators require two operands, and they are overloaded by using member functions and friend functions.

  1. Overloading binary operators using member functions
    If overloaded as a member function, binary operators require one argument. The argument contains value of the object, which is to the right of the operator. If we want to perform the addition of two objects o1 and o2, the overloading function should be declared as follows.
    We learnt that a member function can be called by using class of that object. Hence, the called member function is always preceded by the object. Here, in the above statement, the object o1 invokes the function operator() and the object o2 is used as an argument for the function. The above statement can also be written as follows.
    Here, the data members of o1 are passed directly and data members of o2 are passed as an argument. While overloading binary operators, the left-hand operand calls the operator function and the right-hand operator is used as argument.
  2. Overloading binary operators using friend functions
    The friend functions can be used alternatively with member functions for overloading of binary operators. The friend function requires two operands to be passed as arguments.
    Both the above statements have the same meaning. In the second statement, two objects are passed to the operator function.
    The use of member function and friend function produces the same result. friend functions are useful when we require performing an operation with operand of two different types. Consider the statements
    where X and Y are objects of the same type. The first statement is valid. However, the second statement will not work. The first operand must be an object of the same class. This problem can be overcome by using friend function. The friend function can be called without using an object. The friend function can be used with standard data type as left-hand operand and an object as right-hand operand.

The following programs are illustrated based on the above discussion.

10.7 Write a program to overload + binary operator.


Explanation: In the above program, binary operator + is overloaded. Using the overloading operator +, addition of member variables of two objects is performed and results are assigned to member variables of third object. In this program X, Y, and Z are objects of class num. The statement Z=X+Y invokes the operator function. In this statement, the object y is assigned to object t of operator function and member variables of X are accessed directly. The object tmp is used for holding the result of addition, and it is returned to object Z after function execution. The function show() displays the values of three objects.

10.8 Write a program to perform multiplication using an integer and object. Use friend function.


Explanation: In the above program, the equation Z=3*X contains integer and class object. We know that the left-hand operand is always used to invoke the function and the right-hand operand is passed as an argument. In such type of equations, member functions are not useful because the left-hand operand is integer and cannot invoke the function. Hence, the function operator *() is declared as friend. The friend function calls the operator *() and carries the multiplication of each member variable by three. The results are displayed in the output.

Test Your Skills Now!
Take a Quiz now
Reviewer Name