Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to add a multiplication operator function in the Complex program that I h

ID: 3554267 • Letter: I

Question

I have to add a multiplication operator function in the Complex program that I have included here. I do not have the slightest idea of how to do this. Any help will be greatly appreciated and and explaination of how you got there would be the icing on the cake. Thanks!

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//declaration section
class Complex
{
private:
  double realPart;       //declare realPart as a double variable
  double imaginaryPart; //declare imaginaryPart as a double variable
public:
  Complex(double real = 0.0, double imag = 0.0) //inline constructor
   {realPart= real; imaginaryPart = imag;}
  void showComplexValues();      //accessor prototype
  void assignNewValues(double real, double imag); //inline mutator
   Complex operator+(const Complex&);     //prototype for the addition operator
   };             //end of class declaration
//implementation section
void Complex::showComplexValues() //ACCESSOR
{
char sign = '+';
if (imaginaryPart<0) sign = '-';
cout<<realPart<<' '<<sign<<' '<<abs(imaginaryPart)<<'i';
}
Complex Complex::operator+(const Complex& complex2)
{
Complex temp;
temp.realPart = realPart+complex2.realPart;
temp.imaginaryPart = imaginaryPart+complex2.imaginaryPart;

return temp;
}

int main()
{
Complex a(2.3, 10.5), b(6.3,19.2), c; //declare three objects
cout<<"Complex number a is ";
a.showComplexValues();
cout<<" Complex number b is ";
b.showComplexValues();

c= a+b; //add two complex numbers

cout<<" The sum of a and b: ";
c.showComplexValues();

return 0;
}

Explanation / Answer

Dear,

Implementation of * operator:

    //overload the operator *
complexType complexType::operator*
                (const complexType& otherComplex) const
{
    complexType temp;

    temp.realPart = (realPart * otherComplex.realPart) -
                 (imaginaryPart * otherComplex.imaginaryPart);
    temp.imaginaryPart = (realPart * otherComplex.imaginaryPart)
                   + (imaginaryPart * otherComplex.realPart);
    return temp;
}

Hope this will help you!!