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

C++ Create the implementation portion of the complex class described in the comp

ID: 3770990 • Letter: C

Question

C++ Create the implementation portion of the complex class described in the complex definition file "complex.h"

#ifndef COMPLEX_H

#define COMPLEX_H

class complex {

public:

complex();

complex(double a);

complex(double a,double b);

complex(int a,int b);

void print() const;

void set(double a=0,double b=0);

void set(int a=0,int b=0);

double getReal() const;

double getImag() const;

void get(double&,double&) const;

complex operator+ (double&);

complex operator+ (complex&);

complex operator+= (complex&);

complex operator+= (int&);

complex operator++ (int);

complex operator++ ();

complex operator- (double&);

complex operator- (complex&);

complex operator-= (complex&);

complex operator-= (double&);

complex operator-- (int);

complex operator-- ();

complex operator* (complex&);

complex operator* (double&);

complex operator*= (complex&);

complex operator*= (double&);

complex operator/ (complex&);

complex operator/= (complex&);

complex operator/= (double);

complex operator/ (double);

void operator= (const complex&);

bool operator== (complex&);

bool operator!=(complex &c);

friend std::istream &operator>> (std::istream &in, complex& c);

friend std::ostream &operator<<(std::ostream &out, complex c);

complex conj() const;

double norm() const;

double modulus() const;

private:

double real;

double imag;

};

#endif // COMPLEX_H

Explanation / Answer

#include "complex.h"
#include<math.h>
int main()
{
   return 0;  
}

complex::complex()
{
   real=0;
   imag=0;
}
complex::complex(double a)
{
   real=a;
}
complex::complex(double a,double b)
{
   real=a;
   imag=b;
}
complex::complex(int a,int b)
{
   real=a;
   imag=b;
}
void complex::print() const
{
   cout<<real<<"+i "<<imag<<endl;
}
void complex::set(double a,double b)
{
   real=a;
   imag=b;
}
void complex::set(int a,int b)
{
   real=a;
   imag=b;
}
double complex::getReal() const
{
   return real;
}
double complex::getImag() const
{
   return imag;
}
std::ostream& operator<< (std::ostream& out, complex const& value) {
    return out << value.getReal() << " " << value.getImag();
}

std::istream& operator>> (std::istream& in, complex& c) {
    double h, j;
    if (in >> h >> j) {
        c.set(h, j);
    }
    return in;
}

void complex::get(double& a,double& b) const
{
   a=getReal();  
   b=getImag();
}
complex complex::operator+ (double&a)
{
   return complex(real+a,imag);
}
complex complex::operator+ (complex&c)
{
   return complex(real+c.real,imag+c.imag);
}

complex complex::operator+= (complex&c)
{
   return complex(real+=c.real,imag+=c.imag);
}

complex complex::operator+= (int&i)
{
   return complex(real+=i,imag+=i);
}

complex complex::operator++ (int i)
{
   return complex(real++,imag++);
}

complex complex::operator++ ()
{
   return complex(++real,++imag);
}
complex complex::operator- (double&i)
{
   return complex(real-i,imag);
}
complex complex::operator- (complex&c)
{
   return complex(real-c.real,imag-c.imag);
}
complex complex::operator-= (complex&c)
{
   return complex(real-=c.real,imag-=c.imag);
}
complex complex::operator-= (double&d)
{
   return complex(real-d,imag-d);
}
complex complex::operator-- (int i)
{
   return complex(real--,imag--);
}
complex complex::operator-- ()
{
   return complex(--real,--imag);
}

complex complex::operator* (complex&c)
{
   return(complex(   (real*c.real-imag*c.imag), (real*c.imag+imag*c.real) ));
}
complex complex::operator* (double&d)
{
   return(complex(real*d, imag*d));
}
complex complex::operator*= (complex&c)
{
   return(complex(   (real*c.real-imag*c.imag), (real*c.imag+imag*c.real) ));
}
complex complex::operator*= (double&d)
{
   return(complex(real*d, imag*d));
}
complex complex::operator/ (complex&c)
{
   return(complex( ( (real*c.real+imag*c.imag) / (c.real*c.real + c.imag*c.imag) ), (    (real*c.imag-imag*c.real) / (c.real*c.real + c.imag*c.imag) )));
}
complex complex::operator/= (complex&c)
{
   return(complex( ( (real*c.real+imag*c.imag) / (c.real*c.real + c.imag*c.imag) ), (    (real*c.imag-imag*c.real) / (c.real*c.real + c.imag*c.imag) )));
}
complex complex::operator/= (double d)
{
   return(complex(real/=d, imag/=d));
}
complex complex::operator/ (double d)
{
       return(complex(real/d, imag/d));
}
void complex::operator= (const complex&c)
{
   real=c.real;
   imag=c.imag;
}
bool complex::operator== (complex&c)
{
   if(c.real==real && c.imag==imag)
   {
       return true;
   }
   else
   {
       return false;
   }
}
bool complex::operator!=(complex &c)
{
   if(c.real!=real || c.imag!=imag )
   {
       return true;
   }
   else
   {
       return false;
   }
}
complex complex::conj() const
{
   return complex(real,(-1*imag));
}
double complex::norm() const
{
   return ((real*real)+(imag*imag));
}
double complex::modulus() const
{
   return (sqrt((real*real)+(imag*imag)));
  
}

// Check Complete Solution, If you have any doubt then you can ask, But ask with some examples.