您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 浙大远程_面向对象程序设计的_离线作业答案
22.3(C++C)//thisisastrange/*waytodoacomment*//**/2.4C++#includeiostreamusingnamespacestd;sum(inta,intb)intmain(){inta,b,c;coutEntertwonumbers:;cinab;c=sum(a,b);coutsumis:c;return0;}sum(inta,intb){returna+b;}.usingnamespacestd;sum(inta,intb)2.5(1)floatfun(inta,floatb,char*c);floatfun(int,float,char*);(2)floatfun(inta,floatb,char*c);floatfun(int,float,char*);12112.6(D)Aint*p=newint(10)Bint*p=newint[10]Cint*p=newintDint*p=newint[40](0)2.7constchar*constname=chen;(D)A.name[3]='a';B.name=lin;C.name=newchar[5];D.coutname[3];2.8char*constname=chen;(A)A.name[3]='q';B.name=lin;C.name=newchar[5];D.name=newchar('q');2.9constchar*name=chen;(A)A.name[3]='q';B.name=lin;C.name=newchar[5];D.name=newchar('q');2.10,BABCD2.11AABCD.2.12CA.B.C.D.2.13C++BABCD.2.14DA.int&a[4]B.int&*pC.int&&qD.inti,*p=&i2.15CABC.D2.16newDAB.deleteC.D.2.17deleteCAnewB.C.D2.18#includeiostreamusingnamespacestd;inti=15;intmain(){inti;i=100;::i=i+1;cout::iendl;return0;}1012.19#includeiostreamusingnamespacestd;voidf(int&m,intn){inttemp;temp=m;m=n;n=temp;}intmain(){inta=5,b=10;f(a,b);coutabendl;return0;}10,102.20#includeiostreamusingnamespacestd;int&f(int&i){i+=10;returni;}intmain(){intk=0;int&m=f(k);coutkendl;m=20;coutkendl;return0;}10202.21constdefinedefine2.22C++Fibonacci20#includeiostreamusingnamespacestd;intmain(){int*p=newint[20];*p=1;*(p+1)=1;cout*pt*(p+1)tp=p+2;for(inti=3;i=20;i++){*p=*(p-1)+*p(p-2);Cout*ptIf(i%5==0)coutendl;P++;}}2.23C++sroot(),sroot()3,sqrt#includeiostream#includecmathUsingnamespacestd;Doublesroot(inti){Returnsqrt(i);}Doublesroot(longl){returnsqrt(l);}Doublesroot(doubled){returnsqrt(d);}Intmain(){Inti=12;Longl=1234;Doubled=12.34;Couti:sroot(i)endl;Coutl:sroot(l)endl;Coutd:sroot(d)endl;Return0;}2.24C++125#includeiostreamUsingnamespacestd;Intmain(){IntI,j,sum=0;For(i=0;i=20;i++)For(j=0;j=50;j++)If(100-5*I-2*j=0){Sum++;Cout100-5*I2*jtjtiendl;}Coutsumissumendl;Return0;}2.25C++#includeiostreamUsingnamespacestd;Intmain(){voidchange(int&,int&);Inta,b;Cinab;If(ab)change(a,b);Coutabendl;Return0;}Voidchange(int&a1,int&b1){inttemp;Temp=a1;A1=b1;B1=temp;}3.7BABCD3.8CABregisterCprivatepublicprotectedDprivatepublicprotected3.9CAB.C.D.3.10BAB.C.D3.11BAthisB.,C.staticD.3.12AAB.C.D.friend3.13AABC.D.3.14(B)#includeiostreamusingnamespacestd;classB{public:B(){}B(inti,intj){x=i;y=j;}voidprintb(){coutx,yendl;}private:intx,y;};classA{public:A(){}A(intI,intj);voidprinta();private:Bc;};A::A(inti,intj):c(i,j){}voidA::printa(){c.printb();}intmain(){Aa(7,8);a.printa();return0;}A8,9B.7,8C.5,6D.9,103.15(A)#includeiostreamusingnamespacestd;classA{public:voidset(inti,intj){x=i;y=j;}intget_y(){returny;}private:intx,y;};classbox{public:voidset(intl,intw,ints,intp){length=1;width=w;label.set(s,p);}intget_area(){returnlength*width;}private:intlength,width;Alabel;};intmain(){boxb;b.set(4,6,1,20);coutb.get_area()endl;return0;}A24B.4C.20D.63.16B#includeiostreamusingnamespacestd;classSample{public:Sample(inti,intj){x=i;y=j;}voiddisp(){coutdisp1endl;}voiddisp()const{coutdisp2endl;}private:intx,y;};intmain(){constSamplea(1,2);a.disp();return0;}A.disp1B.disp2C.disp1disp2D.3.17B#includeiostreamusingnamespacestd;classR{public:R(intr1,intr2){R1=r1;R2=r2;}voidprint();voidprint()const;private:intR1,R2;};voidR::print(){coutR1,R2endl;}voidR::print()const{coutR1,R2endl;}intmain(){Ra(6,8);constRb(56,88);b.print();return0;}A.6,8B.56,88C.0,0D.8,63.18#includeiostreamusingnamespacestd;classtoy{public:toy(intq,intp){quan=q;price=p;}intget_quan(){returnquan;}intget_price(){returnprice;}private:intquan,price;};intmain(){toyop[3][2]={0,0toy(10,20),toy(30,48),toy(50,68),toy(70,80),toy(90,16),toy(11,120),};for(inti=0;i3;i++){coutop[i][0].get_quan(),;coutop[i][0].get_price()\n;coutop[i][1].get_quan(),;coutop[i][1].get_price()\n;}coutendl;return0;}10,2030,4850,6870,8090,1611,1203.19#includeiostreamusingnamespacestd;classexample{public:example(intn){i=n;coutConstructing\n;}~example(){coutDestructing\n;}intget_i(){returni;}private:inti;};intsqr_it(exampleo){returno.get_i()*o.get_i();}intmain(){examplex(10);coutx.get_i()endl;coutsqr_it(x)endl;return0;}Constructing10Destructing100Destructing3.20#includeiostreamusingnamespacestd;classaClass{public:aClass(){total++;}~aClass(){total--;}intgettotal(){returntotal;}private:staticinttotal;};intaClass::total=0;intmain(){aClasso1,o2,o3;couto1.gettotal()objectsinexistence\n;aClass*p;p=newaClass;if(!p){coutAllocationerror\n;return1;}couto1.gettotal();coutobjectsinexistenceafterallocation\n;deletep;couto1.gettotal();coutobjectsinexistenceafterdeletion\n;return0;}3objectsinexistence4objectsinexistenceafterallocation3objectsinexistenceafterdeletion3.21#includeiostreamusingnamespacestd;classtest{public:test();~test(){};private:inti;};test::test(){i=25;for(intctr=0;ctr10;ctr++){coutCountingatctr\n;}}testanObject;intmain(){return0;}Countingat0C
本文标题:浙大远程_面向对象程序设计的_离线作业答案
链接地址:https://www.777doc.com/doc-8705950 .html