运算符已经重载完成。但输出k仍然是(0,0)
istream& operator>>(istream& input, Complex&a)
{
input.ignore(); // skip (
input >> a.imaginary; // input imaginary
input.ignore(); // skip,
input >>a.real; // input real
input.ignore(); // skip )
return input; // enables cin >> a >> b >> c;
} // end function operator>>
int main()
{
Complex x, y(4.3, 8.2), z(3.3, 1.1), k;
cout << "Enter a complex number in the form: (a, b)\n? ";
cin>> k; // demonstrating overloaded >>
cout << "x: " << x << "\ny: " << y << "\nz: " << z << "\nk: "
<< k << '\n'; // demonstrating overloaded <<
x = y + z; // demonstrating overloaded + and =
cout << "\nx = y + z:\n" << x << " = " << y << " + " << z << '\n';
x = y - z; // demonstrating overloaded - and =
cout << "\nx = y - z:\n" << x << " = " << y << " - " << z << '\n';
x = y * z; // demonstrating overloaded * and =
cout << "\nx = y * z:\n" << x << " = " << y << " * " << z << "\n\n";
if (x != k) // demonstrating overloaded !=
cout << x << " != " << k << '\n';
cout << '\n';
x = k;
if ( x == k ) // demonstrating overloaded ==
cout << x << " == " << k << '\n';
return 0;
}
输出如下
输入了(2,3),但k输出仍为(0,0)