有如下复数类的声明,请补充完整。 class complex { double real; //实部 double imag; //虚部 public: complex(double x,double y) {

admin2009-06-20  39

问题 有如下复数类的声明,请补充完整。
   class complex {
     double real;  //实部
     double imag;  //虚部
   public:
     complex(double x,double y) {
         real=x;
         imag=y;
     }
     complex operator +(complex  c) {   //重载加法运算符"+"
         return complex(【  】);
     }
   };

选项

答案real+c.real,imag+c.imag或this->real+c.real,this->imag+c.imag,注意:运算符+的数据可交换次序

解析 本题考查的知识点是:运算符重载。根据题目的要求,要实现类complex的重载加法运算符“+”。复数的加法是将其实部与虚部分别相加,因此空处应填入 real+c.real,imag+c.imag,这样就构造出一个临时的complex对象,并且它的实部和虚部分别为自身和形参c的实部与虚部之和,然后通过return语句返回这个结果。
转载请注明原文地址:https://kaotiyun.com/show/lojp777K
0

最新回复(0)