C++程序设计习题概要1.docxVIP

  • 101
  • 0
  • 约1.63万字
  • 约 27页
  • 2017-07-02 发布于湖北
  • 举报
C程序设计习题概要1

C++程序设计练习题学院:计算机学院专业班级:物联网1002学号:0121010340705姓名:徐远志第八章1.下面是一个类的测试程序,试设计出能是用如下测试程序的类.Int main(){ Test x;x.initx(30,20);x.printx();return 0;}解:#include iostreamusing namespace std;class Test//类的开始{public:void initx(int i,int j);void printx();private:int x,y;}; void Test::initx(int i,int j){x=i;y=j;}void Test::printx(){coutx*y=x*yendl;}//类的结束int main()//测试函数{Test x;x.initx(30,20);x.printx();return 0;}得到的测试结果:4.定义并实现一个矩形类Crectangle。该类包含了下列成员函数。Crectangle(): 累的构造函数,根据需要可以定义多个构造函数SetTop(),SetLeft(): 设置矩形的左上角坐标SetLength(),SetWidth(): 设置矩形的长和宽Perimeter(): 求矩形的周长Area(): 求矩形的面积GetWidth(): 返回矩形的宽度Getlength(): 返回矩形的长度IsSquare(): 判断矩形是否为正方形Move(): 将矩形从一个位置移动到另一个位置Size(): 改变矩形的大小Where(): 返回矩形的左上角的坐标PrintRectangle(): 输出矩形的四个顶点的坐标数据成员int top,left;int length,width;解:#include iostreamusing namespace std;class Crectangle //类的开始{int top,left;int length,width;public:Crectangle(int t=0,int l=0,int len=1,int w=1) {top=t;left=l;if (len0) length=len; else length=0;if (w0) width=w; else width=0;}void SetTop(int t){top=t;}void SetLeft(int l){left=l;}void SetLength(int len){length=len;}void SetWidth(int w){width=w;}int Perimeter(){return 2*(width+length);}int Area(){return width*length;}int GetWidth(){return width;}int GetLength(){return length;}int IsSquare(){if(width==length){cout该矩形是正方形endl;return 1;}else{cout该矩形不是正方形endl;return 0;}}void Move(int x,int y){ left+=x; top+=y;}void Size(int l,int w) {length=l; width=w; }void PrintRectangle(){cout左上方的点为:(left,top)endl;cout右上方的点为:(left+length,top)endl;cout左下方的点为:(left,top+width)endl;cout右下方的点为:(left+length,top+width)endl;}};//类的结束int main(){Crectangle a(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;retu

文档评论(0)

1亿VIP精品文档

相关文档