永发信息网

计算机编程题

答案:1  悬赏:80  手机版
解决时间 2021-07-30 04:39
  • 提问者网友:鼻尖触碰
  • 2021-07-30 00:56
请大家帮我做一下这道编程题(编程环境c++):设计一个复数类,提供构造函数、拷贝构造函数和析构函数,并重载+,-,*,/,+=,-=,==,!=,++和―运算符的重载,要求重载为成员函数。
最佳答案
  • 五星知识达人网友:鸠书
  • 2021-07-30 02:24

#include <iostream>
using namespace std;
class complex{
public:
double sb;
double xb;
complex(){sb=0,xb=0;};
complex(double k,double x);
void display();
complex operator +(complex &p);
complex operator -(complex &y);
complex operator *(complex &p);
complex operator /(complex &p);
};
complex::complex(double k,double x):sb(k),xb(x){}
complex complex::operator +(complex &p)
{
complex c;
c.sb=sb+p.sb;
c.xb=xb+p.xb;
return c;
}
complex complex::operator -(complex &y)
{
complex c;
c.sb=sb-y.sb;
c.xb=xb-y.xb;
return c;
}
complex complex::operator*(complex &p)
{
complex c;
c.sb=sb*p.sb;
c.xb=xb*p.xb;
return c;
}
complex complex::operator /(complex &p)
{
complex c;
c.sb=sb / p.sb;
c.xb=xb / p.xb;
return c;
}
void complex::display()
{cout <<"(" <<sb<<","<<xb<<"i)";}
int main()
{
complex t1(7,8),t2(2,9),t3;
t3=t1+t2;
cout <<"t1+t2";
t3.display();
t3=t1-t2;
cout<<"t1-t2";
t3.display();
t3=t1*t2;
cout<<"t1*t2";
t3.display();
t3=t1/t2;
cout<<"t1/t2";
t3.display();
return 0;
}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯