ddd128 2016-04-21 11:58 采纳率: 0%
浏览 1753

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

#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;

}

  • 写回答

1条回答

  • ddd128 2016-04-21 14:05
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置