Problem with overloading and class construction
This is a program with quadratic equation. The equations are working ok. I can get r1, r2, i1 and i2 to print ok out of the "solve" function, but I cannot get them to print by overloading the << operator. I have difficulty with overloading, and I know I'm doing something wrong, I just don't know what.
Also, I have to declare double r1, r2, i1 and i2 in both the class complex, and the solve function, or it won't work. What am I doing wrong??
Code:
#include<iostream.h>
#include<math.h>
class complex{
double c;
double r1, r2, i1, i2;
public:
void solve(double, double, double);
friend ostream& operator<<(ostream&, complex ) ;
};
ostream& operator<<(ostream& out, complex c )
{
out << "The solutions are: " << c.r1 << " + " << c.i1 << c.r2 << " + " << c.i2 << endl;
return out;
}
void solve(double a, double b, double c)
{
double disc = b*b - 4.0*a*c;
double r1, r2, i1 = 0, i2 = 0;
if( disc >= 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;
}
// cout << "The solutions are:\n";
// cout << r1 << "+" << i1 << "i\n";
// cout << r2 << "+" << i2 << "i\n";
}
void getCoeff(double& a, double &b, double &c)
{
cout << "please insert the coefficeints:\n";
cin >> a>>b>>c;
}
void main()
{
double a, b, c;
getCoeff(a,b,c);
solve(a, b, c);
getCoeff(a,b,c);
solve(a,b,c);//C3, C4);
}