This part will cover the concept of Binary Operator Overloading in C++ programming. Binary Operator Overloading refers to an operator that works with two operands to execute a mathematical operation. It is a form of polymorphism during compilation where a single operator can execute different actions based on the two operands provided by the user or programmer. Various binary operators such as +, -, *, /, and more are available to directly modify or overload class objects.
For instance, imagine we possess two values, 5 and 6, and decide to overload the addition (+) operator. Consequently, when the binary (+) operator is applied to 5 and 6, the result will be 11. Additionally, we have the flexibility to carry out subtraction, multiplication, and division using the overloaded binary operator to facilitate diverse mathematical computations.
Syntax of the Binary Operator Overloading
Here is the syntax for Binary Operator Overloading in the C++ programming language:
return_type :: operator binary_operator_symbol (arg)
{
// function definition
}
Here,
return_type: It specifies the data type of the value that the function will return.
The operator keyword is utilized in function overloading.
The binaryoperatorsymbol signifies the symbol of a binary operator that is overloaded in a function to carry out computations.
It specifies the parameter supplied 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: Define the variables and their corresponding member functions.
Step 4: Obtain a pair of numerical values by utilizing the custom inp function created by the user.
Define the subtraction operation for two numbers by specifying the binary (-) operator in the same manner as done for addition.
Step 7: Invoke the print function to showcase the numbers that have been inputted.
Declare the class instances x1, y1, sum, and sub in Step 8.
Step 9: Proceed to invoke the print method utilizing the x1 and y1 instances.
Step 10: Following that, calculate the sum and subtraction outcomes by using the addition and subtraction operators on the objects.
Finally, invoke the print and print2 functions by passing in the arguments x1, y1, sum, and sub.
Step 12: Demonstrate the sum and difference of the complex numbers.
Step 13: Stop or terminate the program.
Example 1: Code to execute the addition and subtraction operations on two complex numbers by utilizing the binary addition (+) and subtraction (-) operators.
Let's develop a software to compute the sum and the difference of two complex numbers by overloading the addition '+' and subtraction '-' operators in the C++ programming language.
/* 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
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 program mentioned earlier, we prompt the user to input two numbers. Subsequently, we utilize operator overloading with the binary operators '+' and '-' to perform addition and subtraction operations on two complex numbers within a class.
Example 2: Code to sum two values with binary operator overloading
Let's develop a program in C++ to compute the total of two numbers within a class by overloading the binary plus (+) operator.
/* 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
Enter the first number: 5
Enter the second number: 6
The sum of two numbers is: 11
In this program example, we input two values, 5 and 6, provided by the user. Subsequently, we redefine the binary addition operator (+) to execute the sum operation, resulting in the output of 11 as the total of the two numbers.
Create a program that demonstrates performing arithmetic operations by overloading various binary operators.
Let's develop a software to overload various binary operators within a class in order to execute arithmetic calculations.
/* 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
Addition : 40
Subtraction : 0
Multiplication : 400
Division : 1
In the preceding code snippet, we initialize the variable num with a value of 20. Subsequently, we implement operator overloading for the addition (+), subtraction (-), multiplication (*), and division (/) operators to enable different arithmetic computations within the Arith_num class.