ImageVerifierCode 换一换
格式:DOCX , 页数:15 ,大小:17.86KB ,
资源ID:2365040      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/2365040.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验5类和对象题目.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验5类和对象题目.docx

1、实验5类和对象题目实验5 类和对象程序填空1. 题目描述:仔细阅读下列求两个点之间距离的程序,程序的输出结果是50,根据程序的输出结果在划线处填入正确语句。代码:#include#includeusing namespace std;class pointpublic: point(float a,float b) x=a; y=b; float Distance(point &p) float dx=p.x-x; float dy=p.y-y; return (float)sqrt(dx*dx+dy*dy); private: float x,y;int main() point p1(2,

2、3),p2(32,43); coutp1.Distance(p2)endl; return 0;2. 题目描述:设计一个矩阵类CRectangle,该类中的私有成员变量存放Rectangle的长和宽,并设置它们的默认值为1,通过成员函数set()来设定长和宽的值,并确保长宽都在(0,50)范围之内,求其周长Perimeter并显示输出。以下是完成此项工作的程序,请将未完成的部分填入,使之完整。代码:#includeusing namespace std;class CRectanglepublic: void Set(float a,float b) if(a0)&(a0)&(b50) wid

3、th=b; else width=1; float perimeter() return 2*(length+width); private: float length; float width;int main() CRectangle R; float l,w;/定义矩形的长和宽做为输入变量; / cout请输入矩形的长和宽:lw; R.Set(1,w); /设置矩形的长和宽 cout矩形的周长为:R.perimeter()endl; return 0;3. 题目描述:设计一个类CRectangle,要求如下所述。(1)定义两个点坐标x1,y1,x2,y2,两点所确定的一条直线构成了矩形的

4、对角线。(2)初始化矩形的两个点时,判断给定的两个点是否能够构成一个矩形,如果不能构成矩形,则矩形对角线的两点初始化为(0,0)和(1,1)。如果可以构成,则用形参初始化对象的数据成员。根据以上描述完成下列程序。代码:#include#includeusing namespace std;class CRectanglepublic: CRectangle(float Rx1=0,float Ry1=0, float Rx2=1,float Ry2=1); bool IsSquare( ); void PrintRectangle( );private: /确定直线的两点的坐标 float x

5、1,y1,x2,y2;CRectangle:CRectangle(float Rx1 ,float Ry1, float Rx2,float Ry2) if (Rx1=Rx2|Ry1=Ry2) /两点的横坐标或纵坐标的值相等,则不能构成矩形 x1=y1=0; x2=y2=1; cout不能构成矩形! endl; else x1=Rx1,x2=Rx2,y1=Ry1,y2=Ry2 /初始化数据成员x1,y1,x2,y2 cout可以构成矩形! endl; int main() CRectangle R1(1,3,5,6); CRectangle R2(1,3,1,6); return 0;4. 题

6、目描述:下列程序中声明了类girl,其中函数“display”是类girl的友元函数,请在(1)、(2)和(3)处各填入正确的内容,使程序能正常运行。代码:#includeusing namespace std;class girlprivate: char name; int age;public: girl(char n, int d) /构造函数 name= n; age=d; Friend void display(girl &x); /声明友元函数;void display(girl &x) /类外定义 友元函数 coutGirls name is :x. name, age is

7、:x.ageendl; /girl类的友元函数能访问girl类对象的私有成员int main( ) girl e(z,18); display(e); /调用友元函数 return 0;5. 题目描述:,请完善下面程序,使程序的运行结果如下:This is a constructor ! This is a constructor ! The value of ch is a The value of ch is b This is a destructor of b This is a destructor of a 代码:#includeusing namespace std;class

8、MyClass char ch;public: MyClass( ) coutThis is a constructor! endl; ch=a; MyClass(char character ) coutThis is a constructor! endl; ch=character; void Print( ) coutThe value of ch is chendl; MyClass( ) coutThis is a destructor ofchendl; ;int main( ) MyClass first, second(b); first.Print( ); second.P

9、rint( ); return 0;程序设计6.题目标题:计算两点间的距离 题目描述:仿照本次实验预习的程序填空题1,将以上Distance函数定义为类piont的友元函数,实现程序的功能。并在主函数中增加输入两点作为友元函数的实参。其主函数如下:输入描述:输入四个数,用空格隔开。输出描述:两个点的距离。样例输入:1 3 5 6样例输出:5#include#includeusing namespace std;class pointpublic: point(float a,float b) x=a; y=b; friend float Distance( point &p1, point &

10、p2);private: float x,y;float Distance( point &p1, point &p2) float dx=p1.x-p2.x; float dy=p1.y-p2.y; return (float)sqrt(dx*dx+dy*dy);int main() float p1_x,p1_y,p2_x,p2_y; /输入四个点 cinp1_xp1_yp2_xp2_y; point p1(p1_x,p1_y),p2(p2_x,p2_y); coutDistance(p1,p2)endl; return 0;7.题目标题:日期类CDateInfo的设计。题目描述:根据以下

11、主函数的功能来设计日期类CDateInfo,使其能正确运行。类CDateInfo中应该具有描述年、月、日的三个数据成员和相应的成员函数。#includeusing namespace std;class CDateibfo int day,month,year;public: CDateibfo(); CDateibfo(int yy,int mm,int dd); void setdate(int yy,int mm,int dd); void getdate();CDateibfo:CDateibfo() day=10; month=10; year=2011;CDateibfo:CDat

12、eibfo(int yy,int mm,int dd) year=yy; month=mm; day=dd;void CDateibfo:setdate(int yy,int mm,int dd) year=yy; month=mm; day=dd;void CDateibfo:getdate() coutyear-month-dayymd; date1.setdate(y,m,d); date1.getdate(); date2.getdate(); return 0;输入描述: 三个整数,分别用来设置对象data1的年、月、日输出描述:两行:第1行为对象data1的年月日;第2行为data

13、2的年月日。样例输入:2011 12 5样例输出:2012-12-52011-10-108.题目标题:学生类Student的设计题目描述:根据以下主函数的功能来设计日期类Student,使其能正确运行。类Student中应该具有描述学生姓名、性别、年龄的三个数据成员和相应的成员函数。输入描述:3行,第一行为一个长度不超过10的字符串表示姓名;第二行为0和1中的一个整数;第三行为一个整数,表示年龄。输出描述: 按主函数要求输出。#include#includeusing namespace std;class Studentprivate: char name20; int sex; unsigned old;public: void SetName(char *chOne); void SetGender(int isex); void SetAge(unsigned iold); vo

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1