网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

(3)下列程序定义了一实部为real,虚部为imag的复数类complex,要求重载运算符“+”、“-”、“*”,实现复数对象的加法、减法、乘法运算,并编写测试程序。 class Complex { private: double real, imag; public: complex(double r, double i) { real=r; imag=i; } };


参考答案和解析
virtual void fun()=0;
更多 “(3)下列程序定义了一实部为real,虚部为imag的复数类complex,要求重载运算符“+”、“-”、“*”,实现复数对象的加法、减法、乘法运算,并编写测试程序。 class Complex { private: double real, imag; public: complex(double r, double i) { real=r; imag=i; } };” 相关考题
考题 有如下程序:#includeusing namespace std;class Complex{double re, im;public:Complex(double r, double i):re(r), im(i){}double real() const{return re;}double image() const{return im;}Complex operator +=(Complex a){re += a.re;im += a.im;return *this;}};ostream operator(ostream s,const Complex z){return s'('}int main(){Complex x(1, -2), y(2, 3);cout(x += y)return 0;}执行这个程序的输出结果是A . (1, -2)B . (2, 3)C . (3, 5)D . (3, 1)

考题 下面是复数类 complex 的定义 , 其中重载的运算符 “ + ” 的功能是返回一个新的复 数对象 , 其实部等于两个操作对象实部之和,其虚部等于两个操作对象虚部之和;请补充完整:class complex{double real; // 实部double imag; // 虚部public:complex(double r,double i):real(r),imag(i){}complex operator+(complex a){return complex( 【 14 】 );}};

考题 阅读以下程序说明和C++程序,将程序段中(1)~(5)空缺处的语句填写完整。【说明】以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。【C++程序】ifndef H_MiniComplexdefine H_MiniComplexinclude <iostream>using namespace std;class MiniComplex{public: //重载流插入和提取运算符(1) ostreamoperator<<(ostream osObject,const MiniComplexcomplex){osObject<<"("<<complex.realPart<<"+"<<complex.imagPart<<"i"<<")";return osObject;}(2) istreamoperator>>(istreamisObject, MiniComplexcomplex){char ch;isObject >>complex.realPart>>ch>>complex.imagPart>>ch;return isObject;}MiniComplex(double real=0,double imag=0); //构造函数MiniComplex operator+(const MiniComplexotherComplex)const; //重载运算符+MiniComplex operator-(const MiniComplexotherComplex)const; //重载运算符-MiniComplex operator*(const MiniComplexotherComplex)const; //重载运算符*MiniComplex operator/(const MiniComplexotherComplex)const; //重载运算符/bool perator==(const MiniComplexotherComplex)const; //重载运算符==private :double (3);double imagPart;};end ifinclude "MiniComplex.h"bool MiniComplex::operator==(const MiniComplexotherComplex)const{return(realPart==otherComplex.realPartimagPart==ortherComplex.imagPart);}MiniComplex::MiniComplex(double real,double imag){realPart== real; imagPart==imagPart;}MiniComplex MiniComplex::operator+(const MiniComplexotherComplex)const{MiniComplex temp;temp.realPart = realPart+ortherComplex. realPart;temp.imagPart = imagPart +ortherComplex. imagPart;return temp;}(4){ MiniComplex temp;temp.realPart= realPart-ortherComplex. realPart;temp.imagPart = imagPart-ortherComplex. imagPart;return temp;}MiniComplex MiniComplex::operator*(const MiniComplexotherComplex)const{MiniComplex temp;temp.realPart = (realPart*ortherComplex. realPart)-(imagPart *ortherComplex.imagPart);temp.imagPart = (realPart*ortherComplex. imagPart)+(imagPart *ortherComplex.realPart);return temp;}MiniComplex MiniComplex::operator/(const MiniComplexotherComplex)const{MiniComplex temp;float tt;tt=1/(ortherComplex.realPart*ortherComplex.realPart+ortherComplex.imagPart *ortherComplex. imagPart);temp.realPart=((realPart*ortherComplex, realPart)+(imagPart *ortherComplex. imagPart))*tt;temp.imagPart =((imagPart *ortherComplex. realPart)-(realPart*ortherComplex. imagPart))*tt;return temp;}include <iostream>include <MiniComplex.h>using namespace std;int main(){MiniComplex numl(23, 34),num2(56, 35);cout<<"Initial Value of num1="<<num1<<"\n Initial Value of num2="<<num2<<end1;cout<<num1<<"+"<<num2<<"="<<num1+num2<<end1; //使用重载的加号运算符cout<<num1<<"-"<<num2<<"="<<num

考题 阅读以下说明和Java源程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序能够计算三角形、矩形和正方形的周长并输出。程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算周长的抽象方法。【程序】public class girthTest{public static void main (String args[]){Figure[]figures={new Triangle (2,3,3),new Rectangle(5,8),new Square(5)};for(int i=0;i<figures.length;i++){System.out.println(figures[i]+"girth="+figures[i].getGirth());}}}public abstract class Figure{public abstract double getGirth();}public class Rectangle extends (1) {double height;double width;public Rectangle(double height,double width){this.height=height;this.width=width;}public String toString(){return "Rectangle:height="+height+",width="+width+":";}public double getGirth(){return (2);}}public class Square extends (3) {public Square(double width){(4);}public Stdng toString(){return "Square:width='+width+":";}}public class Triangle extends (5) {double la;double lb;double lc;public Triangle(double la,double lb,double lc){this.la=la;this.lb=lb;this.lc=lc;}public String toString(){return "Triangle:sides=" +la+"," +lb+"," +lc+":";}public double getGirth(){return la+lab+lc;}}

考题 阅读下列Java程序和程序说明,将应填入(n)处的字句写在对应栏内。【说明】下面的程序先构造Point类,再顺序构造Ball类。由于在类Ball中不能直接存取类Point中的xCoordinate及yCoordinate属性值,Ball中的toString方法调用Point类中的toString方法输出中心点的值。在MovingBall类的toString方法中,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值。public class Point{private double xCoordinate;private double yCoordinate;public Point 0 }public Point(ouble x, double y){xCoordinate = x;yCoordinate = y;}public String toString(){return "( + Double.toString(Coordinate)+ ","+ Double.toString(Coordinate) + ");}//other methods}public class Ball{(1); //中心点private double radius; //半径private String colour; ///颜色public Ball() { }public Ball(double xValue, double yValue, double r)// 具有中心点及半径的构造方法{center=(2);//调用类Point 中的构造方法radius = r;}public Ball(double xValue, double yValue, double r, String c)// 具有中心点、半径及颜色的构造方法{(3);//调用3个参数的构造方法colour = c;}public String toString(){return "A ball with center" + center, toString() + ", radius"+ Double.toString(radius) + ", colour" + colour;}//other methods}public class MovingBall. (4){private double speed;public MovingBall() { }public MovingBall(double xValue, double yValue, double r, String e, double s){(5);// 调用父类Ball中具有4个参数的构造方法speed = s;}public String toString( ){ return super, toString( ) + ", speed "+ Double.toString(speed); }//other methods}public class Tester{public static void main(String args[]){MovingBall mb = new MovingBall(10,20,40,"green",25);System.out.println(mb);}}

考题 有以下程序include using namespace std;class Complex{public: Complex(double r=O, 有以下程序 #include<iostream> using namespace std; class Complex { public: Complex(double r=O,double i=0):re(r),im(i){} double real() const {return re;} double imag()const {return im;} Complex operator +(Complex C) const {return Complex (re+c.re,im+c.im);} private: double re,im; }; int main() { Complex a=Complex(1,1)+Complex (5); cout<<a.real()<<'+'<<a.imag()<<'i'<<endl; retum 0; } 程序执行后的输出结果是A.6+6iB.6+1iC.1+6iD.1+1i

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

考题 ( 13 )补充完整下面的类定义:const double PI=3 .14;class Circle{ // 圆形物体的抽象基类protected:double r; // 半径public:Circle ( double radius=0 ) : r ( radius ) {}【 13 】 ; // 计算圆形物体表面积的纯虚函数声明};class Cylinder:public Circle { // 圆柱体类double h; // 高度public:Cylindr ( double radius=0, doubli height=0 ) :Circle ( radius ) , h ( height ) {}Virtual double Area () { // 计算圆柱体的表面积return 2*PI*r* ( r+h ) ;}};

考题 阅读以下说明和C++代码,将应填入(n)处的字句写在对应栏内。【说明】以下C++程序的功能是计算三角形、矩形和正方形的面积并输出。程序由4个类组成:类 Triangle、Rectangle和Square分别表示三角形、矩形和正方形:抽象类Figure提供了一个纯虚函数getAxea(),作为计算上述3种图形面积的通用接口。【C++代码】include<iostream>include<cmath>using namespace std;class Figure{public:virtual double getArea()=0;//纯虚函数};class Rectangle : (1) {protected:double height;double width;public:Rectangle(){}Rectangle(double height, double width){this->height=height;this->width=width;}double getArea(){return (2);}};class Square: (3) {public:Square(double width){(4);}};class Triangle: (5) {private:double la,lb,lc;public:Triangle(double la,double lb,double lc){this->la=la;this->1b=1b;this->lc=lc;}double getArea(){double s=(la+lb+lc)/2.0;return sqrt(s*(s-la)*(s-lb)*(s-lc));}int main(){Figure *figures[3]={new Triangle(2,3,3),new Rectangle(5,8), new Square(5)};for(int i=0;i<3;i++){cout<<"figures["<<i<<"]area="<<(figures[i])->getArea()<<endl;}return 0;}

考题 有以下程序include using namespacestd;class Complex{public:Complex (doubler=0, d 有以下程序 #include <iostream> using namespace std; class Complex { public: Complex (double r=0, double i =0 :re(r) ,im (i) {} double real() const {return re;} double imag() const { return im;} Complex operator + (Complex c} const {return Complex(re+c.re, im+c.im);} privane: double re,im; }; int main { Complex a =Complex (1,1)+Complex(5); cout<<a.real()<<'+'<<a.imag() << 'i' <<endl return 0; } 程序执行后的输出结果是A.6+6iB.6+1iC.1+6iD.1+1i

考题 有以下程序include using namespace std;class Complex{public:Complex(double r=0,d 有以下程序#include <iostream>using namespace std;class Complex{public: Complex(double r=0,double i=0):re(r),im(i){ double real() const {return re;} double imag() const { return im; } Complex operator+(Complex c) const {return Complex(re+c.re,im+c.im);}private: double re,im;};int main(){ Complex a =Complex(1,1)+ Complex(5); cout<<a.real()<<'+'<<a.imag()<<'i'<<end1; retura 0;}A.6+6iB.6+1iC.1+6iD.1+1i

考题 阅读以下说明和Java 码,将应填入(n)处的字名写在的对应栏内。[说明] 编写一个完整的JavaApplet 程序使用复数类Complex 验证两个复数1+2i 和3+4i 相加产生一个新的复数4+6i。复数类Complex 必须满足如下要求:(1) 复数类Complex 的属性有:RealPart: int 型,代表复数的实数部分ImaginPart: int 型,代表复数的虚数部分(2) 复数类Complex 的方法有:Complex():构造函数,将复数的实部和虚部都置0Complex (intr,inti):构造函数,形参r为实部的初值,i为虚部的初值。ComplexeomplexAdd (Complexa):将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者String ToString():把当前复数对象的实部、虚部组合成s+ bi 的字符串形式,其中a和b分别为实部和虚部的数据。importjava. applet. * ;importjava. awt. * ;publicclassabcextends Applet{Complex a, b, c;publi cvoid init( ){a = newComplex(1,2);b = newComplex(3,4);c = newComplex();}publievoidpaint (Graphicsg){(1)g. drawstring( “第一个复数:” + a. toString(), 10,50);g. drawstring( “第二个复数:” + b. toString( ), 10,70 );g. drawstring( “两复之和:” + c. toString( ), 10,90);}}class Complex{int RealPart;int ImaginPart;Complex( ) { (2) }Complex( intr , inti){ (3) }ComplexeomplexAdd (Complexa){Complextemp = newComplex( );temp. BealPart = RealPart + a. BealPart;(4)returntemp;}public StringtoString( ){ return( RealPart + " + " + ImaginPart + " i "); }}

考题 有如下程序: include using namespace std; class Complex { double re, im, public 有如下程序: #include <iostream> using namespace std; class Complex { double re, im, public: Complex(double r, double i): re(r), im(i) {} double real() const {return re;} double image() const {return im,} Complex operator +=(Complex a) { re +=a.re; im +=a.im; return *this; } }; ostream operator << (ostream s, const Complex z) { return s<<'('<<z.real()<<','<<z.image()<<')'; } int main() { Complex x(1,-2), y(2,3); cout << (x+=y) << endl; return 0; } 执行这个程序的输出结果是( )。A.(1,-2)B.(2,3)C.(3,5)D.(3,1)

考题 阅读以下说明和Java源程序,将应填入(n)处的字句写在答题纸的对应栏内。说明以下程序的功能是计算三角形、矩形和正方形的面积并输出。程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。程序public class AreaTest{public static void main(String args[]){Figure[]figures={new Triangle(2,3,3),new Rectangle(5,8), new Square(5)};for(int i=0;i<figures.1ength;i++){System.out.println(figures[i]+"area="+figures[i].getArea());}}}public abstract class Figure{public abstract double SetAJea();public class Rectangle extends (1) {double height;double width;public Rectangle(double height,double width){this.height=height;this.width=width;}public String toString(){return "Rectangle:height="+height+",width="+width+":";}public double getArea() { return (2);} } public class Square extends (3) {public Square(double width) {(4);}public String toString() {return "Square:width="+width+":";} } public class Triangle extends (5). {double la;double lb;double lc;public Triangle(double la,double lb,double lc) {this.la=la; this.lb=lb; this.lc=lc;public String toString(){return "Triangle: sides="+la+","+lb+","+lc+":";public double getArea() {double s=(la+lb+lc)/2.0;return Math.sqrt(s*(s-la)*(s-lb)*(s?1c));}}

考题 有如下程序: include using namespace std; class Complex { double 有如下程序: #include<iostream> using namespace std; class Complex { double re,im; public: Complex(double r,double i):re(r),im(i){} double real()const{return re;} double image()const{return im;} Complex operator+=(Complex a) { re+=a.re; im+=a.im; return *this; } }; ostream operator<<(ostream s,const Complex z) { return s<<'('<<z.real()<<','<<z.image()<<')'; } int main() { Complex x(1,2),y(2,3); tout<<(x+=y)<<endl; return 0; } 执行这个程序的输出结果是( )。A.(1,-2)B.(2,3)C.(3,5)D.(3,1)

考题 有以下程序,输出结果()。includeusing namespace std;class Complex{public:Complex(d 有以下程序,输出结果( )。 #include<iostream> using namespace std; class Complex { public: Complex(double r=0,double i=0):re(r),im(i){} double real()const{return re;} double imagoconst{return im;} Complex operator+(Complex C) const {return Complex(re+c.re,im+c.im);} private: double re,im; }; int main() { Complex a=Complex(1,1)+Complex(5); cout<<a.real()<<'+'<<a.imag()<<'i'<<end1; return 0; }A.6+iB.2i+5C.6+1iD.1i+5

考题 下列程序的执行结果为【 】。include class Point{public:Point(double i, double j) 下列程序的执行结果为【 】。include <iostream. h>class Point{public:Point(double i, double j) { x=i; y=j;}double Area() const { return 0.0;}private:double x, y;};class Rectangle: public Point{public:Rectangle(double i, double j, double k, double 1)double Area() const {return w * h;}private:double w, h;};Rectangle: :Rectangle(double i, double j, double k. double 1): Point(i,j).{w=k, h=1}void fun(Point s){cout<<s. Area()<<end1;}void main( ){Rectangle rec(3.0, 5.2, 15.0. 25.0);fun(rec)}

考题 有以下程序; #includeiostream usingnamespacestd; classComplex { public: Complex(doubler=0,doublei=O):re(r),im(i){} doublereal()const{returnre;} doubleimag()const{returnim;} Complexoperator+(Complexc)const {returnComplex(re+C.re,im+C.im);} private: doublere,im; }; intmain() { Complexa=Complex(1,1)+Complex(5); couta.real()+a.imag()iendl; return0; } 程序执行后的输出结果是( )。A.6+6iB.6+1iC.1+6iD.1+1i

考题 下面是复数类complex的定义,其中作为友元函数重载的运算符“--”的功能是将参数对象的实部减1,然后返回对该对象的引用;请补充完整。class complex{private:int real;int imag;public:complex(int r=0,int i=0):real(r),imag(i){}void show (){cout<<real<<(imag<0?"-":"+")<<imag<<'i';}______;};complex operator -- (complex c){c.real --;return c;}

考题 有下列程序:includeusing namespace std;class Complex{double re,im;public:Complex 有下列程序: #include<iostream> using namespace std; class Complex { double re,im; public: Complex(double r,double i):re(r),im(i){} double real()const{retum re;} double image()const{return im;} Complex operator+=(Complex A) { rA.(1,-2)B.(2,3)C.(3,5)D.(3,1)

考题 有以下程序: include using namespace std; class Complex{public: Complex(double 有以下程序: #include <iostream> using namespace std; class Complex { public: Complex(double r=0,double i=0):re(r),im(i){ double zeal() const {return re;} double imag() const {return im;} Complex operator+(Complex c) const { return Complex(re+c.re,im+c.im);} private: double re,im; }; int main() Complex a=Complex(1,1)+Complex(5); cout<<a.real()<<'+'<<a.imag()<<'i'<<end 1; return 0; }程序执行后的输出结果是______。A.6+6iB.6+1iC.1+6iD.1+1i

考题 使用VC6打开考生文件夹下的工程test38_3。此工程包含一个test38_3.cpp,其中定义了类complex,但类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)添加类complex的无参数的构造函数的定义,将私有成员real和imag都初始化为0。请在注释“//**1**”之后添加适当的语句。(2)添加类complex的带两个参数的重载构造函数,两个参数r和i都是double类型,要求将r赋值给私有成员real, i赋值给私有成员imag,同时允许调用时参数i可是省略,请在注释“//**2**”之后添加适当的语句。(3)完成类complex重载加法函数的定义,该函数直接返回一个类complex的对象,同时把参数对象c的实部real和虚部imag分别与当前对象对应部分分别相加,请在注释“//**3**”之后添加适当的语句。(4)完成类complex的友元函数print的定义,使其以格式“real+imagi”输出,请在注释“//**4**”之后添加适当的语句。源程序文件test38_3.cpp清单如下;include <iostream.h>class complex{public:// ** 1 **// ** 2 **{real=r;imag=i;}complex operator+(complex c);friend void print(complex c);private:double real,imag;};complex complex::operator+(complex c){// ** 3 **}// ** 4 **cout<<c.real<<"+"<<c.imag<<"i"<<endl;}void main( ){complex c1(2.0,3.0),c2(4.0,-2.0),c3;c3=c1+c2;print(c3);c3=c3+complex(3.5);print(c3);}

考题 有如下程序: include using namespace std; class Complex { dou 有如下程序: #include<iostream> using namespace std; class Complex { double re, im; public: Complex(double r, double i): re(r), im(i) {} double real() const { return re; } double image() const { return im; } Complex operator+= (Complex a) { re += a.re; im += a.im; return *this; } }; ostream operator<<(ostream s, const Complex z) { remm s<<'('<<z.real()<<','<<z.image()<<')'; } int main() { Complex x(1,-2), y(2,3); cout<<(x+=y)<<endl; return 0; } 执行这个程序的输出结果是A.(1,-2)B.(2,3)C.(3,5)D.(3,1)

考题 阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。【说明】软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。【代码13-l】/*___________________________________*//********* 文件 MiniComplex. h*********//*___________________________________*/include<iostream>using namespace std;class MiniComplex{(1)://重载流插入和提取运算符(2) ostream operator <<(ostream osObject, const MiniComplex complex){ osObject <<"("<<complex. realPart<<"+"<<complex. imagPart <<"I"<<")";return osObject;}friend (3) operator >>(istream isObject, MiniComplex complex){ char ch;isObject >>complex. realPart >>ch>>complex. imagPart >>ch;return isObject;}MiniComplex(double real=0, double imag=0); //构造函数MiniComplex operator+(const MiniComplex otherComplex)const! //重载运算符+MiniComplex operator--(const MiniComplex otherComplex)const! //重载运算符-MiniComplex operator*(const MiniComplex othmComplex)const; //重载运算符*MiniComplex operator/(const MiniComplex otherComplex)const; //重载运算符/bool perator==(const MiniComplex otherComplex)const; //重载运算符==private:double realPart; //存储实部变量double imagPart; //存储虚部变量};/*_______________________________________________________*//* * * * * * * * *文件 MiniComplex. cpp* * * * * * * * * *//*_______________________________________________________*/include "MiniComplex.h"bool MiniComplex:: perator==(const MiniComplex otherComplex)const{ (1);}MiniComplex:: MiniComplex(double real, double imag){realPart=real;imagPart=imag!}MiniComplex MiniComplex:: operator+(const MiniComplex otherComplex)const{ MiniComplex temp;temp. realPart=realPart+ otherComplex. realPart;temp. imagPart=imagPart+ otherComplex. imagPart;return temp;}MiniComplex MiniComplex::operator--(const MiniComplex otherComplex)const{ MiniComplex temp;temp.realPart=realPart-otherComplex.realPart;temp. imagPart=imagPart-otherCompler.imagPart;return temp;}MiniComplex MiniComplex:: operator*(const MiniComplex otherComplex)const{ MiniComplex temp;temp.realPart=(realPart* otherComplex.realPart)-(imag-Part* otherComplex.imag-Part);temp imagPart=(realPart* otherComplex. imagPart)+(imag-Part *otherComplex.realPart);return temp,}MiniComplex MiniComplex:: operator/(const MiniComplex otherComplex)eonst{ MiniComplex temp;float tt;tt=1/(otherComplex. realPart *otherComplex. realPart+otherComplex. imagPart* other Complex.imagPart);temp. realPart=((realPart* otherComplex.realPart)+(imagPart* otherComplex.imagPart))*tt;temp. imagPart=((imagPart * otherComplex.realPart)-(realPart* otherComplex.imagPart))*tt;

考题 有以下程序:includeusing namespace std;class Complex{public:Complex(dOuble r=0,d 有以下程序: #include<iostream> using namespace std; class Complex { public: Complex(dOuble r=0,dOuble i=0):re(r),im(i){} doublereal()const{return re;} doubleimag()const{return im;} Complex operator+(Complex c)const {return ComplexA.6+6iB.6+1iC.1+6iD.1+1i

考题 有以下程序;#includeiostreamusingnamespacestd;classComplex{public:Complex(doubler=0,doublei=O):re(r),im(i){}doublereal()const{returnre;}doubleimag()const{returnim;}Complexoperator+(Complexc)const{returnComplex(re+C.re,im+C.im);}private:doublere,im;};intmain(){Complexa=Complex(1,1)+Complex(5);couta.real()+a.imag()iendl;return0;}程序执行后的输出结果是( )。A.6+6iB.6+1iC.1+6iD.1+1i

考题 问答题关键字解释 1. INTEGER  2. REAL  3.DOUBLE PRECISION  4.COMPLEX 5.LOGICAL  6.IMPLICIT NONE   7.SELECT CASE(R3)  8.CHARACTER(LEN=13) :: AS   9. 12  FORMAT(T5,I3)  10.WRITE *,RE

考题 单选题如果Y为复数数组,则plot(Y)相当于()。A plot(real(Y))B plot(imag(Y))C plot(real(Y),imag(Y))D plot(imag(Y),real(Y))