定义一个复数类complex 重载运算符 ,使之能用于复数的加法运算
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/12 08:13:17
//main函数里能输入两个复数.已经用VC++6.0编译通过了...希望对你有帮助#include#includeclassComplex{private:floatreal;floatimagin
教材上一般都有,这是标准例题.
classcomplex{public:complex(doubler=0,doublei=0){real=r;image=i;}doublereal;doubleimage;};complexope
#includeusingnamespacestd;classcomplex//复数类声明{public://外部接口complex(doubler=0.0,doublei=0.0){real=
importjava.util.Scanner;publicclassComplex{privateintrealPart;privateintimagPart;publicintgetRealPar
#include#includeclassComplex{public:Complex():_real(0),_imag(0){}explicitComplex(doubler):_real(r),_
classComplex{doublereal;doubleimaginary;public:Complex(double&r=0,double&i=0):real(r),imaginary(i){}
publicclassComplexDemo{//main方法publicstaticvoidmain(String[]a){Complexb=newComplex(2,5);Complexc=new
#includeusingnamespacestd;classComplex//定义complex类{public:Complex(){real=0;imag=0;}//定义构造函数Complex(d
#includeusingnamespacestd;classComplex{public:Complex(floatr1,floati1);Complex(floatr1);voidadd(Comp
//定义复数类,构造两个对象,并调用成员函数对它们进行加减乘除#include"iostream.h"classComplex{private:doublereal;doubl
#include<iostream>usingnamespacestd;classComplex{public: Complex(doubler=0,doublei=0):rea
#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubl
#includeusingnamespacestd;classcomplex{public:\x05complex(complex&c);//深度复制构造函数\x05complex(floatr,fl
只重载了加法,其他用的方法是一样的classcomplex{public:complex(doubler,doublei):real(r),imag(i){}voidprint();friend
//Complex.hclassComplex{private:floatReal;floatImag;public:Complex();Complex(floatRl,floatIm);Comple
你的构造函数里面的赋值写反了应该是real=r;imag=i;.粗心问题.亲=.=
public class $ { public static void main(String
Complexoperator+(Complex&a,Complex&b){staticc;//函数结束时为了保留值c.Real=a.Real+b.Real;c.Imag=a.Imag+b.Imag;