Experiment no.
5
Date :
Title : Write a program in C++ to perform following operations on complex
numbers Add, Subtract, Multiply, Divide. Use operator overloading for these
operations.
Objective : The objective of this assignment is to learn the concept operator overloading.
Software : Dev C++ IDE
Theory :
• Binary Operators –
Operators that operate on 2 operands are known as binary operators.
Like unary operators, binary operators can also be overloaded.
• Syntax of the Binary Operator Overloading
Following is the Binary Operator Overloading syntax in the C++ Programming language.
1. return_type :: operator binary_operator_symbol (arg)
2. {
3. // function definition
4. }
Here operator is a keyword and symbol is the operator to be overloaded.
When we overload the binary operator for user-defined types by using the code:
obj3 = obj1 + obj2;
The operator function is called using the obj1 object and obj2 is passed as an argument to
the function.
Things to Remember in C++ Operator Overloading
1. Two operators = and & are already overloaded by default in C++. For example,
to copy objects of the same class, we can directly use the = operator. We do not need
to create an operator function.
2. Operator overloading cannot change the precedence and associativity of operators.
However, if we want to change the order of evaluation, parentheses should be used.
3. There are 4 operators that cannot be overloaded in C++. They are:
a. :: (scope resolution)
b. . (member selection)
c. .* (member selection through pointer to function)
d. ?: (ternary operator)
In this program, the operator function is:
Complex operator + (const Complex& obj) {
// code
}
Instead of this, we also could have written this function like:
Complex operator + (Complex obj) {
// code
}
However,
• using & makes our code efficient by referencing the complex2 object instead of
making a duplicate object inside the operator function.
• using const is considered a good practice because it prevents the operator function
from modifying complex2.
Algorithm:
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 inp()function.
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
Conclusion : Hence successfully implemented a program in C++ to perform following
operations on complex numbers Add, Subtract, Multiply, Divide. Use operator overloading
for these operations.