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

STEP 1: Create a New Multifile Project Create a new multifile project, and creat

ID: 652516 • Letter: S

Question

STEP 1: Create a New Multifile Project

Create a new multifile project, and create a class to model complex-number, mathematical operations using overloaded operators and add main() test functions to test the operations.

Create a class for complex numbers that will provide the means to store both the real and the imaginary values of a complex number.

Design and add overloaded operators to perform correct and complex mathematical operations on objects of the complex-number class.

STEP 2: Create and Add the Overloaded Operators

Overload the addition operator (+) to perform complex mathematical addition on two objects of the complex-number class.

Overload the subtraction operator (-) to perform complex mathematical subtraction on two objects of the complex-number class.

Overload the multiplication operator (*) to perform complex mathematical multiplication on two objects of the complex-number class.

Overload the division operator (/) to perform complex mathematical division on two objects of the complex-number class.

Overload the assignment operator (=) to assign all the data values of an object in the complex-number class to another object in the complex-number class.

STEP 3: Test the Overloaded Operators

Create at least three objects of the complex-number class in function main() and use them to perform the following operation tests.

Demonstrate the correct operation of the complex addition overloaded operator.

Demonstrate the correct operation of the complex subtraction overloaded operator.

Demonstrate the correct operation of the complex division overloaded operator.

Demonstrate the correct operation of the complex multiplication overloaded operator.

using c++

Explanation / Answer

complex.h file

#include<iostream>

using namespace std;

class complexnum
{
   private:
   double p, q;

   public:
       complexnum(double a, double b);
       complexnum(double a);
       double realpart();
       double imaginarypart();
       complexnum operator+(complexnum a);
       complexnum operator-(complexnum a);
       friend ostream& operator<<(ostream& s, complexnum r);
       complexnum operator*(complexnum a);
       complexnum operator/(complexnum a);
       complexnum operator=(complexnum a);

};

complex.cpp

#include"complex.h"
// constructor forming the a + bi
   // overloaded function, with two arguments
complexnum::complexnum(double a, double b)
   {
       p = a;
       q = b;
   }
  
complexnum::complexnum(double a)
   {
       p = a;
       q = 0;
   }
   // returns the real part
   double complexnum:: realpart()
   {
       return p;
   }
   // returns the complex part
   double complexnum::imaginarypart()
   {
       return q;
   }

   complexnum complexnum::operator+(complexnum a)
   {
       return complexnum(a.p + p, a.q + q);
   }

   complexnum complexnum::operator-(complexnum a)
   {
       return complexnum(p-a.p,q-a.q);
   }

   ostream& operator<<(ostream& s, complexnum r)
   {
       // if no imaginary part
       if(r.imaginarypart() == 0)
       // return real part only
       return s<<r.realpart();
       // if imaginary part < 0, i.e negative
       else if(r.imaginarypart() < 0 )
       {
           // and if no real part
           if(r.realpart() == 0)
           // return imaginary part only
           return s<<r.imaginarypart()<<"i";
           else
           // return both real and imaginary parts
           return s<<r.realpart()<<r.imaginarypart()<<"i";
       }
       else
       {
       // and if no real part
           if(r.realpart() == 0)
           // return imaginary part only
           return s<<r.imaginarypart()<<"i";
           else
           // return both, real and imaginary parts
           return s<<r.realpart()<<" + "<<r.imaginarypart()<<"i";
       }
   }

   complexnum complexnum::operator*(complexnum a)
   {
       return complexnum(a.p * p, a.q * q);
   }

   complexnum complexnum::operator/(complexnum a)
   {
       return complexnum(p/a.p, q / a.q);
   }

   complexnum complexnum::operator=(complexnum a)
   {
       return complexnum(a.p = p, a.q = q);
   }

main_complex.cpp

#include"complex.h"

int main()
{
   double a,b;

   complexnum r = complexnum(2.4,4.5);

   cout<<" The complex number is r is = "<<r<<endl;

   complexnum t = complexnum(7.2,3.4);
   cout<<" complex number t is = "<<t<<endl;

   // addition of complex number and constant
   complexnum result= r+t;
   cout<<" Then, r + t = "<<result<<endl;

   // subtraction of complex number and constant
   complexnum diff = r-t;
   cout<<" Then, r - t = "<<diff<<endl;

   // multiplication of complex number and constant
   complexnum prod= r*t;
   cout<<" Then, r * t = "<<prod<<endl;

   // division of complex number and constant
   complexnum div= r/t;
   cout<<" Then, r / t = "<<div<<endl;

   // equal to of complex number and constant
   complexnum eq = t;
   cout<<" Then, r = t = "<<eq<<endl;


}