C++程序设计练习题学院:计算机学院专业班级:物联网1002学号:0121010340705姓名:徐远志第八章1.下面是一个类的测试程序,试设计出能是用如下测试程序的类.Intmain(){Testx;x.initx(30,20);x.printx();return0;}解:#includeiostreamusingnamespacestd;classTest//类的开始{public:voidinitx(inti,intj);voidprintx();private:intx,y;};voidTest::initx(inti,intj){x=i;y=j;}voidTest::printx(){coutx*y=x*yendl;}//类的结束intmain()//测试函数{Testx;x.initx(30,20);x.printx();return0;}得到的测试结果:4.定义并实现一个矩形类Crectangle。该类包含了下列成员函数。Crectangle():累的构造函数,根据需要可以定义多个构造函数SetTop(),SetLeft():设置矩形的左上角坐标SetLength(),SetWidth():设置矩形的长和宽Perimeter():求矩形的周长Area():求矩形的面积GetWidth():返回矩形的宽度Getlength():返回矩形的长度IsSquare():判断矩形是否为正方形Move():将矩形从一个位置移动到另一个位置Size():改变矩形的大小Where():返回矩形的左上角的坐标PrintRectangle():输出矩形的四个顶点的坐标数据成员inttop,left;intlength,width;解:#includeiostreamusingnamespacestd;classCrectangle//类的开始{inttop,left;intlength,width;public:Crectangle(intt=0,intl=0,intlen=1,intw=1){top=t;left=l;if(len0)length=len;elselength=0;if(w0)width=w;elsewidth=0;}voidSetTop(intt){top=t;}voidSetLeft(intl){left=l;}voidSetLength(intlen){length=len;}voidSetWidth(intw){width=w;}intPerimeter(){return2*(width+length);}intArea(){returnwidth*length;}intGetWidth(){returnwidth;}intGetLength(){returnlength;}intIsSquare(){if(width==length){cout该矩形是正方形endl;return1;}else{cout该矩形不是正方形endl;return0;}}voidMove(intx,inty){left+=x;top+=y;}voidSize(intl,intw){length=l;width=w;}voidPrintRectangle(){cout左上方的点为:(left,top)endl;cout右上方的点为:(left+length,top)endl;cout左下方的点为:(left,top+width)endl;cout右下方的点为:(left+length,top+width)endl;}};//类的结束intmain(){Crectanglea(1,1,5,5);a.PrintRectangle();a.SetTop(2);a.SetLeft(3);a.SetLength(4);a.SetWidth(5);a.IsSquare();cout周长为:a.Perimeter()endl;cout面积为:a.Area()endl;a.PrintRectangle();cout用getwidth等函数得到的值为:endl;cout长为:a.GetLengthendl;cout宽为:a.GetWidthendl;return0;}得到的测试结果:6.某次歌手大赛中,有JudgeNum个评委给选手打分,参加比赛的选手有PlayerNum名,现为比赛积分编写一个CompetitionResult类,类的定义如下:(定义略)(1)写出所有的成员函数和实现代码。(2)编写main()函数对该类进行测试。在函数体中,定义了一个competitionResult类的对象数组r[PlauerNum],他的每个元素记录了每个选手的所有信息,个评委的打分通过键盘的输入,在屏幕上应有提示信息进行交互式操作,比赛结果按选手得分从高到底排列输出。解:(1)CompetitionResult::CompetitionResult(){num=0;strcpy(name,);for(inti=0;iJudgeNum;i++)score[i]=0.0;average=0;}CompetitionResult::CompetitionResult(shortn,char*ps){num=n;strcpy(name,ps);for(inti=0;iJudgeNum;i++)score[i]=0.0;average=0;}floatCompetitionResult::MaxScore(){intmax=score[0];for(inti=0;iJudgeNum;i++)if(maxscore[i])max=score[i];returnmax;}floatCompetitionResult::MinScore(){intmin=score[0];for(inti=0;iJudgeNum;i++)if(minscore[i])min=score[i];returnmin;}voidCompetitionResult::SetAvg(){inti;floatsum=0.0;for(i=0;iJudgeNum;i++)sum+=score[i];sum=sum-MaxScore()-MinScore();average=sum/(JudgeNum-2);}floatCompetitionResult::GetAvg(){returnaverage;}shortCompetitionResult::GetNo(inti){returnnum;}voidCompetitionResult::SetNo(inti){num=i;}charCompetitionResult::*GetName(){returnname;}floatCompetitionResult::GetScore(intj){returnscore[j];}voidCompetitionResult::Setscore(intk,floatav){score[k]=av;}voidSort(CompetitionResult*pr,intn){inti,j,k;CompetitionResulttemp;for(i=0;in-1;i++){k=i;for(j=i+1;jn;j++)if((pr[j].average)(pr[k].average))k=j;if(k!=j){temp=pr[i];pr[i]=pr[k];pr[k]=temp;}}}(2)intmain(){CompetitionResultplayer[PlayerNum]={CompetitionResult(1,one),CompetitionResult(2,two),CompetitionResult(3,there),CompetitionResult(4,four),CompetitionResult(5,five),CompetitionResult(6,six),CompetitionResult(7,seven),CompetitionResult(8,eight),CompetitionResult(9,nine),CompetitionResult(10,ten)};floata;for(inti=0;iPlayerNum;i++){cout请评委给第i+1个选手打分:endl;for(intj=0;jJudgeNum;j++){cina;player[i].Setscore(j,a);}player[i].SetAvg();coutendl;}Sort(player,PlayerNum);cout最后的得分情况为:endl;for(intj=0;jPlayerNum;j++){coutj+1;coutsetw(6)player[j].GetAvg();coutplayer[j].GetName()endl;}return0;}程序运行的结果为:第九章1.在下面的程序中,派生类Derived的成员函数sets()能否访问基类base中得变量a和b?为什么?如果在基类base中将数据成员a和b定义为私有成员,下面的程序能否通过编译?为什么?#includeiostreamusingnamespacestd;classBase{public:voidset(inti,intj){a=i;b=j;}voidshow(){couta=a,b=bendl;}protected:inta,b;};classDerived:publicBase{public:voidsets(){s=a*b;}voidshows(){couts=sendl;}private:ints;};intmain(){Derivedobj;obj.set(2,3);obj.show();obj.sets();obj.shows();return0;}解:可以访问a和b;因为a,b为保护成员,经公有继承,成为派生类中得保护成员,所以派生类Derived的成员函数sets()能访问基类base中得变量a和b。如果定义为私有,则不能通过编译。因为派生类中的成员函数不能访问基类中得私有成员2.声明一个图形基类Shape,在它的基础上派生出矩形类Rectangle和圆形类Circle,它们都有计算面积的和周长、输出图形信息的成员函数,再在Rectangle类的基础上派生方形类Square。编写程序和各类的定义和实现,以及类的使用。解:#includeiostreamusingnamespacestd;classShape{public:doublegetArea(){}doublegetPerimeter(){}};classRectangle:publicShape{protected:doubleheight;doublewidth;public:Rectangle(){}Rectangle(doublea,doubleb){height=a;width=b;}doublegetArea(){returnheight*width;}doublegetPerimeter(){return2*(height+width);}};classCircle:publicShape{public:Circle(doublex):r(x){}doublegetPerimeter(){return2*3.1416*r;}doublegetArea(){returnr*r*3.1416;}private:doubler;};classSquare:publicRectangle{public:Square(doublex