OK, here we go.
The code i'm about to post i've sorta put in my own standard. change it back to your own standard (at least then you'll have something to do :D).
I have put a couple of things in there that make life easier, such as the assignment operator (operator=).
i made the Solve and GetCoeff functions static because they have nothing to do with any members in the complex class, they just solve the quadratic and produce 2 complex solutions.
I added the operator+ and operator* stuff aswell. I made it so they return a copy of the result in another complex number so that the original objects dont get changed. at the moment the example just outputs them, if you want to store them the just do something like this:
Complex result = solution1 * solution2;
The rest of the code in there is yours.
Here it is (works fine on my machine):
Code:
#include <iostream.h>
#include <math.h>
class Complex
{
public:
Complex(double real = 0.0, double imaginary = 0.0);
Complex(const Complex& c);
Complex& operator=(const Complex& c);
~Complex(void);
static void Solve(double a, double b, double c, Complex& solution1, Complex& solution2);
static void GetCoeff(double& a, double& b, double& c);
friend ostream& operator<<(ostream& out, Complex& c);
friend Complex operator+(const Complex& c1, const Complex& c2);
friend Complex operator*(const Complex& c1, const Complex& c2);
private: // variables
double _real;
double _imaginary;
};
Complex::Complex(double real, double imaginary)
: _real(real),
_imaginary(imaginary)
{}
Complex::Complex(const Complex& c)
: _real(c._real),
_imaginary(c._imaginary)
{}
Complex::~Complex(void)
{}
Complex& Complex::operator=(const Complex& c)
{
// good habit to get into, make sure they haven't assigned to themselves
// eg. c = c
if(this != &c)
{
_real = c._real;
_imaginary = c._imaginary;
}
return(*this);
}
void Complex::Solve(double a, double b, double c, Complex& solution1, Complex& solution2)
{
double disc = b * b - 4.0 * a * c;
double r1 = 0.0;
double r2 = 0.0;
double i1 = 0.0;
double i2 = 0.0;
if(disc >= 0.0)
{
r1 = (-b + sqrt(disc)) / (2.0 * a);
r2 = (-b - sqrt(disc)) / (2.0 * a);
}
else
{
r1 = r2 = -b / (2.0 * a);
i1 = sqrt(-disc) / (2.0 * a);
i2 = -i1;
}
solution1 = Complex(r1, i1);
solution2 = Complex(r2, i2);
}
void Complex::GetCoeff(double& a, double &b, double &c)
{
cout << "please insert the coefficeints:\n";
cin >> a >> b >> c;
}
ostream& operator<<(ostream& out, Complex& c)
{
out << c._real << " + " << c._imaginary << "i";
return out;
}
Complex operator+(const Complex& c1, const Complex& c2)
{
return(Complex(c1._real + c2._real, c1._imaginary + c2._imaginary));
}
Complex operator*(const Complex& c1, const Complex& c2)
{
// remember, when you times imaginary numbers, they become real!
double real = c1._real * c2._real + c1._imaginary * c2._imaginary;
// the other multiplications are the imaginary part
double imaginary = c1._real * c2._imaginary + c1._imaginary * c2._real;
// return the complex number
return(Complex(real, imaginary));
}
int main(void)
{
Complex solution1, solution2;
double a, b, c;
// first ask for user input
Complex::GetCoeff(a, b, c);
// now solve it, storing the result in 2 separate complex objects
Complex::Solve(a, b, c, solution1, solution2);
// output the result
cout << "The first solution is " << solution1 << " and the second is " << solution2 << endl;
// do some other funky stuff
cout << "(" << solution1 << ") x (" << solution2 << ") = " << (solution1 * solution2) << endl;
cout << "(" << solution1 << ") + (" << solution2 << ") = " << (solution1 + solution2) << endl;
return(1);
}
hope that helps!
U.