- 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个评委给选手打分,参加比赛的选手有
您可能关注的文档
- CWDM测试方法计划.docx
- Cytoscape软件画图说明.docx
- C上机实验报告计划实验四.docx
- c人行道隔离护栏施工实施方案.docx
- C及232串口通信方法计划.docx
- C基础培训教材(001).docx
- C基础培训教材001.docx
- C学生管理系统课程设计报告计划.docx
- C实验二类与对象(附)..docx
- C实验学习报告计划.docx
- 2025-2026学年天津市和平区高三(上)期末数学试卷(含解析).pdf
- 2025-2026学年云南省楚雄州高三(上)期末数学试卷(含答案).pdf
- 2025-2026学年甘肃省天水市张家川实验中学高三(上)期末数学试卷(含答案).docx
- 2025-2026学年福建省厦门市松柏中学高二(上)期末数学试卷(含答案).docx
- 2025-2026学年广西钦州市高一(上)期末物理试卷(含答案).docx
- 2025-2026学年河北省邯郸市临漳县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省石家庄二十三中七年级(上)期末历史试卷(含答案).docx
- 2025-2026学年海南省五指山市九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省唐山市玉田县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省邢台市市区九年级(上)期末化学试卷(含答案).docx
最近下载
- 心源性晕厥临床表现及护理.pptx VIP
- 食物不耐受IgG解读.pptx
- 2021 固态锂电池用固态电解质性能要求及测试方法 无机氧化物固态电解质.docx VIP
- 毕业论文(设计)小型混凝土搅拌机设计.doc VIP
- 人教版(2024)新教材八年级数学下册第20章20.1 勾股定理及其应用每课时教学设计汇编(含三个教学设计).docx VIP
- 2025CSCO前列腺癌诊疗指南更新解读(全文).pdf VIP
- 湘教版(2024)新教材八年级地理下册第六章第二节《东北地区的人口与城乡分布》精品教学设计.docx VIP
- 山西省2024年中考英语模拟试卷(含答案)2.pdf VIP
- 2025年食品安全国家标准-食品生产卫生规范GB14881培训考试试题及参考答案.docx VIP
- 语文 《红楼梦》阅读中“选择题”解题技巧阐释与训练 2024-2025学年统编版高一语文必修下册.docx VIP
原创力文档

文档评论(0)