c++ - Overloading Multiplication Operator -


i working on assignment c++ class. having overload several operators such +, -, !=, =, etc. well, have of them figured out except multiplication. have tried gives overflow or doesn't compile. not sure need it.

here header file holds overloads.

#ifndef complexnumber_h #define complexnumber_h #include<iostream> using namespace std;  class complexnumber{ public:      double real, imaginary;     complexnumber(){         real = 0;         imaginary = 0;     }     complexnumber(double a, double b){         real = a;         imaginary = b;     }     complexnumber(double a){          real = a;         imaginary = 0;      }      complexnumber & operator= (const complexnumber & rhs){          if(this == &rhs){              return *this;          }         else{              real = rhs.imaginary;             imaginary = rhs.imaginary;         }          return *this;     }      complexnumber & operator+= (const complexnumber &rhs){          real += rhs.real;         imaginary += rhs.imaginary;         return *this;      }      complexnumber & operator-= (const complexnumber &rhs){          real -= rhs.real;         imaginary -= rhs.imaginary;         return *this;      }      const complexnumber operator+ (const complexnumber &rhs){          complexnumber result = *this;         result += rhs;         return result;      }      const complexnumber operator- (const complexnumber &rhs){          complexnumber result = *this;         result -= rhs;         return result;      }      const complexnumber operator* (const complexnumber &rhs){          return *this * rhs;      }      friend ostream & operator<< (ostream &out, const complexnumber &c){          out << "(" << c.real << (c.imaginary<0?" - ":" + ") << abs(c.imaginary) << " i)";         return out;      }      friend istream & operator>> (istream & in, complexnumber &c){          in >> c.real >> c.imaginary;         return in;      }      operator double(){          return real;      }      bool operator== (const complexnumber & rhs) const {          bool result = (this->real == rhs.real) && (this->imaginary == rhs.imaginary);         return result;      }      bool operator!= (const complexnumber &rhs) const{          return !(*this == rhs);      } };  #endif 

i know multiplication operator way off, have @ moment. here on own. ideas appreciated!!

const complexnumber operator* (const complexnumber &rhs){              return *this * rhs;          } 

it gives overflow because of way call it. call, multiplying complex number complex number, , keeps calling same operator without doing anything. try use basic math , derive formula complex number multiplication. specifically, let's have 2 complex numbers z1 , z2. let z1 = + bi, real part, , b imaginary part, , z2 = c + di, c real part, , d imaginary part. have z1 * z2 = (a + bi)(c + di) = ( ac + adi + cbi - bd ). now, separate real , imaginary part, real part here without i, ac - bd, , imaginary part ad + cb. now, use in terms of class members, , this:

const complexnumber operator* (const complexnumber &rhs) {     complexnumber result;     result.real = real * rhs.real - imaginary * rhs.imaginary;     result.imaginary = real * rhs.imaginary + imaginary * rhs.real;     return result; } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -