>I'm trying to make class "Complex" with operator overload so I can perform c1+c2 etc.
>I ask user to enter complex number from keyboard and he(she)
I hope you aren't trying to ask for this input from within the class. A complex number class doesn't lend itself well to asking for its own input. I would imagine that a good complex class would look something like this:
Code:
class Complex {
public:
Complex ( const int real, const int imaginary );
Complex ( const Complex& com );
public:
int real() const;
int imaginary() const;
public:
Complex& operator= ( const int rhs );
Complex& operator+= ( const int rhs );
Complex& operator-= ( const int rhs );
Complex& operator*= ( const int rhs );
Complex& operator/= ( const int rhs );
public:
Complex& operator= ( const Complex& rhs );
Complex& operator+= ( const Complex& rhs );
Complex& operator-= ( const Complex& rhs );
Complex& operator*= ( const Complex& rhs );
Complex& operator/= ( const Complex& rhs );
private:
int real_part;
int imag_part;
};
If, on the other hand, you are parsing input from without and then passing it to your class, parsing a complete string would work, but it really depends on how your program is laid out whether or not there is a better way. I personally would take input in a more simplistic manner to make things easier, such as a complex number pair without an operation that you can work with later:
This way you can more easily use formatted input to get what you want:
Code:
vector<Complex> get_complex_vector ( string& input )
{
vector<Complex> ret;
stringstream sin ( input );
int re, im;
while ( sin>> re && sin>> im )
ret.push_back ( Complex ( re, im ) );
return ret;
}