> C1; 2+4i; cout << "Enter Real and Imaginary part of the Complex Number 2 : \n"; cin >> C2; …">

C++ program to perform arithmetic operations using objects and classes


Asked by admin @ in General viewed by 411 People


How do you write a C++ program to find the arithmetic operations on complex numbers using operator overloading using the C++ language?

Answered by admin @


class Complex
{
double real;
double img;
public:
Complex();
friend istream & operator >> (istream &, Complex &);
friend ostream & operator << (ostream &, const Complex &);
Complex operator + (Complex);
Complex operator * (Complex);
};
Complex::Complex()
{
real = 0;
img = 0;
}
istream & operator >> (istream &, Complex & i)
{
cin >> i.real >> i.img;
return cin;
}
ostream & operator << (ostream &, const Complex & d)
{
cout << d.real << " + " << d.img << "i" << endl;
return cout;
}
Complex Complex::operator + (Complex c1)
{
Complex temp;
temp.real = real + c1.real;
temp.img = img + c1.img;
return temp;
}
Complex Complex::operator * (Complex c2)
{
Complex tmp;
tmp.real = real * c2.real - img * c2.img;
tmp.img = real * c2.img + img * c2.real;
return tmp;
}
int main()
{
Complex C1, C2, C3, C4;
int flag = 1;
char b;
while (flag == 1)
{
cout << "Enter Real and Imaginary part of the Complex Number 1 : \n";
cin >> C1; 2+4i;
cout << "Enter Real and Imaginary part of the Complex Number 2 : \n";
cin >> C2; 3+2i;
int f = 1;
while (f == 1)
{
cout << "Complex Number 1 : " << C1 << endl; 2+4i;
cout << "Complex Number 2 : " << C2 << endl; 3+2i;
cout << "**********MENU**********" << endl;
cout << "1. Addition of Complex Numbers" << endl;
cout << "2. Multiplication of Complex Numbers" << endl;
cout << "3. Exit\n";
int a;
cout << "Enter your choice from above MENU (1 to 3) : ";
cin >> a;
if (a == 1)
{
C3 = C1+C2;
cout << "Addition : " << C3 << endl;
cout << "Do you want to perform another operation (y/n) : \n";
cin >> b;
if (b == 'y' || b == 'Y')
{
f=1;
}
else
{
cout << "Thanks for using this program!!\n";
flag=0;
f=0;
}
}
else if (a == 2)
{
C4 = C1 * C2;
cout << "Multiplication : " << C4 << endl;
cout << "Do you want to perform another operation (y/n) : \n";
cin >> b;
if (b == 'y' || b == 'Y')
{
f=1;
}
else
{
cout << "Thanks for using this program!!\n";
flag=0;
f=0;
}
}
else
{
cout << "Thanks for using this program!!\n";
flag=0;
f=0;
}
}
}
return 0;
}
Arithmetic operations on complex numbers using operator overloading using C++ language.
Program: # include using namespace std; class Complex { double real; double img; public: Complex(); friend istream & operator >> (istream &, Complex &); friend ostream & operator << (ostream &, const Complex &); Complex operator + (Complex); Complex operator * (Complex); }; Complex::Complex() { real = 0; img = 0; } istream & operator >> (istream &, Complex & i) { cin >> i.real >> i.img; return cin; } ostream & operator << (ostream &, const Complex & d) { cout << d.real << " + " << d.img << "i" << endl; return cout; } Complex Complex::operator + (Complex c1) { Complex temp; temp.real = real + c1.real; temp.img = img + c1.img; return temp; } Complex Complex::operator * (Complex c2) { Complex tmp; tmp.real = real * c2.real - img * c2.img; tmp.img = real * c2.img + img * c2.real; return tmp; } int main() { Complex C1, C2, C3, C4; int flag = 1; char b; while (flag == 1) { cout << "Enter Real and Imaginary part of the Complex Number 1 : \n"; cin >> C1; 2+4i; cout << "Enter Real and Imaginary part of the Complex Number 2 : \n"; cin >> C2; 3+2i; int f = 1; while (f == 1) { cout << "Complex Number 1 : " << C1 << endl; 2+4i; cout << "Complex Number 2 : " << C2 << endl; 3+2i; cout << "**********MENU**********" << endl; cout << "1. Addition of Complex Numbers" << endl; cout << "2. Multiplication of Complex Numbers" << endl; cout << "3. Exit\n"; int a; cout << "Enter your choice from above MENU (1 to 3) : "; cin >> a; if (a == 1) { C3 = C1+C2; cout << "Addition : " << C3 << endl; cout << "Do you want to perform another operation (y/n) : \n"; cin >> b; if (b == 'y' || b == 'Y') { f=1; } else { cout << "Thanks for using this program!!\n"; flag=0; f=0; } } else if (a == 2) { C4 = C1 * C2; cout << "Multiplication : " << C4 << endl; cout << "Do you want to perform another operation (y/n) : \n"; cin >> b; if (b == 'y' || b == 'Y') { f=1; } else { cout << "Thanks for using this program!!\n"; flag=0; f=0; } } else { cout << "Thanks for using this program!!\n"; flag=0; f=0; } } } return 0; } Output: Enter Real and Imaginary part of the Complex Number 1 : 4 6 Enter Real and Imaginary part of the Complex Number 2 : 2 3 Complex Number 1 : 4 + 6i Complex Number 2 : 2 + 3i **********MENU********** 1. Addition of Complex Numbers 2. Multiplication of Complex Numbers 3. Exit Enter your choice from above MENU (1 to 3) : 1 Addition : 6 + 9i Do you want to perform another operation (y/n) : y Complex Number 1 : 4 + 6i Complex Number 2 : 2 + 3i **********MENU********** 1. Addition of Complex Numbers 2. Multiplication of Complex Numbers 3. Exit Enter your choice from above MENU (1 to 3) : 2 Multiplication : -10 + 24i Do you want to perform another operation (y/n) : n Thanks for using this program!!
https://programiz2.blogspot.com/2021/06/arithmetic-operations-on-complex.html

Similar Questions

C++ program to add two complex numbers using class

Asked by admin @ in Computer Science viewed by 398 persons

Write a c++ program to perform addition of two complex numbers using classes​

What arithmetic operators cannot be used with strings

Asked by admin @ in Computer Science viewed by 299 persons

which arithmetic operators cannot be used with strings?i) + ii) * iii) - iv) All of the above​

C++ program to calculate area of rectangle using constructor

Asked by admin @ in Computer Science viewed by 343 persons

C++ program to calculate area of traingle circle and rectangle using constructor overloading

C program to print number in words using switch case

Asked by admin @ in Computer Science viewed by 357 persons

C program to display the number in words using switch case where a sample input and output is given

C programing

Asked by pranav94 @ in Engineering viewed by 467 persons

In chemistry the mole is used to measure objects by

Asked by admin @ in Chemistry viewed by 286 persons

Why is it useful to use moles to measure chemical quantities?

Only class b and class c networks can be subnetted

Asked by admin @ in Computers and Technology viewed by 327 persons

Only class b and class c networks can be subnetted true or false

Name the main functional areas of operation used in business

Asked by admin @ in Business viewed by 371 persons

Identify the three major functional areas of an organization’s operations and describe how they interrelate.

In windows explorer ctrl j performs which operation

Asked by admin @ in Computer Science viewed by 294 persons

In window explorer ctrl +j perform which operation

Which is used to perform what if analysis

Asked by admin @ in Computer Science viewed by 306 persons

In Calc which is used to perform what if analysis? * 2 points a. Solver b. Goal seek c. Scenario Manager d. All of above

What is fundamental theorem of arithmetic class 10

Asked by admin @ in Math viewed by 289 persons

State the fundamental theorem of Arithmetic class 10

Which program is used to create a presentation

Asked by admin @ in Computer Science viewed by 328 persons

Which application software is used to create presentation? (a). OpenOffice impress(b). OpenOffice calc(c). OpenOffice writer(d). None of these​

What are variables used for in javascript programs

Asked by admin @ in Computer Science viewed by 331 persons

What are variables used for in javascript programs mcq?

State the fundamental theorem of arithmetic class 10

Asked by admin @ in Math viewed by 288 persons

State the fundamental theorem of Arithmetic class 10

The basic operations performed by a computer are

Asked by admin @ in English viewed by 366 persons

The basic operation performed by a computer are

Most viewed questions in General


A day in the life of a debt collector summary

Asked by admin @ in General viewed by 487 persons


C++ program to perform arithmetic operations using objects and classes

Asked by admin @ in General viewed by 411 persons


Business email how to make it professional and effective pdf

Asked by admin @ in General viewed by 404 persons



Can you have normal thyroid levels and still have hypothyroidism

Asked by admin @ in General viewed by 391 persons


How long does it take a dog to digest food

Asked by admin @ in General viewed by 390 persons


Iran's shah was a big ally of the united states

Asked by admin @ in General viewed by 378 persons



Do you need a license to be a private detective

Asked by admin @ in General viewed by 375 persons


9 tips on how to stop your hands from shaking

Asked by admin @ in General viewed by 374 persons


Appreciation mail to team member for successful completion of project

Asked by admin @ in General viewed by 373 persons



According to the nebular theory what are asteroids and comets

Asked by admin @ in General viewed by 372 persons


Can ringing in the ears be a sign of cancer

Asked by admin @ in General viewed by 367 persons


Does a walk in tub add value to a house

Asked by admin @ in General viewed by 367 persons



Can i be on my period and still get pregnant

Asked by admin @ in General viewed by 358 persons


How long does it take to do defensive driving online

Asked by admin @ in General viewed by 356 persons


How long does it take for hiv to show up

Asked by admin @ in General viewed by 355 persons