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

Do you know the better answer?

Similar Questions

Explain why french control of the louisiana territory worried jefferson

Asked by wiki @ in History viewed by 175 persons

Explain why french control of the louisiana territory worried jefferson

What do the angles of a trapezoid add up to

Asked by wiki @ in Mathematics viewed by 147 persons

The sum of the angle measures of any trapezoid is greater than the sum of the angle measures of any parallelogram is it true or false and explain how

What was the main effect of the contract with america

Asked by wiki @ in History viewed by 201 persons

What was the main effect of the Contract With America? A. The Constitution was amended. B. Democrats lost the White House. C. The Senate impeached Bill Clinton. D. Republicans took …

Describe the part played by germany in increasing european militarism.

Asked by wiki @ in History viewed by 58 persons

Describe the part played by germany in increasing european militarism.

In most chemical reactions the amount of product obtained is

Asked by wiki @ in Chemistry viewed by 158 persons

In most chemical reactions the amount of product obtained is

Article one of the constitution describes the powers of the

Asked by wiki @ in Social Studies viewed by 181 persons

Article One of the Constitution describes the powers of the executive branch, such as enforcing laws judicial branch, such as interpreting laws legislative branch, such as declaring war state governments, …

The risk of carbon monoxide poisoning can be reduced by

Asked by wiki @ in Advanced Placement (AP) viewed by 609 persons

The risk of carbon monoxide poisoning can be reduced by a. never leaving a car running in a garage b. never backing a vehicle into a garage c. turning on …

What is the term for repetitive cyclical patterns of behavior

Asked by wiki @ in Biology viewed by 96 persons

One of the most important ways behavior becomes integrated is through the development of various body __________, which are repetitive, cyclical patterns of behavior.

There are usually _______ collisions in a motor vehicle crash

Asked by wiki @ in Physics viewed by 149 persons

There are usually _________ collisions in a motor vehicle crash.

Which group creates regulations in mixed market economies

Asked by wiki @ in Social Studies viewed by 245 persons

Which group creates regulations in mixed-market economies?

What is the role of an electron in an atom

Asked by wiki @ in Chemistry viewed by 100 persons

What is the role of electrons within an atom? A. Electrons are in the nucleus and have a negative charge B. Electrons are in the nucleus and have a positive …

Why is the endocrine system slower than the nervous system

Asked by wiki @ in Biology viewed by 179 persons

Choose the accurate statements about how communication by the nervous and endocrine systems differs. Check all that apply. Check All That Apply The nervous system releases neurotransmitters at synapses at …

In another evening at the club the gifts of jewelry

Asked by wiki @ in English viewed by 63 persons

1. In "Another Evening at the Club," the gifts of jewelry from Abbound Bey most likely represent? A. The depth of his affection for and loyalty toward Samia. B. The …

Current address from date

Asked by wiki @ in Physics viewed by 55 persons

What does current address from date mean?

The unit used to measure the energy value of foods

Asked by wiki @ in Health viewed by 197 persons

The energy value of food is is measured in units of heat called ___ ? A. Metabolism B. Complex carbohydrates C.Calories D. Basal metabolic rate

Word equation for cellular respiration

Asked by wiki @ in Biology viewed by 169 persons

What is the equation for cellular respiration in word and chemical symbols

When doing a blank for the purposes of career planning

Asked by wiki @ in Health viewed by 112 persons

When doing a ________ for the purposes of career planning, you should take your skills and interests into account?

Why is china's economy considered more mixed than north korea's

Asked by wiki @ in History viewed by 122 persons

What is the main difference between the economies of China and North Korea? A. North Korea is not centrally planned. B. North Korea is privatizing businesses. C. China is privatizing …

Calculate the income needed based on the following budgeted expenses

Asked by wiki @ in Mathematics viewed by 205 persons

Calculate the income needed based on the following budgeted expenses. month _______ budgeted amount rent $400 utilities $150 food $175 entertainment $125 cell phone $75 car expenses - gas, insurance …

How many yards of dirt will a dump truck hold

Asked by wiki @ in Mathematics viewed by 173 persons

A dump truck driver must haul 112 cubic yards of dirt to a construction site. The maximum load for the dump truck is 14 cubic yards. Which inequality represents the …

The space between the objective lens and the microscope stage

Asked by wiki @ in Biology viewed by 154 persons

The coarse and fine focus knobs adjust the distance between a. the objective and ocular lenses. b. the ocular lenses. c. the ocular lenses and your eyes. d. the stage …

What is the theme of blues ain t no mockingbird

Asked by wiki @ in English viewed by 79 persons

What does the narrator of “Blues Ain’t No Mockin Bird” mean when she says that Granny teaches steady with no letup? A Granny never stops talking B Granny does not …

What was one provision of the homestead act of 1862

Asked by wiki @ in History viewed by 218 persons

What was one provision of the Homestead Act of 1862? It gave participants as much land as they could farm. It gave an extra eighty acres to anyone who could …

How do you find the surface area of a sphere

Asked by wiki @ in Mathematics viewed by 239 persons

3. The formula to find the surface area of a sphere is to take the radius of the sphere and square it and multiply that amount by pi and then …

How much does hair grow in a year on average

Asked by wiki @ in Biology viewed by 151 persons

How much does hair grow on average in one month

Which of the following is an example of corporate crime

Asked by wiki @ in Social Studies viewed by 107 persons

Which of the following is an example of corporate crime? Embezzlement Larceny Assault Burglary

How does a product market differ from a factor market

Asked by wiki @ in Business viewed by 158 persons

What is the difference between Product Markets &amp; Factor Markets?

The mujahideen were fighters in afghanistan who

Asked by wiki @ in History viewed by 92 persons

The mujahideen were fighters in Afghanistan who? A. Rebelled against the communist . B. Marginalized the Muslim population. C. Closed all of the mosques in the nation. D. Requested support …

Squaw valley ski area in california claims that its lifts

Asked by wiki @ in Physics viewed by 133 persons

Squaw Valley ski area in California claims that its lifts can move 49800 people per hour. If the average lift carries people about 190 m (vertically) higher, estimate the maximum …

Write the equation of a line passing through two points

Asked by wiki @ in Mathematics viewed by 231 persons

Writing Linear Equations Instruction Active Writing an Equation Given Two Points on the Line Write the equation of the line that passes through the points (7,-4) and (-1,3), first in …

Most viewed questions in General


Do you need a license to be a private detective

Asked by wiki @ in General viewed by 269 persons


Can i be on my period and still get pregnant

Asked by wiki @ in General viewed by 245 persons


What do you need to do to get your license

Asked by wiki @ in General viewed by 242 persons



Business email how to make it professional and effective pdf

Asked by wiki @ in General viewed by 227 persons


How long does it take for hiv to show up

Asked by wiki @ in General viewed by 223 persons


Best places to see cherry blossoms in tokyo at night

Asked by wiki @ in General viewed by 220 persons



9 tips on how to stop your hands from shaking

Asked by wiki @ in General viewed by 219 persons


Can you program more than one remote for garage door

Asked by wiki @ in General viewed by 217 persons


Can ringing in the ears be a sign of cancer

Asked by wiki @ in General viewed by 216 persons



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

Asked by wiki @ in General viewed by 214 persons


A day in the life of a debt collector summary

Asked by wiki @ in General viewed by 211 persons


How long does it take a dog to digest food

Asked by wiki @ in General viewed by 206 persons



How long does it take to do defensive driving online

Asked by wiki @ in General viewed by 206 persons


How long does it take to get rid of trichomoniasis

Asked by wiki @ in General viewed by 198 persons


A unique number that identifies a vehicle is called a

Asked by wiki @ in General viewed by 193 persons