#include <iostream>
using namespace std;
//重载流插入/流提取运算符
class Complex
{
public:
friend ostream& operator <<(ostream&, Complex&);
friend istream& operator >> (istream&, Complex&);
private:
double real;
double imag;
};
ostream& operator <<(ostream& output, Complex &c)
{
output << "(" << c.real;
if (c.imag >= 0)//判断输入
output << "+";
output<< c.imag << "i)"<<endl;
return output;
}
istream& operator >> (istream& input, Complex &c)
{
input >> c.real >> c.imag;
return input;
}
int main()
{
Complex c1, c2;
cin >> c1 >> c2;
cout << "c1=" << c1 << endl;
cout << "c2=" << c2 << endl;
return 0;
}
重载流插入/流提取运算符
最新推荐文章于 2024-02-22 15:44:53 发布