2 ddd128 ddd128 于 2016.04.21 19:58 提问

类模版对运算符重载以及输入输出重载
c++

#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
CSDNXIAOD   2016.04.21 20:02

C++重载<<和>>(输入输出运算符)
C++ 输入输出运算符重载
重载输入输出运算符
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

ddd128
ddd128   2016.04.21 22:05

啊,解决了,太粗心了图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!