c++中怎样求复数相加
1个回答
展开全部
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator+(Complex &c1,Complex &c2);
void display();
private:
double real,imag;
};
Complex operator+(Complex &c1,Complex &c2)
{
Complex c;
c.real = c1.real+c2.real;
c.imag = c1.imag+c2.imag;
return c;
}
void Complex::display()
{
cout << "("<<real<<","<<imag<<"i)"<<endl;
}
int main(void)
{
Complex c1(5,1),c2(3,1),c3;
c3=c1+c2;
cout << "c1+c2=";
c3.display();
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询