In this blog post, you will learn about the addition, subtraction,multiplication and division of 2 complex numbers using the concept of object as a function arguments in C++.
(Q) Write a program to add, subtract, multiply, and divide 2 complex objects taking objects as a function argument and returning object to the function.
So, the source code of the above question will be as follows-:
using namespace std;
cout<<"\nEnter the real and imaginary part\n";
complexx addcomplexx(complexx c1,complexx c2)
complexx subtractcomplexx(complexx c1,complexx c2)
complexx multiplycomplexx(complexx c1,complexx c2)
complexx dividecomplexx(complexx c1,complexx c2)
cout<<"\nEnter the start complex number\n";
cout<<"\nEnter the end complex number\n";
cout<<"\nthe addition of two complex is\n";
cout<<"\nThe subtraction of two complex is\n";
cout<<"\nThe multiplied value is\n";
cout<<"\nthe divided value is\n";
It all starts with the int main() function.
So, this program looks a bit tedious and long.
Don’t get tensioned. In your exams, you will not be asked to write about all the four things ie addition, subtraction, multiplication and division. Instead in exams you will be asked about one of them.
At the beginning we created the complex objects c1,c2,c3…c10 as:
Then, we took the input as
The same process was followed for the object 2 to take input.
After that, we create a new object c5 where we will store the addition of complex numbers c1 and c2 and store the result in c3.
If we look deep inside the function “addcomplexx” then we can see that the complex numbers are being added using this concept actually.
It is represented below in figure-:
Then, we displayed it using the display function which is not so hard to understand.
After that, we will start to take input from the user. Then invoke a “subtractcomplexx” function as below-:
Then inside the “subtractcomplexx” function, same thing as in add complex will happen.
I will show it to you more clearly by explaining the logic and the representation of that program. It is shown in figure below.
And then, OOF…take a deep breath.
You might be hammering yourself for not getting this well before some time. And now, you may be feeling that it was so easy. If you continue to learn from this website, there will come a day when none programming will be a piece of cake for you. But just learning this much only in programming is not sufficient. This is necessary to get good marks in your exam. But, there is something else that is necessary to make yourself ahead in this field. And we will discuss those things later on.
So, we took a little break from what we were doing. Now, let’s start again for multiplication.
we create an object c7 and invoked it using the same object c7 as c7.mulitplycomplexx(c1,c2).
So, what will go inside the “multiplycomplex” function?
This is what goes inside it-:
Then, let’s look at the division function.
a is a real part of c1, b is an imaginary part of c1. Likewise, c is a real part of c2 and d is an imaginary part of c2.
I hope everything is clear upto now.
Now, solve a homework problem-:
Q) Write a program to create a class LandMeasure that reads Ropani, Ana, Paisa and Dam as data members. Write a function to pass 2 objects of type LandMeasure and return their sum.
Solve this question and post in the comment section. If you don’t know how to solve this, I will create a blog post related to the solution of this question.