C程序设计习题.docxVIP

  • 3
  • 0
  • 约1.59万字
  • 约 31页
  • 2021-06-27 发布于山东
  • 举报
精品文档 精品文档 PAGE PAGE27 精品文档 PAGE C++程序设计 练习题 学院:计算机学院 专业班级: 物联网1002 学号: 姓名: 徐 远 志 第八章 下面是一个类的测试程序,试设计出能是用如下测试程序的类.Intmain() { Testx;x.initx(30,20);x.printx();return0; } 解: #includeiostream usingnamespacestd; 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; } 得到的测试结果: 定义并实现一个矩形类Crectangle。该类包含了下列成员函数。 Crectangle():累的结构函数,根据需要能够定义多个结构函数 SetTop(),SetLeft():设置矩形的左上角坐标 SetLength(),SetWidth():设置矩形的长和宽 Perimeter():求矩形的周长 Area():求矩形的面积 GetWidth():返回矩形的宽度 Getlength():返回矩形的长度 IsSquare():判断矩形是否为正方形 Move():将矩形从一个位置移动到另一个位置 Size():改变矩形的大小 Where():返回矩形的左上角的坐标 PrintRectangle(): 输出矩形的四个极点的坐标 数据成员 inttop,left; intlength,width; 解: #includeiostream usingnamespacestd; 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 cout cout cout  左上方的点为右上方的点为左下方的点为右下方的点为  :(left,top)endl;:(left+length,top)endl;:(left,top+width)endl;:(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个评委给选手打分,参加比赛的选手有

文档评论(0)

1亿VIP精品文档

相关文档