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-:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include<iostream> using namespace std; class complexx { float real,imag; public: void input() { cout<<"\nEnter the real and imaginary part\n"; cin>>real>>imag; } void display() { cout<<real<<","<<imag<<"i"; } complexx addcomplexx(complexx c1,complexx c2) { complexx temp; temp.real=c1.real+c2.real; temp.imag=c2.imag-c1.imag; return temp; } complexx subtractcomplexx(complexx c1,complexx c2) { complexx temp; temp.real=c1.real-c2.real; temp.imag=c2.imag-c1.imag; return temp; } complexx multiplycomplexx(complexx c1,complexx c2) { complexx temp; temp.real=(c1.real*c2.real); temp.imag=(-1)*(c1.imag*c2.real+c2.imag*c1.real); return temp; } complexx dividecomplexx(complexx c1,complexx c2) { complexx temp; temp.real=(c1.real*c2.real+c1.imag*c2.imag)/(c1.real*c1.real+c2.imag*c2.imag); temp.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); } }; int main() { complexx c1,c2,c3,c4,c5,c6,c7,c8,c9,c10; cout<<"\nEnter the start complex number\n"; c1.input(); cout<<"\nEnter the end complex number\n"; c2.input(); c5=c3.addcomplexx(c1,c2); cout<<"\nthe addition of two complex is\n"; c5.display(); c6=c4.subtractcomplexx(c1,c2); cout<<"\nThe subtraction of two complex is\n"; c6.display(); c8=c7.multiplycomplexx(c1,c2); cout<<"\nThe multiplied value is\n"; c8.display(); c10=c9.dividecomplexx(c1,c2); cout<<"\nthe divided value is\n"; c10.display(); return 0; } |

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-:**

(a+ib)*(c+id)=(ac-bd)+i(bc+ad)

## =(c1.real*c2.real-c1.imag*c2.imag)+i*(c1.imag*c2.real+c1.real*c2.imag).

**Then, let’s look at the division function.**

Here

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.

### Did you read the previous articles?

http://www.scitechtop.com/destructors-in-c-program-with-explanation/

http://www.scitechtop.com/write-a-c-program-showing-the-uses-of-constructorsdestructors-with-theory/

http://www.scitechtop.com/dynamic-memory-allocation-in-c-program-with-explanation/

http://www.scitechtop.com/classes-and-objects-in-c-theory-with-programs/

## Read similar articles-:

#10-Write a program to show the concept of “Returning Objects by Function”

#9-Write a program to show the concept of object as a function argument in C++ with explanation