您的位置首页百科问答

C++ 定义来自一个复数类Complex重载360问答运算符 “+”, “-”, “ * ”, “ / ”........................

C++ 定义来自一个复数类Complex重载360问答运算符 “+”, “-”, “ * ”, “ / ”........................

的有关信息介绍如下:

问题补充说明:定义一个复数类Complex重载运算符 “+”, “-”, “ * ”, “ / ”, 使之能用于复数的加,减,乘,除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数的和,差,积,商。

C++ 定义来自一个复数类Complex重载360问答运算符 “+”, “-”, “ * ”, “ / ”........................

#include<iostream>

usingnamespacestd;

classCComplex

{

private:

floatreal,image;

public:

CComplex(){}

CComplex(floatr,floatimg){re行协国建向渐始al=r;image=img;}

CComplex(CComplex&another)

{

real=another.real;

image=another.image;

}

CC把动据个宗办与omplex&operator=(CComplex&another)

{

real=another.real;

image=another.ima液层井主专ge;

return*this岩和卫怎;

}

CComplexoper常代群ator+(CComplex&another)

{

returnCComplex(real+another.real,image+another.image);

}

CComplexoperator-(CCo粉重承血mplex&another)

{

ret沙盾子活倒诉宪放行urnCComplex(real-another.rea旧假面l,image-another.image);

}

CComplexoperator*(CComplex&another)

{

CComplexprod;

//p新秋罪立相支染刚防货位rod=*this;

prod.real=real*another.real-image*another.image;

prod.image=real*another.image+image*another.real;

returnprod下离若;

//returnCComplex(real+another.real,image+another.image);

}

CComplexoperator/(CC离选初洋决黄牛然饭路omplex&another)

{

CComplexquot;

floatsq=another.real*ano度执作边转从ther.real+烧地月还扬杨another.image*another.image;

quot.real=(real*another.real+image*another.image)/华情朝控点然铁袁只但sq;

quot.image=(image*卷统汉鲜艺侵笔端another.real-real*another.image)/sq;

returnqu度施ot;

}

};

voidmain()

{

CComplexc1(2,3),c2(3,3);

CComplexc4,c5,c6,c7;

c4=c1+c2;

c5=c1-c2;

c6=c1*c2;

c7=c1/c2;

}