Binary Operator Overloading In C++

This section will discuss the Binary Operator Overloading in the C++ programming language. An operator which contains two operands to perform a mathematical operation is called the Binary Operator Overloading. It is a polymorphic compile technique where a single operator can perform various functionalities by taking two operands from the programmer or user. There are multiple binary operators like +, -, *, /, etc., that can directly manipulate or overload the object of a class.

For example, suppose we have two numbers, 5 and 6; and overload the binary (+) operator. So, the binary (+) operator adds the numbers 5 and 6 and returns 11. Furthermore, we can also perform subtraction, multiplication, and division operation to use the binary operator for various calculations.

Syntax of the Binary Operator Overloading

Following is the Binary Operator Overloading syntax in the C++ Programming language.

Example

return_type :: operator binary_operator_symbol (arg)
{
// function definition
}

Here,

return_type: It defines the return type of the function.

operator: It is a keyword of the function overloading.

binaryoperatorsymbol: It represents the binary operator symbol that overloads a function to perform the calculation.

arg: It defines the argument passed to the function.

Steps to Overload the Binary Operator to Get the Sum of Two Complex Numbers

Step 1: Start the program.

Step 2: Declare the class.

Step 3: Declare the variables and their member function.

Step 4: Take two numbers using the user-defined inpfunction.

Step 6: Similarly, define the binary (-) operator to subtract two numbers.

Step 7: Call the print function to display the entered numbers.

Step 8: Declare the class objects x1, y1, sum, and sub.

Step 9: Now call the print function using the x1 and y1 objects.

Step 10: After that, get the object sum and sub result by adding and subtracting the objects using the '+' and '-' operators.

Step 11: Finally, call the print and print2 function using the x1, y1, sum, and sub.

Step 12: Display the addition and subtraction of the complex numbers.

Step 13: Stop or terminate the program.

Example 1: Program to perform the addition and subtraction of two complex numbers using the binary (+) and (-) operator

Let's create a program to calculate the addition and subtraction of two complex numbers by overloading the '+' and '-' binary operators in the C++ programming language.

Example

/* use binary (+) operator to add two complex numbers. */
#include <iostream>
using namespace std;
class Complex_num
{
	// declare data member or variables
	int x, y;
	public:
		// create a member function to take input
		void inp()
		{
			cout << " Input two complex number: " << endl;
			cin >> x >> y;
		}		
		// use binary '+' operator to overload
		Complex_num operator + (Complex_num obj)
		{
			// create an object
			Complex_num A;
			// assign values to object
			A.x = x + obj.x;
			A.y = y + obj.y;
			return (A);
		}		
		// overload the binary (-) operator
		Complex_num operator - (Complex_num obj)
		{
			// create an object
			Complex_num A;
			// assign values to object
			A.x = x - obj.x;
			A.y = y - obj.y;
			return (A);
		}		
		// display the result of addition
		void print()
		{
			cout << x << " + " << y << "i" << "\n";
		}
		
		// display the result of subtraction
		void print2()
		{
			cout << x << " - " << y << "i" << "\n";
		}
};
int main ()
{
Complex_num x1, y1, sum, sub; // here we created object of class Addition i.e x1 and y1	
	// accepting the values
	x1.inp();
	y1.inp();	
	// add the objects
	sum = x1 + y1;
	sub = x1 - y1; // subtract the complex number	
	// display user entered values
	cout << "\n Entered values are: \n";
	cout << " \t";
	x1.print();
	cout << " \t";
	y1.print();	
	cout << "\n The addition of two complex (real and imaginary) numbers: ";
	sum.print(); // call print function to display the result of addition	
	cout << "\n The subtraction of two complex (real and imaginary) numbers: ";
	sub.print2(); // call print2 function to display the result of subtraction
	return 0;
}

Output

Output

Input two complex numbers:
5
7
Input two complex numbers:
3
5
Entered values are:
	5 + 7i
	3 + 5i
The addition of two complex (real and imaginary) numbers: 8 + 12i
The subtraction of two complex (real and imaginary) numbers: 2 - 2i

In the above program, we take two numbers from the user, and then use the binary operator to overload the '+' and '-' operators to add and subtract two complex numbers in a class.

Example 2: Program to add two numbers using the binary operator overloading

Let's create a program to calculate the sum of two numbers in a class by overloading the binary plus(+) operator in the C++ programming language.

Example

/* use binary (+) operator to perform the addition of two numbers. */
#include <iostream>
using namespace std;
class Arith_num
{
	// declare data member or variable
	int x, y;
	public:
		// create a member function to take input
		void input()
		{
			cout << " Enter the first number: ";
			cin >> x;
		}		
		void input2()
		{
			cout << " Enter the second number: ";
			cin >> y;
		}	
		// overloading the binary '+' operator to add number
		Arith_num operator + (Arith_num &ob)
		{
			// create an object
			Arith_num A;
			// assign values to object
			A.x = x + ob.x;
			return (A);
		}		
		// display the result of binary + operator
		void print()
		{
			cout << "The sum of two numbers is: " <<x;
		}		
};
int main ()
{
   Arith_num x1, y1, res; // here we create object of the class Arith_num i.e x1 and y1	
	// accepting the values
	x1.input();
	y1.input();	
	// assign result of x1 and x2 to res
	res = x1 + y1;	
	// call the print() function to display the results
	res.print();	
	return 0;	
}

Output

Output

Enter the first number: 5
Enter the second number: 6
The sum of two numbers is: 11

In the above program, we take two numbers, 5 and 6, from the user and then overload the binary plus (+) operator to perform the addition that returns the sum of two numbers is 11.

Example 3: Program to perform the arithmetic operation by overloading the multiple binary operators

Let's create a program to overload multiple binary operators in a class to perform the arithmetic operations.

Example

/* use binary operator to perform the arithmetic operations in C++. */
#include <iostream>
using namespace std;
class Arith_num
{
	// declare data member or variable
	int num;
	public:
		// create a member function to take input
		void input()
		{
			num = 20; //define value to num variable
		}
		// use binary '+' operator to add number
		Arith_num operator + (Arith_num &ob)
		{
			// create an object
			Arith_num A;
			// assign values to object
			A.num = num + ob.num;
			return (A);
		}
		// overload the binary (-) operator
		Arith_num operator - (Arith_num &ob)
		{
			// create an object
			Arith_num A;
			// assign values to object
			A.num = num - ob.num;
			return (A);
		}	
		// overload the binary (*) operator
		Arith_num operator * (Arith_num &ob)
		{
			// create an object
			Arith_num A;
			// assign values to object
			A.num = num * ob.num;
			return (A);
		}		
		// overload the binary (/) operator
		Arith_num operator / (Arith_num &ob)
		{
			// create an object
			Arith_num A;
			// assign values to object
			A.num = num / ob.num;
			return (A);
		}		
		// display the result of arithmetic operators
		void print()
		{
			cout << num;
		}		
};
int main ()
{
	Arith_num x1, y1, res; // here we created object of class Addition i.e x1 and y1	
	// accepting the values
	x1.input();
	y1.input();	
	// assign result of x1 and x2 to res
	res = x1 + y1;
	cout << " Addition : " ;
	res.print();	
	// assign the results of subtraction to res
	res = x1 - y1; // subtract the complex number
	cout << " \n \n Subtraction : " ;
	res.print();	
	// assign the multiplication result to res
	res = x1 * y1;
	cout << " \n \n Multiplication : " ;
	res.print();	
	// assign the division results to res
	res = x1 / y1;
	cout << " \n \n Division : " ;
	res.print();
	return 0;	
}

Output

Output

Addition : 40
Subtraction : 0
Multiplication : 400
Division : 1

In the above program, we declare the value of variable num is 20 and then overload the binary plus (+), minus (-), multiply (*), and division (/) operator to perform the various arithmetic operations in the Arith_num class.

Input Required

This code uses input(). Please provide values below: