ddd128 于 2016.04.21 19:58 提问

#include
//using namespace std;
template
class Complex
{
public:
friend istream& operator >> (istream &input,Complex &c)
{
cin >> c.real >> c.imag;
return input;
}
friend ostream& operator << (ostream &output,Complex &c)
{
cout << "(" << c.real << "+" << c.imag << "i)" << endl;
return output;
}
//friend istream & operator >> (istream &input,Complex &);
//friend ostream & operator << (ostream &output,Complex &);
Complex(T r,T i)
{
real = r;
imag = i;
}
Complex(){}
Complex operator + (Complex &c2)
{
return Complex(real + c2.real,imag + c2.imag);
}

``````Complex operator * (Complex &c2)
{
return Complex(real * c2.real,imag * c2.imag);
}
Complex operator % (Complex &c2)
{
return Complex(real % c2.real,imag % c2.imag);
}
Complex operator - (Complex &c2)
{
return Complex(real - c2.real,imag - c2.imag);
}
``````

private:
T real,imag;
};
//template

int main()
{
Complexc1,c2,c3;
cout << "请输入第一个对象的实部和虚部" << endl;
cin >> c1;
cout << "请输入第一个对象的实部和虚部" << endl;
cin >> c2;
c3 = c1 + c2;
cout << c3;
c3 = c1 - c2;
cout << c3;
c3 = c1 * c2;
cout << c3;
c3 = c1 % c2;
cout << c3;
return 0;

}

2个回答

CSDNXIAOD   2016.04.21 20:02

C++重载<<和>>（输入输出运算符）
C++ 输入输出运算符重载

----------------------biu~biu~biu~~~在下问答机器人小D，这是我依靠自己的聪明才智给出的答案，如果不正确，你来咬我啊！

ddd128   2016.04.21 22:05