实验5类和对象题目.docx
- 文档编号:2365040
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:15
- 大小:17.86KB
实验5类和对象题目.docx
《实验5类和对象题目.docx》由会员分享,可在线阅读,更多相关《实验5类和对象题目.docx(15页珍藏版)》请在冰豆网上搜索。
实验5类和对象题目
实验5类和对象
程序填空
1.题目描述:
仔细阅读下列求两个点之间距离的程序,程序的输出结果是50,根据程序的输出结果在划线处填入正确语句。
代码:
#include
#include
usingnamespacestd;
classpoint
{
public:
point(floata,floatb){x=a;y=b;}
floatDistance(point&p)
{
floatdx=p.x-x;
floatdy=p.y-y;
return(float)sqrt(dx*dx+dy*dy);
}
private:
floatx,y;
};
intmain()
{
pointp1(2,3),p2(32,43);
cout< return0; } 2.题目描述: 设计一个矩阵类CRectangle,该类中的私有成员变量存放Rectangle的长和宽,并设置它们的默认值为1,通过成员函数set()来设定长和宽的值,并确保长宽都在(0,50)范围之内,求其周长Perimeter并显示输出。 以下是完成此项工作的程序,请将未完成的部分填入,使之完整。 代码: #include usingnamespacestd; classCRectangle { public: voidSet(floata,floatb) { if((a>0)&&(a<50))length=a; elselength=1; if((b>0)&&(b<50))width=b; elsewidth=1; } floatperimeter() { return2*(length+width); } private: floatlength; floatwidth; }; intmain() { CRectangleR; floatl,w;//定义矩形的长和宽做为输入变量; //cout<<"请输入矩形的长和宽: "< cin>>l>>w; R.Set(1,w);//设置矩形的长和宽 cout<<"矩形的周长为: "< return0; } 3.题目描述: 设计一个类CRectangle,要求如下所述。 (1)定义两个点坐标x1,y1,x2,y2,两点所确定的一条直线构成了矩形的对角线。 (2)初始化矩形的两个点时,判断给定的两个点是否能够构成一个矩形,如果不能构成矩形,则矩形对角线的两点初始化为(0,0)和(1,1)。 如果可以构成,则用形参初始化对象的数据成员。 根据以上描述完成下列程序。 代码: #include #include usingnamespacestd; classCRectangle { public: CRectangle(floatRx1=0,floatRy1=0,floatRx2=1,floatRy2=1); boolIsSquare(); voidPrintRectangle(); private: //确定直线的两点的坐标 floatx1,y1,x2,y2; }; CRectangle: : CRectangle(floatRx1,floatRy1,floatRx2,floatRy2) { if(Rx1==Rx2||Ry1==Ry2)//两点的横坐标或纵坐标的值相等,则不能构成矩形 { x1=y1=0; x2=y2=1; cout<<"不能构成矩形! "< } else { x1=Rx1,x2=Rx2,y1=Ry1,y2=Ry2//初始化数据成员x1,y1,x2,y2 cout<<"可以构成矩形! "< } } intmain() { CRectangleR1(1,3,5,6); CRectangleR2(1,3,1,6); return0; } 4.题目描述: 下列程序中声明了类girl,其中函数“display”是类girl的友元函数,请在 (1)、 (2)和(3)处各填入正确的内容,使程序能正常运行。 代码: #include usingnamespacestd; classgirl { private: charname; intage; public: girl(charn,intd)//构造函数 { name=n; age=d; } Friendvoiddisplay(girl&x);//声明友元函数 }; voiddisplay(girl&x)//类外定义友元函数 { cout<<"Girl'snameis: "< "< //girl类的友元函数能访问girl类对象的私有成员 } intmain() { girle('z',18); display(e);//调用友元函数 return0; } 5.题目描述: ,请完善下面程序,使程序的运行结果如下: Thisisaconstructor! Thisisaconstructor! Thevalueofchisa Thevalueofchisb Thisisadestructorofb Thisisadestructorofa 代码: #include usingnamespacestd; classMyClass { charch; public: MyClass() { cout<<"Thisisaconstructor! "< ch='a'; } MyClass(charcharacter) { cout<<"Thisisaconstructor! "< ch=character; } voidPrint() { cout<<"Thevalueofchis"< } ~MyClass() { cout<<"Thisisadestructorof"< } }; intmain() { MyClassfirst,second(b); first.Print(); second.Print(); return0; } 程序设计 6.题目标题: 计算两点间的距离 题目描述: 仿照本次实验预习的程序填空题1,将以上Distance函数定义为类piont的友元函数,实现程序的功能。 并在主函数中增加输入两点作为友元函数的实参。 其主函数如下: 输入描述: 输入四个数,用空格隔开。 输出描述: 两个点的距离。 样例输入: 1356 样例输出: 5 #include #include usingnamespacestd; classpoint { public: point(floata,floatb){x=a;y=b;} friendfloatDistance(point&p1,point&p2); private: floatx,y; }; floatDistance(point&p1,point&p2) { floatdx=p1.x-p2.x; floatdy=p1.y-p2.y; return(float)sqrt(dx*dx+dy*dy); } intmain() { floatp1_x,p1_y,p2_x,p2_y; //输入四个点 cin>>p1_x>>p1_y>>p2_x>>p2_y; pointp1(p1_x,p1_y),p2(p2_x,p2_y); cout< return0; } 7.题目标题: 日期类CDateInfo的设计。 题目描述: 根据以下主函数的功能来设计日期类CDateInfo,使其能正确运行。 类CDateInfo中应该具有描述年、月、日的三个数据成员和相应的成员函数。 #include usingnamespacestd; classCDateibfo { intday,month,year; public: CDateibfo(); CDateibfo(intyy,intmm,intdd); voidsetdate(intyy,intmm,intdd); voidgetdate(); }; CDateibfo: : CDateibfo() { day=10; month=10; year=2011; } CDateibfo: : CDateibfo(intyy,intmm,intdd) { year=yy; month=mm; day=dd; } voidCDateibfo: : setdate(intyy,intmm,intdd) { year=yy; month=mm; day=dd; } voidCDateibfo: : getdate() { cout< } intmain() { CDateibfodate1,date2(2011,10,10); inty,m,d; cin>>y>>m>>d; date1.setdate(y,m,d); date1.getdate(); date2.getdate(); return0; } 输入描述: 三个整数,分别用来设置对象data1的年、月、日 输出描述: 两行: 第1行为对象data1的年月日;第2行为data2的年月日。 样例输入: 2011125 样例输出: 2012-12-5 2011-10-10 8.题目标题: 学生类Student的设计 题目描述: 根据以下主函数的功能来设计日期类Student,使其能正确运行。 类Student中应该具有描述学生姓名、性别、年龄的三个数据成员和相应的成员函数。 输入描述: 3行,第一行为一个长度不超过10的字符串表示姓名;第二行为0和1中的一个整数;第三行为一个整数,表示年龄。 输出描述: 按主函数要求输出。 #include #include usingnamespacestd; classStudent { private: charname[20]; intsex; unsignedold; public: voidSetName(char*chOne); voidSetGender(intisex); voidSetAge(unsignediold); vo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 对象 题目
![提示](https://static.bdocx.com/images/bang_tan.gif)