I need to do a header ccomplex.h please help!!!!!! Define the class CComplex.cpp
ID: 3569336 • Letter: I
Question
I need to do a header ccomplex.h please help!!!!!! Define the class CComplex.cpp for working with complex numbers, in such a way that the standard arithmetic and relational operators are overloaded for objects of this class. Additionally, overload the standard input and the standard output operators for this class.
// PROG0502.CPP - Overloading of Operators. Complex Numbers
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////////
class CComplex
{
private:
double real, imag;
public:
CComplex( double r = 0, double i = 0 )
{
real = r;
imag = i;
}
double GetReal() const { return real; }
double GetImag() const { return imag; }
void AssignComplex( double r, double i )
{
real = r;
imag = i;
}
friend CComplex operator+( const CComplex & ,const CComplex &);
CComplex operator-( CComplex x );
};
/*
CComplex CComplex::operator+( CComplex x )
{
return CComplex( real + x.real, imag + x.imag );
}
*/
CComplex operator+( const CComplex &c1, const CComplex &c2 )
{
return CComplex( c1.real + c2.real, c1.imag + c2.imag );
}
CComplex CComplex::operator-( CComplex x )
{
return CComplex( real - x.real, imag - x.imag );
}
/////////////////////////////////////////////////////////////////
// App
void show( const CComplex & );
int main()
{
CComplex a, b, c(1.5, 2), d;
double re, im;
cout << "Complex number - insert re im: ";
cin >> re >> im;
a.AssignComplex( re, im );
b = a;
d = a + b - c;
d = d + CComplex(3, 3);
show( d );
show( a );
system("pause");
return 0;
}
// Visualizar un Complex
void show( const CComplex &c )
{
cout << "(" << c.GetReal() << ", ";
cout << c.GetImag() << ")" << endl;
}
Explanation / Answer
#include #include #include #include #include #include #include const double PI = acos(-1.); const double TOL = 1.e-10; class Complex{ public: double re,im; Complex (){};//default constructor Complex (double,double);//alternative constructor Complex operator + (Complex); Complex operator - (Complex); Complex operator * (Complex); Complex operator / (Complex); unsigned int operator == (Complex); void operator = (Complex); void PrintComplex(); Complex Conjugate(); double MagnitudeSq(); double Magnitude(); Complex ComplexPower(Complex); }; void Complex::operator = (Complex param){ re = param.re; im = param.im; } unsigned int Complex::operator== (Complex param){ if((re == param.re)&&(im == param.im)){return 1;} else{return 0;} } Complex::Complex(double a, double b){ re=a; im=b; } Complex Complex::operator+ (Complex param) { Complex temp; temp.re = re + param.re; temp.im = im + param.im; return (temp); } Complex Complex::operator- (Complex param) { Complex temp; temp.re = re - param.re; temp.im = im - param.im; return (temp); } Complex Complex::operator* (Complex param) { Complex temp; temp.re = re*param.re - im*param.im; temp.im = im*param.re + re*param.im; return (temp); } Complex Complex::operator/ (Complex param) { Complex temp; double mag2 = param.MagnitudeSq(); temp.re = re*param.re + im*param.im; temp.im = im*param.re - re*param.im; temp.re = temp.re/mag2; temp.im = temp.im/mag2; return (temp); } void Complex::PrintComplex(void){ coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.