实验报告 5.docx
- 文档编号:26136088
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:9
- 大小:45.65KB
实验报告 5.docx
《实验报告 5.docx》由会员分享,可在线阅读,更多相关《实验报告 5.docx(9页珍藏版)》请在冰豆网上搜索。
实验报告5
学生实验报告
学生姓名
王国文
学号
0103347
同组人
实验项目
□必修□选修
□演示性实验□验证性实验□操作性实验□综合性实验
实验地点
机房
实验仪器台号
指导教师
杨勇
实验日期及节次
2011.10.13
一、实验综述
1、实验目的及要求
1.GivenaRectangleclass,whichhasWidthandHeightattributesandthefollowingbehaviors:
(1)Init():
toinitializetheWidthandHeight
(2)SetWidth():
tosetWidth
(3)SetHeight():
tosetHeight
(4)GetWidth():
togetWidth
(5)GetHeight():
togetHeight
(6)Area():
tocalculatetheareaoftherectangle
(7)DispInfo():
toshowtheinformationoftherectanglesuchasWidth,Heightandarea.
Pleaseanalyzeanddesigntheclassbasedontheaboverequirements.
1.定义一个长方形Rectangle类,它有宽、高(Width,Height)和一些计算方法:
(1)Init():
设定宽、高的初始值
(2)SetWidth():
设定宽度
(3)SetHeight():
设定高度
(4)GetWidth():
得到宽度
(5)GetHeight():
得到高度
(6)Area():
计算长方形的面积
(7)DispInfo():
显示长方形的信息(包括宽、高和面积)
分析并设计:
(1)Rectangle类中有哪些数据成员,应定义为公有的还是私有的。
(2)Rectangle类中有哪些成员函数,应定义为公有的还是私有的。
并设计编写这些函数。
(3)编写一个主函数对Rectangle类实现调试。
2.DesignaclassCircle,whichcanbeusedtocalculatetheareaandcircumferenceofacircle.Theradiusissetwheninitializinganobject.Pleasegiveamainfunctiontocalltheclass.
2.定义一个Circle类,可以计算圆的面积和圆的周长。
圆的半径通过说明对象时由初始化完成。
编写相应的调试用函数,来测试该类的正确性。
3.DesignaRectangleclasswithtwoattributes,WidthandHeightandtosatisfythefollowingrequirements.Whentheobjectisinitializedbytwoarguments,whichrepresentWidthandHeightrespectively.But,whentheobjectisinitializedbyonlyoneargument,whichmeanstherectangleisasquare.ThereisamemberfunctiontoshowtheareaoftheRectangleintheclass.Pleasealsogiveamainfunctiontocalltheclass.
3.定义一个长方形Rectangle类,它有宽、高(Width,Height)。
在说明该类的对象时,若给定了两个参数,分别设为宽和高;若只给了一个参数,则宽和高相同(即正方形)。
同时编写可以显示面积的成员函数。
编写相应的调试用函数,来测试该类的正确性。
2、实验仪器、设备或软件
机房电脑
二、实验过程(实验步骤、记录、数据、分析)
1.
#include
classRectangle
{
public:
voidInit(intWidth,intHeight)
{
X=Width;
Y=Height;
}
intSetWidth(inta)
{
X=a;
returnX;
}
intSetHeight(intb)
{
Y=b;
returnY;
}
intGetWidth(){returnX;}
intGetHeight(){returnY;}
intArea(){returnX*Y;}
voidDispInfo()
{
cout<<"Rectangle'sWidth:
"< cout<<"Rectangle'sHeight: "< cout<<"Rectangle'sArea: "< } private: intX,Y; }; voidmain(){ Rectanglea; a.SetWidth(10); a.SetHeight(20); a.DispInfo(); intx,y; cin>>x>>y; a.Init(x,y); cout< } 2. #include #definepai3.14 classCircle { public: voidInit(intX) { Panj=X; } intSetPanj(inta) { Panj=a; returnPanj; } intGetPanj(){returnPanj;} doubleArea(){returnpai*Panj*Panj;} doubleZhouz(){return2*pai*Panj;} voidDispInfo() { cout<<"Circle'sWidth: "< cout<<"Circle'sArea: "< cout<<"Circlee'sZhouz: "<<2*pai*Panj< } private: intPanj; }; voidmain() { Circlea; intx; a.SetPanj(10); a.DispInfo(); cin>>x; a.Init(x); a.DispInfo(); } 3. #include classRectangle { public: voidInit1(intX,intY) { Width=X; Height=Y; } voidInit1(intX) { Width=X; Height=X; } intSetWidth(inta) { Width=a; returnWidth; } intSetHeight(intb) { Width=b; returnWidth; } intGetWidth(){returnWidth;} intGetHeight(){returnHeight;} intArea(){returnWidth*Height;} voidDispInfo() { cout<<"Rectangle'sWidth: "< cout<<"Rectangle'sHeight: "< cout<<"Rectangle'sArea: "< } private: intWidth,Height; }; voidmain() { Rectanglea; a.Init1(10,20); a.DispInfo(); a.Init1(10); a.DispInfo(); } 三、结论 1、实验结果 (1). (2). (3). 2、分析讨论 (1).class里private的X,Y应定义为私有成员,public里的Width,heigth,a,b应定义为公有成员。 Init(),SetWidth(),SetHeight(),GetWidth(),GetHeight(),Area(),DispInfo()这些函数应定义为公有的,使其能够被主函数调用。 (2).编写该函数应先定义pai的值,其次在写求面积和周长的函数时,返回值类型最好使用double型。 (3).该题主要是要进行函数重载来解决只输入一个值的情况。 四、指导教师评语及成绩: 评语: 成绩: 指导教师签名: 批阅日期:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验报告 实验 报告