Create the implementation portion of the complex class described in the complex
ID: 3771075 • Letter: C
Question
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
Answer:
/*IMPLEMENTATION OF COMPLEX_H*/
/*constructor*/
complex::complex()
{
real=0;
imag=0;
}
complex::complex(double a)
{
real=a;
imag=a;
}
complex::complex(double a,double b)
{
real=a;
imag=b;
}
complex::complex(int a,int b)
{
real=a;
imag=b;
}
/*print complex nmber*/
void complex::print() const
{
cout<<getReal()<<"+j"<<getImag()<<endl;
}
/*return real part of complex number */
double complex::getReal() const
{
return real;
}
/*return imaginary part of complex number */
double complex::getImag() const
{
return imag;
}
void complex::get(double& a,double& b) const
{
a=real;
b=imag;
}
/* overloading operator +*/
complex complex:: operator+ (double& a)
{
return complex(a+real, a+imag);
}
complex complex::operator+ ( complex& c2)
{
return complex(real+c2.real+imag+c2.imag);
}
//overloading +=
complex complex:: operator+= ( complex& c2)
{
real=real+c2.real;
imag=imag+c2.imag;
return *this;
}
complex complex:: operator+= (int& a)
{
real=real+a;
imag=imag+a;
return *this;
}
//overloading ++
complex complex::operator++ (int a)
{
real=real+a;
imag=imag+a;
return *this;
}
complex complex::operator++ ()
{
real=real+1;
imag=imag+1;
return *this;
}
//overloading operator -
complex complex::operator- (double& a)
{
return complex(real-a,imag-a);
}
complex complex::operator- (complex& c2)
{
return complex(real-c2.real,imag-c2.imag);
}
//overloading -=
complex complex::operator-= (complex& c2)
{
real=real-c2.real;
imag=imag-c2.imag;
return *this;
}
complex complex:: operator-= (double& a)
{
real=real-a;
imag=imag-a;
return *this;
}
//overloading --
complex complex::operator-- (int a)
{
real=real-a;
imag=imag-a;
return *this;
}
complex complex::operator-- ()
{
real=real-1;
imag=imag-1;
return *this;
}
//overloading *
complex complex ::operator* (complex& c2)
{
return complex(real*c2.real-imag*c2.imag,real*c2.imag+imag*c2.real);
}
complex complex :: operator* (double& a)
{
return complex(real*a,imag*a);
}
//overloading *=
complex complex :: operator*= (complex& c2)
{
real=real*c2.real-imag*c2.imag;
imag=real*c2.imag+imag*c2.real;
return *this;
}
complex complex:: operator*= (double& a)
{
real=real*a;
imag=imag*a;
return *this;
}
//overloading /
complex complex ::operator/ (complex& c2)
{
return (*this)*c2.conj()/c2.norm();
}
//overloading /=
complex complex::operator/= (complex& c2)
{
complex c3=(*this)*(c2.conj())/c2.norm();
real=c3.real;
imag=c3.imag;
return *this;
}
complex complex:: operator/= (double a)
{
real=real/a;
imag=imag/a;
return *this;
}
complex complex:: operator/ (double a)
{
return complex(real/a,imag/a);
}
//overloading assignment opertor
void complex::operator= (const complex& c2)
{
real=c2.real;
imag=c2.imag;
}
//overloading equality operator
bool complex:: operator== (complex& c2)
{
if(real==c2.real&&imag==c2.imag)
return true;
return false;
}
bool complex:: operator!= (complex& c)
{
if(real!=c.real&&imag!=c.imag)
return true;
return false;
}
istream &operator>> (std::istream &in, complex& c)
{
in>>c.real>>c.imag;
return in;
}
ostream &operator<<(std::ostream &out, complex c)
{
out<<"Complex Number is"<<endl;
out<<c.real<<"+j"<<c.imag;
return out;
}
//return the conjugate
complex complex:: conj() const
{
return complex(real,-imag);
}
//return norm of complex number
double complex::norm() const
{
return (real*real+imag*imag);
}
double complex :: modulus() const
{
return sqrt(real*real+imag*imag);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.