#9-Write a program to add, subtract, multiply, and divide 2 complex objects taking objects as a function argument and returning object to the function.

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

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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:

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

Then, we took the input as

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

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.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

 

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/

http://www.scitechtop.com/pass-by-reference-and-return-by-reference-in-c-differences-with-program-source-code/

Read similar articles-:

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

#11-Friend function:Characteristics,Member function of one class as friend of another class,Friend Function as a bridge between 2 classes AND FRIEND CLASS ALSO!

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top