Question

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

//constructor with default values for real and imaginary parts
   Complex(double a1 =0 , double b1 = 0){
       a = a1;
       b = b1;
   }

//operator * defined for Complex number

   Complex operator *( const Complex &c){

//use the formula to calculate the real and imaginary parts separately
       double re = a * c.a - b *c.b;
       double im = b * c.a + a * c.b;
       return Complex(re, im); //now create the resulting complex number using the real and imag parts
   }

//performs multiplication of this complex number with the parameter passed and updates this number and returns it  

Complex & operator *= (const Complex &c){
       *this = (*this) * c; //use the already defined * operator to perform multiplication and then update this using =
       return *this; //return a reference to this object
   }

   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

Add a comment
Answer #2

answered by: Book Solutions
Add a comment
Know the answer?
Add Answer to:
2. A complex number can be expressed as a + bi where a and b are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A complex number is a number of the form a + bi, where a and b...

    A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (a+bi)+(c+di) = (a+c)+(b+d)i (a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i (a...

  • 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 V-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 + di a + bi - (c +...

  • A complex number is a number in the form of a + bi, where a and...

    A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • A complex number is a number in the form a + bi, where a and b...

    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 + di)...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • Assignment 5 Class For Basic Complex Number Operations 1. A complex number is of the form...

    Assignment 5 Class For Basic Complex Number Operations 1. A complex number is of the form a + ib. Let x = a +ib and y = c +id. The operation of adding two complex numbers is: x + y = (a + c)+i(b+d). The operation of subtracting two complex number is: x-y = (a -c)+i(b-d). The operation of multiplying two complex number is: xxy = (ac - bd) +i(bc + ad). The operation of dividing two complex number is:...

  • C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...

    C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT