C实验二复杂形式对象.docx
- 文档编号:12102812
- 上传时间:2023-04-17
- 格式:DOCX
- 页数:20
- 大小:24.92KB
C实验二复杂形式对象.docx
《C实验二复杂形式对象.docx》由会员分享,可在线阅读,更多相关《C实验二复杂形式对象.docx(20页珍藏版)》请在冰豆网上搜索。
C实验二复杂形式对象
天津理工大学实验报告
学院(系)名称:
计算机与通信工程学院
姓名
学号
专业
计算机科学与技术
班级
教学二班
实验项目
实验二复杂形式的对象
课程名称
高级程序设计语言II
课程代码
0667026
实验时间
2016年10月14日第7、8节
实验地点
计算机软件实验室7-215
批改意见
成绩
教师签字:
实验目的:
(1)理解类和对象的概念;
(2)掌握类与对象的定义方法;
(3)理解类的成员的访问控制的含义,公有和私有成员的区别;
(4)掌握构造函数和析构函数的含义与作用、定义方式和实现;
(5)能够根据给定的要求定义类并实现类的成员函数;
(6)掌握string类的使用方法
(7)了解C++面向对象程序设计的基本思想、基本方法和基本步骤;
(8)掌握MSVisualC++6.0或DEVC++调试C++程序的基本方法、基本步骤。
实验内容:
1.Time类的框架定义如下:
classTime//声明Time类
{
public:
Time(int=0,int=0,int=0);//带默认参数的构造函数
//setfunctions
voidsetTime(int,int,int);//设置hour,minute,second
voidsetHour(int);//设置hour(确保数据在合理范围)
voidsetMinute(int);//设置minute(确保数据在合理范围)
voidsetSecond(int);//设置second(确保数据在合理范围)
//getfunctions
intgetHour();//返回hour
intgetMinute();//返回minute
intgetSecond();//返回second
voidprintUniversal();//按24小时格式输出时间:
23:
56:
12
voidprintStandard();//按12小时格式输出时间:
11:
56:
12(PM)或9:
23:
55(AM)
private:
inthour;//0-23(24小时格式)
intminute;//0-59
intsecond;//0-59
};//Timel类定义结束
(1)按照注释的要求完成所有成员函数的定义;
(2)在主程序中定义Time类对象,通过对象指针或引用调用上述成员函数以测试其正确性。
代码
#include
usingnamespacestd;
classTime
{
public:
Time(int=0,int=0,int=0);
voidsetTime(inth,intm,ints);
voidsetHour(int);
voidsetMinute(int);
voidsetSecond(int);
intgetHour();
intgetMinute();
intgetSecond();
voidprintUniversal();
voidprintStandard();
boolcheck_n();
private:
inthour;
intminute;
intsecond;
};
boolTime:
:
check_n()
{
if(hour>24||hour<0||minute>59||minute<0||second>59||second<0)
returntrue;
returnfalse;
}
Time:
:
Time(inth,intm,ints)
{
hour=h;
minute=m;
second=s;
}
voidTime:
:
setTime(inth=0,intm=0,ints=0)
{
hour=h;
minute=m;
second=s;
}
voidTime:
:
setHour(inth)
{
hour=h;
}
voidTime:
:
setMinute(intm)
{
minute=m;
}
voidTime:
:
setSecond(ints)
{
second=s;
}
intTime:
:
getHour()
{
returnhour;
}
intTime:
:
getMinute()
{
returnminute;
}
intTime:
:
getSecond()
{
returnsecond;
}
voidTime:
:
printUniversal()
{
cout< '< '< } voidTime: : printStandard() { if(hour<=12) cout< '< '< else cout< '< '< } intmain() { Timet(12,0,0); intx,y,z; t.printStandard(); t.printUniversal(); t.setTime(13,59,34); t.printStandard(); t.printUniversal(); t.setHour(14); t.setMinute (2); t.setSecond(23); t.printStandard(); t.printUniversal(); cout<<"Pleaseinput######.\n"; cin>>x; cin>>y; cin>>z; t.setTime(x,y,z); while(t.check_n()) { cout<<"Negative! ! ! \n"; cout<<"Pleaseinputthetime(######).\n"; cin>>x; cin>>y; cin>>z; t.setTime(x,y,z); } t.printStandard(); t.printUniversal(); return0; } 结果: 设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果: A(x1,y1),B(x2,y2) Distance1=d1 Distance2=d2 其中: x1,x2,y1,y2为指定的坐标值,d1和d2为两个坐标点之间的距离。 Location类框架可参考如下: classLocation//声明类Location { public: Location(double,double);//构造函数 doubleGetx();//成员函数,取x坐标的值 doubleGety();//成员函数,取y坐标的值 doubledistance1(Location&);//成员函数,求两坐标点之间的距离 frienddoubledistance2(Location&,Location&);//友元函数,求两坐标点之间的距离 private: doublex,y; }; 代码: #include usingnamespacestd; #include classLocation { public: Location(double,double); doublesetx(double); doublesety(double); doubledistance1(Location&); doublefrienddistance2(Location&,Location&); private: doublex; doubley; }; Location: : Location(doublex1,doubley1) { x=x1; y=y1; } doubleLocation: : setx(doublex1) { x=x1; returnx; } doubleLocation: : sety(doubley1) { y=y1; returny; } doubleLocation: : distance1(Location&p) { returnsqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); } doubledistance2(Location&p1,Location&p2) { returnsqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } intmain() { Locationa(-1.0,-1.0),b(-1.0,1.0); cout<<"调用成员函数输出结果: "< cout<<"distance1="< cout<<"调用友元函数输出结果: "< cout<<"distance2="< return0; } 结果: 3. 使用C++的string类,将5个字符串按逆转后的顺序显示出来。 例如,逆转前的5个字符串是: GermanyJapanAmericaBritainFrance 逆转后的顺序输出字符串是: FranceBritainAmericaJapanGermany #include #include usingnamespacestd; intmain() { strings[5]; for(inti=0;i<5;i++) { //cout<<">"; cin>>s[i]; } for(inti=4;i>=0;i--) { cout< } return0; } 4. 设计一个模仿类magic来实现4*4的魔方阵。 类的框架如下: classmagic//声明魔方阵类magic { public: voidgetdata();//输入初值成员函数 voidsetfirstmagic();//设置初始魔方成员函数 intgenerate();//生成最终魔方成员函数 intprintmagic();//显示魔方成员函数 private: intm[4][4]; intstep; intfirst; intsumj; }; 所谓4*4的魔方阵,是指该矩阵的各行值的和等于各列值的和,并等于两对角线值的和,如: ,其各行、各列以及对角线值的和都是64。 求4*4的魔方阵的一般步骤提示如下: (1)设置初始魔方的起始值和相邻元素之间的差值。 例如上述魔方阵的初始魔方的起始值和相邻元素之间的差值分别为: first=1,step=2 (2)设置初始魔方元素的值。 例如上述魔方的初始魔方阵为: (3)生成最终魔方阵。 方法如下: 求最大元素和最小元素的和sum,本例为1+31=32。 用32减去初始魔方阵所有对角线上元素的值,然后将结果放在原来的位置,即可求得最终的魔方阵。 本例最终魔方阵为: (4)编写主程序,通过对象指针或引用调用上述成员函数以测试所设计的魔方类的正确性. #include usingnamespacestd; classMagic { public: voidgetdata(); voidsetfirstmagic(); intgenerate(); intprintmagic(); private: intm[4][4]; intstep; intfirst; intsumj; }; voidMagic: : getdata() { cout<<"Pleaseinputthefirst>"; cin>>first; cout<<"Pleaseinputthestep>"; cin>>step; } voidMagic: : setfirstmagic() { for(inti=0;i<4;i++) for(intj=0;j<4;j++) m[i][j]=first+(i*4+j)*step; sumj=m[0][0]+m[3][3]; } intMagic: : generate() { for(inti=0;i<4;i+=3) for(intj=0;j<4;j+=3) m[i][j]=sumj-m[i][j]; for(inti=1;i<3;i++) for(intj=1;j<3;j++) m[i][j]=sumj-m[i][j]; return0; } intMagic: : printmagic() { for(inti=0;i<4;i++) { for(intj=0;j<4;j++) cout< cout< } return0; } intmain() { Magicm; m.getdata(); m.setfirstmagic(); m.generate(); m.printmagic(); return0; } 5. 声明一个Employee类,数据成员包括: 一个long型的id,两个字符指针成员,代表职员的姓名和家庭住址,一个double数据,代表月薪。 成员函数有: 构造函数、析构函数、set_salary()和get_salary()代表修改和获取薪资、set_name()和get_name()代表修改和获取姓名、set_address()和get_address()代表修改和获取家庭住址,set_id()和get_id()用来修改获取当前职员的id号。 在主程序中,创建对象数组调用上述函数以测试Employee类设计的正确性。 Employee类的框架可参考: classEmployee//声明Employee类 { public: Employee(long,char*,char*,double);//构造函数 ~Employee();//析构函数 //setfunctions voidset_id(long);//设置id voidset_salary(double);//设置salary voidset_name(char*);//设置name voidset_address(char*);//设置adress //getfunctions longget_id();//返回id doubleget_salary();//返回salary char*get_name();//返回name char*get_addressSecond();//返回address voidprint();//打印输出Employee的相关信息 private: longid;//工号 char*name;//姓名 char*address;//家庭住址 doublesalary;//月薪 };//Employee类声明结束 #include #include usingnamespacestd; classEmployee { public: Employee(long,char*,char*,double); ~Employee(); voidset_id(long); voidset_salary(double); voidset_name(char*); voidset_address(char*); longget_id(); doubleget_salary(); char*get_name(); char*get_addressSecond(); voidprint(); private: longid; char*name; char*address; doublesalary; }; Employee: : Employee(longid0,char*name0,char*address0,doublesalary0) { id=id0; name=newchar[strlen(name0)+1]; strcpy(name,name0); address=newchar[strlen(address0)+1]; strcpy(address,address0); salary=salary0; } Employee: : ~Employee() { delete[]name; delete[]address; } voidEmployee: : set_id(longid0) { id=id0; } voidEmployee: : set_salary(doublesalary0) { salary=salary0; } voidEmployee: : set_name(char*name0) { name=newchar[strlen(name0)+1]; strcpy(name,name0); } voidEmployee: : set_address(char*address0) { address=newchar[strlen(address0)+1]; strcpy(address,address0); } longEmployee: : get_id() { returnid; } doubleEmployee: : get_salary() { returnsalary; } char*Employee: : get_name() { returnname; } char*Employee: : get_addressSecond() { returnaddress; } voidEmployee: : print() { cout< } intmain() { Employeee(1,"liziye","qianan",100000.00); e.print(); return0; } 6. 声明一个Student类,在该类中包括一个数据成员: score(代表课程成绩)、两个静态数据成员: total_score(代表总分),count(代表学生总人数)。 成员函数有: 构造函数、析构函数、account(int)用于设置分数、静态成员函数sum()返回所有学生的总成绩、静态成员函数average()返回所有学生的平均成绩、print()用于打印当前学生对象的成绩等。 在主程序中,输入某班同学的成绩,并调用上述函数打印全班同学的成绩、求出全班学生的成绩之和与平均分等。 Student类的框架可参考: classStudent//声明Student类 { public: Student(int);//构造函数 ~Student();//析构函数 voidaccount(int);//设置分数 staticintsum();//返回总成绩 staticdoubleaverage();//返回平均成绩 voidprint();//打印输出Student的相关信息 private: intscore;//分数 staticinttotal_score;//总分 staticintcount;//总人数 };//Student类声明结束 #include usingnamespacestd; classStudent { public: Student(int=0); ~Student(); voidaccount(int); staticintsum(); staticdoubleaverage(); voidprint(); private: intid; intscore; staticinttotal_score; staticintcount; }; Student: : Student(ints) { id=count; score=s; total_score=total_score+s; count++; } Student: : ~Student(){} intStudent: : sum() { returntotal_score; } doubleStudent: : average() { returndouble(total_score)/count; } intStudent: : count=0; intStudent: : total_score=0; voidStudent: : account(ints) { total_score=total_score-score; score=s; total_score=total_score+s; } voidStudent: : print() { cout< } intm
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 复杂 形式 对象
![提示](https://static.bdocx.com/images/bang_tan.gif)