面向对象程序设计期末综合练习四(运行结果)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1面向对象程序设计期末综合练习四(运行结果)写出程序运行结果28.#includeiostream.h#includestring.hclassCD{char*a;intb;public:voidInit(char*aa,intbb){a=newchar[strlen(aa)+1];strcpy(a,aa);b=bb;}char*Geta(){returna;}intGetb(){returnb;}voidOutput(){couta''bendl;}}dx;voidmain(){CDdy;dx.Init(abcdef,30);dy.Init(shenyafen,3*dx.Getb()+5);dx.Output();dy.Output();}29.#includeiostream.h#includestring.hclassCD{char*a;intb;public:voidInit(char*aa,intbb){a=newchar[strlen(aa)+1];strcpy(a,aa);b=bb;}char*Geta(){returna;}2intGetb(){returnb;}voidOutput(){couta''bendl;}};voidmain(){CDdx,dy;chara[20];dx.Init(abcdef,30);strcpy(a,dx.Geta());strcat(a,xyz);dy.Init(a,dx.Getb()+20);dx.Output();dy.Output();}30.#includeiostream.hclassCE{private:inta,b;intgetmax(){return(ab?a:b);}public:intc;voidSetValue(intx1,intx2,intx3){a=x1;b=x2;c=x3;}intGetMax();};intCE::GetMax(){intd=getmax();return(dc?d:c);}voidmain(){intx=5,y=12,z=8;CEex,*ep=&ex;ex.SetValue(x,y,z);coutex.GetMax()endl;ep-SetValue(x+y,y-z,20);coutep-GetMax()endl;}331.#includeiostream.hclassCE{private:inta,b;intgetmin(){return(ab?a:b);}public:intc;voidSetValue(intx1,intx2,intx3){a=x1;b=x2;c=x3;}intGetMin();};intCE::GetMin(){intd=getmin();return(dc?d:c);}voidmain(){intx=5,y=12,z=8;CE*ep;ep=newCE;ep-SetValue(x+y,y-z,10);coutep-GetMin()endl;CEa=*ep;couta.GetMin()*3+15endl;}32.#includeiostream.hclassFranction{//定义分数类intnume;//定义分子intdeno;//定义分母public://把*this化简为最简分数,具体定义在另外文件中实现voidFranSimp();//返回两个分数*this和x之和,具体定义在另外文件中实现FranctionFranAdd(constFranction&x);//置分数的分子和分母分别0和1voidInitFranction(){nume=0;deno=1;}//置分数的分子和分母分别n和dvoidInitFranction(intn,intd){nume=n;deno=d;}//输出一个分数voidFranOutput(){coutnume'/'denoendl;}4};voidmain(){Franctiona,b,c,d;a.InitFranction(7,12);b.InitFranction(-3,8);c.InitFranction();c=a.FranAdd(b);d=c.FranAdd(a);couta:;a.FranOutput();coutb:;b.FranOutput();coutc:;c.FranOutput();coutd:;d.FranOutput();}33.#includeiostream.hclassFranction{//定义分数类intnume;//定义分子intdeno;//定义分母public://把*this化简为最简分数,具体定义在另外文件中实现voidFranSimp();//返回两个分数*this和x之和,具体定义在另外文件中实现FranctionFranAdd(constFranction&x);//置分数的分子和分母分别0和1voidInitFranction(){nume=0;deno=1;}//置分数的分子和分母分别n和dvoidInitFranction(intn,intd){nume=n;deno=d;}//输出一个分数voidFranOutput(){coutnume'/'denoendl;}};voidmain(){Franctiona,b,c,d;a.InitFranction(6,15);b.InitFranction(3,10);c.InitFranction();c=a.FranAdd(b);d=c.FranAdd(a);couta:;a.FranOutput();5coutb:;b.FranOutput();coutc:;c.FranOutput();coutd:;d.FranOutput();}34.#includeiostream.h#includestring.hclassA{char*a;public:A(char*s){a=newchar[strlen(s)+1];strcpy(a,s);coutaendl;}~A(){delete[]a;coutDestructor!endl;}};voidmain(){Ax(xuxiaokai);A*y=newA(weirong);deletey;}35.#includeiostream.hclassA{int*a;public:A(intx=0):a(newint(x)){}~A(){deletea;}intgetA(){return*a;}voidsetA(intx){*a=x;}};voidmain(){Ax1,x2(3);A*p=&x2;p-setA(x2.getA()+5);x1.setA(15+x1.getA());6coutx1.getA()''x2.getA()endl;}36.#includeiostream.hclassA{inta;public:A(intaa=0):a(aa){couta'';}~A(){coutXxk;}};voidmain(){A*p;Ax[3]={1,2,3},y=4;coutendl;p=newA[3];coutendl;delete[]p;coutendl;}37.#includeiostream.hclassA{inta,b;public:A(){a=b=0;}A(intaa,intbb){a=aa;b=bb;}intSum(){returna+b;}int*Mult(){int*p=newint(a*b);returnp;}};voidmain(){int*k;Ax(2,3),*p;p=newA(4,5);coutx.Sum()''*(x.Mult())endl;coutp-Sum()''*(k=p-Mult())endl;deletek;}738.#includeiostream.hclassA{inta[10];intn;public:A(intaa[],intnn):n(nn){for(inti=0;in;i++)a[i]=aa[i];}intGet(inti){returna[i];}intSumA(intn){ints=0;for(intj=0;jn;j++)s+=a[j];returns;}};voidmain(){inta[]={2,5,8,10,15,20};Ax(a,4);Ay(a,6);intd=1;for(inti=0;i4;i++)d*=x.Get(i);intf=y.SumA(5);coutd=dendl;coutf=fendl;}39.#includeiostream.hclassA{inta,b;public:A(intaa,intbb){a=aa;b=bb;}floatMultip(charop){switch(op){case'+':returna+b;case'-':returna-b;case'*':returna*b;case'/':if(b!=0)returnfloat(a)/b;else{cout除数为0!endl;return0;}default:cout'\n'op非法运算符!endl;return0;8}}};voidmain(){Ax(10,4);chara[6]=+-*/@;inti=0;while(a[i]){floatk=x.Multip(a[i]);if(k!=0)coutk'';i++;}coutendl;}40.#includeiostream.hclassPoint{intx,y;public:Point(intx1=0,inty1=0):x(x1),y(y1){coutPoint:x''y'\n';}~Point(){coutPointdestructor!\n;}};classCircle{Pointcenter;//圆心位置intradius;//半径public:Circle(intcx,intcy,intr):center(cx,cy),radius(r){coutCircleradius:radius'\n';}~Circle(){coutCircledestructor!\n;}};voidmain(){Circlec(3,4,5);}941.#includeiostream.h#includestring.hclassPoint{intx,y;public:Point(intx1=0,inty1=0):x(x1),y(y1){coutPoint:x''y'\n';}~Point(){coutPointdes!\n;}};classText{chartext[100];//文字内容public:Text(char*str){strcpy(text,str);coutTextcon!\n;}~Text(){coutTextdes!\n;}};classCircleWithText:publicPoint,publicText{public:CircleWithText(intcx,intcy,char*msg):Point(cx,cy),Text(msg){coutPointwithTextcon!\n;}~CircleWithText(){coutPointwithTextdes\n;}};voidmain(){CircleWithTextcm(3,4,hello);}42.#includeiostream.hclassDate{public:voidSetDate(inty

1 / 18
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功