> 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 wiki @ in General viewed by 296 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 wiki @


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

Perform the operation and write the result in standard form

Asked by wiki @ in Mathematics viewed by 142 persons

Perform the indicated operation and write the result in standard form: (-3+2i)(-3-7i) A. -5+27i B. 23+15i C. -5+15i D. 23-15i E-5-27I

Perform the following operations. express each answer in scientific notation

Asked by wiki @ in Chemistry viewed by 161 persons

5. Perform the following operations. Express each answer in scientific notation. a. (1.54 x 10 -2 g) + (2.86 x 10-1 g) b. (7.023 x 109 g) -(6.62 x 107g) …

Incident objectives that drive incident operations are established by the:

Asked by wiki @ in Social Studies viewed by 138 persons

Incident objectives that drive incident operations are established by the

In chemistry the mole is used to measure objects by

Asked by wiki @ in Chemistry viewed by 216 persons

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

Only class b and class c networks can be subnetted

Asked by wiki @ in Computers and Technology viewed by 206 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 wiki @ in Business viewed by 287 persons

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

The comparison operators used 1 excel to compute criteria are

Asked by wiki @ in Computers and Technology viewed by 285 persons

Which phrase is a comparison operator for inserting a not equal to argument in an IF, COUNTIF or SUMIF function? &lt;= &lt;&gt; &gt;&lt; &gt;=

Incident objectives that drive incident operations are established by the

Asked by wiki @ in Social Studies viewed by 439 persons

Incident objectives that drive incident operations are established by the: A. Planning Section Chief B. Operations Section Chief C. Incident Commander or Unified Command D. Agency Administrator

Which formula is used to find an object's acceleration

Asked by wiki @ in Physics viewed by 262 persons

Which formula is used to find an object’s acceleration?

Garbage containers used by an operation should be

Asked by wiki @ in Health viewed by 292 persons

1.Garbage Containers used by an operation should be a. odor proof, damage resistant, and light in color. b. leak proof, waterproof, and easy to clean. c. durable, absorbent, and corrosion …

Performance goals are used to

Asked by wiki @ in History viewed by 328 persons

Standards of performance, or "performance goals," are used to A. communicate nonverbally. B. measure productivity. C. determine a worker's attitude. D. measure effectiveness, but not efficiency.

Write how you would use numbers to investigate each object

Asked by wiki @ in Mathematics viewed by 453 persons

Write how you would use numbers to investigate each object

What operation would you use to convert cups to gallons

Asked by wiki @ in Mathematics viewed by 263 persons

What operation would you we to convert cups to gallon explain

Which object is used to focus light in a microscope

Asked by wiki @ in Biology viewed by 269 persons

Which object is used to focus light in a light microscope? objective lens the stage adjustment knobs specimen slides

One software program used to compose a letter would be

Asked by wiki @ in English viewed by 267 persons

This software application can be used to create letters, reports, and brochures. Excel OneNote PowerPoint Word

Most viewed questions in General


Do you need a license to be a private detective

Asked by wiki @ in General viewed by 356 persons


What do you need to do to get your license

Asked by wiki @ in General viewed by 327 persons


Can i be on my period and still get pregnant

Asked by wiki @ in General viewed by 321 persons



How long does it take for hiv to show up

Asked by wiki @ in General viewed by 301 persons


Business email how to make it professional and effective pdf

Asked by wiki @ in General viewed by 298 persons


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

Asked by wiki @ in General viewed by 296 persons



9 tips on how to stop your hands from shaking

Asked by wiki @ in General viewed by 296 persons


Can you program more than one remote for garage door

Asked by wiki @ in General viewed by 295 persons


Best places to see cherry blossoms in tokyo at night

Asked by wiki @ in General viewed by 290 persons



How long does it take to do defensive driving online

Asked by wiki @ in General viewed by 286 persons


Can ringing in the ears be a sign of cancer

Asked by wiki @ in General viewed by 286 persons


How long does it take a dog to digest food

Asked by wiki @ in General viewed by 284 persons



Appreciation mail to team member for successful completion of project

Asked by wiki @ in General viewed by 282 persons


A day in the life of a debt collector summary

Asked by wiki @ in General viewed by 281 persons


How long does it take to get rid of trichomoniasis

Asked by wiki @ in General viewed by 273 persons