Answer to Question #92916 in C++ for Mokete

Question #92916
A number of the form a + ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The addition and multiplication of complex numbers are defined by the following rules: (a + ib) + (c + id) = (a + c) + i(b + d ) (a + ib) * (c + id) = (ac - bd) + i(ad + bc) Using the ordered pair notation, these rules are written as: (a, b) + (c, d) = ((a + c), (b + d )) (a, b) * (c, d) = ((ac - bd ), (ad + bc)) C++ has no built-in data type that allows us to manipulate complex numbers. Construct a data type, complex Type, that can be used to process complex numbers. Overload the stream insertion and stream extraction operators for easy input and output. We will also overload the operators + and * to perform addition and multiplication of complex numbers. If x and y are complex numbers, we can evaluate expressions such as x + y and x * y.
1
Expert's answer
2019-08-20T05:28:15-0400
#include <iostream>

struct complex {
   double a;
   double b;

   complex(double _a = 0, double _b = 0)
         : a(_a), b(_b) {}

   const complex operator+(const complex& rhs) {
      return complex(a + rhs.a, b + rhs.b);
   }

   const complex operator*(const complex& rhs) {
      double c = rhs.a;
      double d = rhs.b;
      return complex(a * c - b * d, a * d + b * c);
   }

   friend std::ostream& operator<<(std::ostream& os, const complex& c) {
      return os << "(" << c.a << ", " << c.b << ")";
   }

   friend std::istream& operator>>(std::istream& is, complex& c) {
      return is >> c.a >> c.b;
   }
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
21.09.20, 01:44

Dear stanley, to run the code you need main()

stanley
21.09.20, 00:44

How would you run the above code ? it doe not seem to work on my side ?

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS