Oop 1
Oop 1
for j in range(0,columns_second):
Object oriented programming lab 2023
a.append(int(input()))
Group A
Assignment no 01 (group A)
Title Arithmatic operations on complex numbers
using operator overloading
Assignment No: 1
Title: Arithmetic operations on complex numbers using operator overloading.
Problem: Implement a class Complex which represents the Complex
Number data type.
Operator Overloading
Complex b(2.1,3); //notice the construction taking 2 parameters for the real
and imaginary part
Arithmetic Operators
Arithmetic Operators are used to do basic arithmetic operations like
addition, subtraction, multiplication, division, and modulus.
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
With C++ feature to overload operators, we can design classes able to perform
operations using standard operators. Here is a list of all the operators that can be
overloaded:
Over loadable operators
+ - * / = <> += -= *= /= <<>>
<<= >>= == != <= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= []
Syntax:
return_typeclass_name :: operator op(arg_list)
//function body
where,
Return type is the value returned by the specified operation
op is the operator to be overload.
op is proceeding by the keyword operator.
operator op is the function name
Process of the overloading has 3 steps:
1. Create a class that define a data types that is used in the overloading
operation
2. Declare the operator function operator op()
e.g.
Facilities:
Linux Operating Systems, G++
Algorithm:
Step 5: Define the overloaded functions such as +, -,/,* and the display
function
For Addition:
(a+bi) + (x + yi) =
((a+x)+(b+y)i) For
Multiplication:
(a+bi) * (x + yi) = (((a*x)-(b*y)) + ((a*y) + (x*b))i)
Step 6: Create objects for complex class in main() function
Step 7:Create a menu for addition, multiplication of complex numbersand display
the result
Step 8: Depending upon the choice from the user the arithmetic operators will
invoke the overloaded operator automatically and returns the result
Step 9: Display the result using display function.
Input:
Complex numbers with real and imaginary values for two complex numbers.
Example :
Complex No 1: Real Part: 5
Imaginary part : 4
Complex No 2: Real part:5
Imaginary part : 4
Output:
Conclusion:
Hence, we have studied concept of operator overloading.
Questions:
1. What is operator overloading?
2. What are the rules for overloading the operators?
3. State clearly which operators are overloaded and which operator are not
overloaded?
4. State the need for overloading the operators.
5. Explain how the operators are overloaded using the friend function.
6. What is the difference between “overloading” and “overriding”?
7. What is operator function? Describe the syntax?
8. When is Friend function compulsory? Give an example?