I'm writing a Class called Complex. and I have overloaded >> and <<.
but there are still something wrong in the code, please help:(
BTW, how do i set failbit to indicate improper input? the input should be the form like : 3 + 8i
Code:#include <iostream>
using namespace std;
class complex{
friend ostream& operator<<(ostream&, const complex&); //inserter
friend istream& operator>>(istream&, complex&); //extractor
public:
complex();
complex(double,double);
private:
double real, imaginary;
}; //end class Complex
complex::complex() //no parameters, both parts are 0
{
real=0.0;
imaginary=0.0;
}
complex::complex(double r, double i = 0.0) //with parameters
{
real=r;
imaginary=i;
}
ostream& operator<<(ostream &output, const complex &com )
{
output<<"("<<com.real<<"+"<<com.imaginary<<"i"<<")";
return output; //enable ouptput in form of a + bi
}
istream& operator>>(istream &input, complex &com)
{
input.ignore (); //skip (
input>>com.real;
input.ignore (3); //skip two spaces and +
input>>com.imaginary;
return input;
int main()
{
complex X;
cout << "Enter a complex number in form of (1.2 + 2.5i)";
cin>> X;
cout << "The complex number entered was: ";
cout<<X<<endl;
return 0;
}