I have to design, implement, and test a complex number class that represents the real and imaginary parts as double-precision values(data type double) and provides at least the following operatios:
1. Constructors for explicit as well as default initialization. The default initial value should be (0,0 0,0).
2. Arithmetic operations that add, subtract, multiple, and divide two complex numbers. These should be implemented as value-returning functions, each returning a class object.
3. A complex absolute value operation.
4. Two observer operations, “RealPart and ImagPart”, That return the real and imaginary parts of a complex number.
I am a newbee with c++, and don't know if I'm coding it in the right direction. CAN ANYONE HELP: HERE IS MY CODE:
//MAIN.CPP//
#include
#include"Complex.h"
using namespace std;
int main()
{
//Instantiate 4 Complex objects using parameter constructor//
Complex a(2,3);
Complex b(-8,10);
Complex result;
// Checks to see if values are present//
cout
using namespace std;
class Complex
{
public:
//constructors//
Complex(); //default constructor
Complex(double a, double b);
Complex(double value);
Complex(const Complex& a);
//Get and set Methods//
double getValue() const;
double setValue(double value);
~Complex();
//Math Methods//
//Add objects of type complex and return the result//
Complex add(const Complex& a);
//Subtract objects of type complex and return the result//
Complex subtract(const Complex& a);
//Multiply with objects of type complex and return the result//
Complex multiply(const Complex& a);
//Division with objects of type complex and return the result//
Complex divide(const Complex& a);
private:
double myReal;
double myIm;
};
#endif
//Complex.cpp//
#include"Complex.h"
//Default Constructor//
Complex::Complex(double a ,double b)
{
myReal=a;
myIm=b;
}
//Constructors with parameter//
Complex::Complex(double value) //Conversion Constructor//
{
myReal=value;
myIm=value;
}
//Copy Constructor//
Complex::Complex(const Complex& a)
{
myReal=a.getValue();
myIm=a.getValue();
}
//Destructor//
Complex::~Complex()
{
}
//GetValue//
double Complex::getValue() const
{
return myReal;
return myIm;
}
//SetValue//
double Complex::setValue(double value)
{
myReal=value;
myIm=value;
}
//Operations//
Complex Complex::add(const Complex& a)
{
Complex result;
result.setValue(myReal+a.getValue());
return result;
}