Question

In: Computer Science

2. A complex number can be expressed as a + bi where a and b are...

2. A complex number can be expressed as a + bi where a and b are real numbers and i is the imaginary unit. The multiplication of two complex numbers is defined as follows:

(a+bi)(c+di) = (ac-bd) + (bc+ad)i

Define a class which represents a complex number. The only member functions you have to define and implement are those which overload the * and *= symbols.

In C++ please, thank you

Solutions

Expert Solution

Given below is the code for question. I have written a main() to show that the implementation is correct
Please do rate the answer if it helped. thank you.


#include <iostream>
using namespace std;
class Complex{
   private:
   double a, b;
   public:
   Complex(double a1 =0 , double b1 = 0){
       a = a1;
       b = b1;
   }

   Complex operator *( const Complex &c){
       double re = a * c.a - b *c.b;
       double im = b * c.a + a * c.b;
       return Complex(re, im);
   }

   Complex & operator *= (const Complex &c){
       *this = (*this) * c;
       return *this;
   }

   void print(){
       cout << a << " + " << b << " i" << endl;
   }
};

int main(){
   Complex c1(1, 2), c2(3, 4);
   Complex c3 = c1 * c2;

   cout << "c1 is ";
   c1.print();

   cout << "c2 is ";
   c2.print();

   cout << "c3 is ";
   c3.print();

   cout << "now c3 *= c2" << endl;

   c3 *= c2;

   cout << "c2 is ";
   c2.print();

   cout << "c3 is ";
   c3.print();
}

output
-----

c1 is 1 + 2 i
c2 is 3 + 4 i
c3 is -5 + 10 i
now c3 *= c2
c2 is 3 + 4 i
c3 is -55 + 10 i


Related Solutions

In java: A complex number is a number in the form a + bi, where a...
In java: A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c...
JAVA Programming A complex number is a number in the form a + bi, where a...
JAVA Programming A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c...
In Matlab: Any complex number z=a+bi can be given by its polar coordinates r and θ,...
In Matlab: Any complex number z=a+bi can be given by its polar coordinates r and θ, where r=|z|=sqrt(a^2+b^2) is the magnitude and θ= arctan(ba) is the angle. Write a function that will return both the magnitude r and the angle θ of a given complex numberz=a+bi. You should not use the built-in functions abs and angle. You may use the built-in functions real and imag.
complex number
Find the algebraic form of the following complex number \( (1+i\sqrt{3}) ^{2000} \)
complex number
write polar, carnation,argument, angele , ,and rectangular form of thi Complex number (1+3i)(3+4i)(-5+3i)
Complex number
What are the values of all the cube roots (Z = -4^3 - 4i)?
ASAP (Math: The Complex class) A complex number is a number in the form a +...
ASAP (Math: The Complex class) A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a +...
Complex Numbers: What's the difference between Log(z), log(z) and ln(z)? where z is a complex number,...
Complex Numbers: What's the difference between Log(z), log(z) and ln(z)? where z is a complex number, z = x + iy
Describe how a mutation in a gene switch can change when/where a protein is expressed. Using...
Describe how a mutation in a gene switch can change when/where a protein is expressed. Using an example, describe how that mutation could affect an organism’s fitness.
What is Vitamin B complex? Briefly describe the Vitamin B complex.
What is Vitamin B complex? Briefly describe the Vitamin B complex. 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT