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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

C实验二复杂形式对象.docx

1、C实验二复杂形式对象天津理工大学实验报告学院(系)名称:计算机与通信工程学院姓名学号专业计算机科学与技术班级教学 二 班实验项目实验二 复杂形式的对象课程名称高级程序设计语言II课程代码0667026实验时间2016年10月14日 第7、8节实验地点计算机软件实验室7-215批改意见成绩教师签字: 实验目的:(1)理解类和对象的概念;(2)掌握类与对象的定义方法;(3)理解类的成员的访问控制的含义,公有和私有成员的区别;(4)掌握构造函数和析构函数的含义与作用、定义方式和实现;(5)能够根据给定的要求定义类并实现类的成员函数; (6)掌握string类的使用方法(7)了解C+面向对象程序设计的

2、基本思想、基本方法和基本步骤;(8)掌握MS Visual C+6.0或DEV C+调试C+程序的基本方法、基本步骤。实验内容:1. Time类的框架定义如下:class Time /声明Time类public: Time( int = 0, int = 0, int = 0 ); / 带默认参数的构造函数 / set functions void setTime( int, int, int ); / 设置hour, minute, second void setHour( int ); / 设置hour (确保数据在合理范围) void setMinute( int ); / 设置minu

3、te (确保数据在合理范围) void setSecond( int ); / 设置second (确保数据在合理范围) / get functions int getHour(); / 返回 hour int getMinute(); / 返回 minute int getSecond(); / 返回 second void printUniversal(); / 按24小时格式输出时间:23:56:12 void printStandard(); / 按12小时格式输出时间:11:56:12 (PM) 或 9:23:55(AM)private: int hour; / 0 - 23 (24

4、小时格式) int minute; / 0 - 59 int second; / 0 - 59; / Timel类定义结束(1) 按照注释的要求完成所有成员函数的定义;(2) 在主程序中定义Time类对象,通过对象指针或引用调用上述成员函数以测试其正确性。代码#include using namespace std;class Time public: Time( int =0 , int =0 , int =0 ); void setTime( int h, int m, int s ); void setHour( int ); void setMinute( int ); void se

5、tSecond( int ); int getHour(); int getMinute(); int getSecond(); void printUniversal(); void printStandard(); bool check_n(); private: int hour; int minute; int second; bool Time:check_n() if (hour24 | hour59 | minute 59|second0) return true; return false;Time:Time(int h,int m,int s) hour=h; minute=

6、m; second=s;void Time:setTime(int h=0,int m=0,int s=0) hour=h; minute=m; second=s;void Time:setHour(int h) hour=h;void Time:setMinute(int m) minute=m;void Time:setSecond(int s) second=s;int Time:getHour() return hour;int Time:getMinute() return minute;int Time:getSecond() return second;void Time:pri

7、ntUniversal() couthour:minute:secondendl;void Time:printStandard() if(hour=12) couthour:minute:secondAMendl; else couthour-12:minute:secondPMendl;int main() Time t(12,0,0); int x,y,z; t.printStandard(); t.printUniversal(); t.setTime(13,59,34); t.printStandard(); t.printUniversal(); t.setHour(14); t.

8、setMinute(2); t.setSecond(23); t.printStandard(); t.printUniversal(); coutx; ciny; cinz; t.setTime(x,y,z); while(t.check_n() coutNegative!n; coutx; ciny; cinz; t.setTime(x,y,z); t.printStandard(); t.printUniversal(); return 0; 结果: 设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标在第3象限,B的坐标点在第2

9、象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:A(x1, y1), B(x2, y2)Distance1=d1Distance2=d2其中:x1, x2, y1, y2为指定的坐标值,d1和d2为两个坐标点之间的距离。Location类框架可参考如下:class Location /声明类Locationpublic: Location(double,double); /构造函数 double Getx(); /成员函数,取x坐标的值 double Gety(); /成员函数,取y坐标的值 double distance1(Location&);/成员函

10、数,求两坐标点之间的距离 friend double distance2(Location&, Location&);/友元函数,求两坐标点之间的距离private: double x,y;代码:#includeusing namespace std;#includeclass Locationpublic: Location(double,double); double setx(double); double sety(double); double distance1(Location&); double friend distance2(Location&,Location&);pri

11、vate: double x; double y;Location:Location(double x1,double y1) x=x1; y=y1;double Location:setx(double x1) x=x1; return x;double Location:sety(double y1) y=y1; return y;double Location:distance1(Location& p) return sqrt(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y);double distance2(Location& p1,Location& p2) retur

12、n sqrt(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);int main() Location a(-1.0,-1.0),b(-1.0,1.0); cout调用成员函数输出结果:endl; coutdistance1=a.distance1(b)endl; cout调用友元函数输出结果:endl; coutdistance2=distance2(a,b)endl; return 0;结果:3. 使用C+的string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前的5个字符串是:Germany Japan America Britain

13、France 逆转后的顺序输出字符串是:France Britain America Japan Germany#include #include using namespace std;int main() string s5; for(int i=0;i5;i+) / cout; cinsi; for(int i=4;i=0;i-) coutsi ; return 0;4.设计一个模仿类magic来实现4*4的魔方阵。类的框架如下:class magic /声明魔方阵类magicpublic: void getdata(); /输入初值成员函数 void setfirstmagic();

14、/设置初始魔方成员函数 int generate(); /生成最终魔方成员函数 int printmagic(); /显示魔方成员函数private: int m44; int step; int first; int sumj;所谓4*4的魔方阵,是指该矩阵的各行值的和等于各列值的和,并等于两对角线值的和,如:,其各行、各列以及对角线值的和都是64。求4*4的魔方阵的一般步骤提示如下:(1) 设置初始魔方的起始值和相邻元素之间的差值。例如上述魔方阵的初始魔方的起始值和相邻元素之间的差值分别为:first=1, step=2(2) 设置初始魔方元素的值。例如上述魔方的初始魔方阵为: (3) 生

15、成最终魔方阵。方法如下:求最大元素和最小元素的和sum,本例为1+31=32。用32减去初始魔方阵所有对角线上元素的值,然后将结果放在原来的位置,即可求得最终的魔方阵。本例最终魔方阵为:(4) 编写主程序,通过对象指针或引用调用上述成员函数以测试所设计的魔方类的正确性.#include using namespace std;class Magic public: void getdata(); void setfirstmagic(); int generate(); int printmagic(); private: int m44; int step; int first; int s

16、umj;void Magic:getdata() cout; cinfirst; cout; cinstep;void Magic:setfirstmagic() for(int i=0;i4;i+) for(int j=0;j4;j+) mij=first+(i*4+j)*step; sumj=m00+m33;int Magic:generate() for(int i=0;i4;i+=3) for(int j=0;j4;j+=3) mij=sumj-mij; for(int i=1;i3;i+) for(int j=1;j3;j+) mij=sumj-mij; return 0;int M

17、agic:printmagic() for(int i=0;i4;i+) for(int j=0;j4;j+) coutmij ; coutendl; return 0;int main() Magic m; m.getdata(); m.setfirstmagic(); m.generate(); m.printmagic(); return 0;5. 声明一个Employee类,数据成员包括:一个long型的id,两个字符指针成员,代表职员的姓名和家庭住址,一个double数据,代表月薪。成员函数有:构造函数、析构函数、set_salary()和get_salary()代表修改和获取薪资、

18、set_name()和get_name()代表修改和获取姓名、set_address()和get_address()代表修改和获取家庭住址,set_id()和get_id()用来修改获取当前职员的id号。在主程序中,创建对象数组调用上述函数以测试Employee类设计的正确性。Employee类的框架可参考:class Employee /声明Employee类public: Employee(long, char*, char*, double ); / 构造函数 Employee(); / 析构函数 / set functions void set_id(long); / 设置id voi

19、d set_salary(double); / 设置salary void set_name(char * ); / 设置name void set_address(char* ); / 设置adress / get functions long get_id(); / 返回id double get_salary(); / 返回 salary char* get_name(); / 返回 name char* get_addressSecond(); / 返回 address void print(); / 打印输出Employee的相关信息 private: long id; / 工号 c

20、har *name; / 姓名 char *address; / 家庭住址 double salary; / 月薪; / Employee类声明结束#include #include using namespace std;class Employee public: Employee(long, char*, char*, double ); Employee(); void set_id(long); void set_salary(double); void set_name(char * ); void set_address(char* ); long get_id(); doubl

21、e get_salary(); char* get_name(); char* get_addressSecond(); void print(); private: long id; char *name; char *address; double salary; ;Employee:Employee(long id0,char* name0,char *address0,double salary0 ) id=id0; name=new charstrlen(name0)+1; strcpy(name,name0); address=new charstrlen(address0)+1;

22、 strcpy(address,address0); salary=salary0;Employee:Employee() delete name; delete address;void Employee:set_id(long id0) id=id0; void Employee:set_salary(double salary0) salary=salary0; void Employee:set_name(char *name0 ) name=new charstrlen(name0)+1; strcpy(name,name0); void Employee:set_address(c

23、har* address0) address=new charstrlen(address0)+1; strcpy(address,address0); long Employee:get_id() return id; double Employee:get_salary() return salary; char* Employee:get_name() return name; char* Employee:get_addressSecond() return address;void Employee:print() coutid name address salaryendl;int

24、 main() Employee e(1,liziye,qianan,100000.00); e.print(); return 0;6.声明一个Student类,在该类中包括一个数据成员:score(代表课程成绩)、两个静态数据成员:total_score(代表总分),count(代表学生总人数)。成员函数有:构造函数、析构函数、account(int)用于设置分数、静态成员函数sum()返回所有学生的总成绩、静态成员函数average()返回所有学生的平均成绩、print()用于打印当前学生对象的成绩等。在主程序中,输入某班同学的成绩,并调用上述函数打印全班同学的成绩、求出全班学生的成绩之

25、和与平均分等。Student类的框架可参考:class Student /声明Student类public: Student(int ); / 构造函数 Student(); / 析构函数 void account(int); / 设置分数 static int sum(); / 返回总成绩 static double average(); / 返回平均成绩 void print(); / 打印输出Student的相关信息 private: int score; / 分数 static int total_score; / 总分 static int count; / 总人数; / Stude

26、nt类声明结束#include using namespace std;class Student public: Student(int =0 ); Student(); void account(int); static int sum(); static double average(); void print();private: int id; int score; static int total_score; static int count; ;Student:Student(int s) id=count; score=s; total_score=total_score+s

27、; count+;Student:Student()int Student:sum() return total_score;double Student:average() return double(total_score)/count;int Student:count=0;int Student:total_score=0;void Student:account(int s) total_score=total_score-score; score=s; total_score=total_score+s;void Student:print() coutid scoreendl;int m

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

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