Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-12-2002
LouB
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); }```
• 07-12-2002
Uraldor

1) Made both the solve and getCoeff functions a part of the complex class.
2) used the r1/2 and i1/2 members instead of local variables
3) use a complex object to do the processing.

The operator overloading code that was already in place was nearly perfect, i just changed it a little (made it a reference to a complex, not a copy!).

here is the code:

Code:

```#include<iostream.h> #include<math.h> class complex{         double c;         double r1, r2, i1, i2;         public:         void solve(double a, double b, double c);     void getCoeff(double& a, double& b, double& c);         friend ostream& operator<<(ostream& out, complex& c) ; }; ostream& operator<<(ostream& out, complex& c) {         out << "The solutions are:\n\t" << c.r1 << " + " << c.i1 << "i\n\t" << c.r2 << " + " << c.i2 << "i\n";         return out; } void complex::solve(double a, double b, double c) {         double disc = b*b - 4.0*a*c;         r1 = r2 = i1 = 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;         } }         void complex::getCoeff(double& a, double &b, double &c) {         cout << "please insert the coefficeints:\n";         cin >> a>>b>>c; } int main(void) {     complex com;         double a, b, c;         com.getCoeff(a,b,c);         com.solve(a, b, c);     cout << com;         com.getCoeff(a,b,c);         com.solve(a,b,c);     cout << com;     return(1); }```
hope this helps!

U.
• 07-12-2002
LouB
Awesome!!! Thanks Uraldor. I understand better now, I appreciate it!!!
• 07-12-2002
Uraldor
hehe.. excellent. I'm glad i could help!

Good luck!!
U.
• 07-12-2002
LouB
Ok, see if you can answer this.

Question (may sound stupid!) : are the solutions saved in complex objects??
Also, I added complex constuctors, but not sure what purpose they serve, and if they serve the purpose they are supposed to the way I have them set up.

Here's updated code:
Code:

``` #include<iostream.h> #include<math.h> class complex{         double c;         double r1, r2, i1, i2;         public:         complex();         complex(double);         complex(double, double);         void solve(double a, double b, double c);             void getCoeff(double& a, double& b, double& c);         friend ostream& operator<<(ostream& out, complex& c) ; //        friend complex operator+(complex, complex);//still working on this }; complex::complex() {         r1 = r2 = i1 = i2 = 0; } complex::complex(double) {         r1 = r2 = 2;         i1 = i2 = 0; } complex::complex(double, double) {         r1 = r2 = 2.3;         i1 = i2 = 4.1; } ostream& operator<<(ostream& out, complex& c) {         out << "The solutions are:\n\t" << c.r1 << " + " << c.i1 << "i\n\t" << c.r2 << " + " << c.i2 << "i\n";         return out; } /* //still working on this, need to show the summation of //solutions of each equation. IE r1 + r1 complex operator+(complex& s1, complex& s2) {         complex s;         s.c = s1.solve+s2.solve; }*/ void complex::solve(double a, double b, double c) {         double disc = b*b - 4.0*a*c;         r1 = r2 = i1 = 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;         } }         void complex::getCoeff(double& a, double &b, double &c) {         cout << "please insert the coefficeints:\n";         cin >> a>>b>>c; } int main(void) {     complex com;         double a, b, c;         com.getCoeff(a,b,c);         com.solve(a, b, c);     cout << com;         com.getCoeff(a,b,c);         com.solve(a,b,c);     cout << com;     return(1); }```
• 07-12-2002
LouB
This is getting tricky, but I think I'm figuring it out...this is what i need.

complex objects C1, C2, C3, C4.
C1 needs to store r1 and i1 of first equation.
C2 needs to store r2 and i2 of first equation.
C3 needs to store r1 and i2 of second equation.
C4 needs to store r2 and i2 of second equation.

Then, I need output:
C1, C2, C3, C4

Any ideas?? I'm going bugeyed trying to figure this out.

I guess the big hurdle, is how do I save the data into C1, C2, C3 & C4.
• 07-12-2002
Uraldor
be with ya in a sec :p
• 07-12-2002
LouB
thanks...this is driving me nutty!!
• 07-12-2002
Uraldor
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.
• 07-12-2002
LouB
Thanks, it definately points me in the right direction. Your version though adds the first part of complex number for solution one, and first part of complex number for solution two, of one equation.

What I need to do is have two equations - ie. two sets of a,b,c. Then add the first part of complex number for solution one of first equation, and first part of complex number for solution one of second equation. Argh, does that make sense??

IE.
user inputs a,b,c
two solutions displayed as complex numbers, ie -1+oi, -4+oi

user inputs a,b,c
two solutions displayed ie. -2+oi, -5+oi

then, it should add -1 from first equation with -2 from second equation. It should also multiply these two numbers.
Get it??

I think I'll be up to the wee hours working on this one!! This is a hw assignment, but it is overdue and at this point I won't receive credit. I just want to learn how to do it for my own purpose now. So no, I'm not asking you to do my hw for me (in case you were wondering).

Thanks so much for your help!!! I really appreciate you taking the time.
• 07-12-2002
Uraldor
Yeah i had guessed that it was homework. /me aint stupid ;)

Look, everything you need is there. If you want access to the individual parts of the number, then add access functions:

Code:

```double Complex::GetReal(void) {     return(_real); } double Complex::GetImaginary(void) {     return(_imaginary); } // then all you need to do after getting the two solutions is this: double result1 = solution1.GetReal() + solution2.GetReal(); double result2 = solution1.GetReal() * solution2.GetReal();```

good luck!
U.
• 07-12-2002
Uraldor
BTW, in case you (or anyone else) is wondering why i'm helping.... it's because you've actually had a go at it yourself.

There are so many people that come on here and say "help me do this..." without actually having a go at it themselves first.

Those people who have tried to do it and are having trouble are the ones that i'll dedicate more time to, coz they are trying! And you'll find alot of other people on the board think the same.

If you had just asked the question without doing it yourself first, then i wouldn't have bothered ;)

cheers
U.
• 07-12-2002
golfinguy4
Quote:

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));
}

Shouldn't the line:
double real = c1._real * c2._real + c1._imaginary * c2._imaginary;

Be:
double real = c1._real * c2._real - c1._imaginary * c2._imaginary;

because i*i=-1
• 07-12-2002
LouB
Your comments are well noted. I have noticed that - others post a general question with no code and expect you to write the whole code. I spend many hours on mine first before resorting to outside sources. And I do learn from this board, so it definately helps. But by no means do I expect someone else to write my program for me. I just need some direction and assistance with what I already have.

I appreciate your help. I see what you are getting at with your last hints, and I'm working on it. Hopefully I'll get this thing running so I can get some sleep tonight :)

Have a great night!!!
• 07-12-2002
Uraldor
golf: yup, that's right indeed. honest mistake :) but it's not my job to pick that up! :p the idea was more to help her understand operator overloading.

but yes you're right. that was wrong. it should have been -

Lou: good luck with the assignment.

time to get my arse away from this computer!

ciao!
U.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last