C实验二复杂形式对象文档格式.docx
- 文档编号:4815007
- 上传时间:2023-05-04
- 格式:DOCX
- 页数:20
- 大小:24.92KB
C实验二复杂形式对象文档格式.docx
《C实验二复杂形式对象文档格式.docx》由会员分享,可在线阅读,更多相关《C实验二复杂形式对象文档格式.docx(20页珍藏版)》请在冰点文库上搜索。
voidsetMinute(int);
//设置minute(确保数据在合理范围)
voidsetSecond(int);
//设置second(确保数据在合理范围)
//getfunctions
intgetHour();
//返回hour
intgetMinute();
//返回minute
intgetSecond();
//返回second
voidprintUniversal();
//按24小时格式输出时间:
23:
56:
12
voidprintStandard();
//按12小时格式输出时间:
11:
12(PM)或9:
55(AM)
private:
inthour;
//0-23(24小时格式)
intminute;
//0-59
intsecond;
};
//Timel类定义结束
(1)按照注释的要求完成所有成员函数的定义;
(2)在主程序中定义Time类对象,通过对象指针或引用调用上述成员函数以测试其正确性。
代码
#include<
iostream>
usingnamespacestd;
classTime
public:
Time(int=0,int=0,int=0);
voidsetTime(inth,intm,ints);
voidsetHour(int);
voidsetMinute(int);
voidsetSecond(int);
intgetHour();
intgetMinute();
intgetSecond();
boolcheck_n();
private:
inthour;
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)
setHour(inth)
setMinute(intm)
setSecond(ints)
intTime:
getHour()
returnhour;
getMinute()
returnminute;
getSecond()
returnsecond;
printUniversal()
cout<
<
hour<
'
minute<
second<
endl;
printStandard()
if(hour<
=12)
cout<
"
AM"
else
hour-12<
PM"
intmain()
Timet(12,0,0);
intx,y,z;
t.printStandard();
t.printUniversal();
t.setTime(13,59,34);
t.setHour(14);
t.setMinute
(2);
t.setSecond(23);
Pleaseinput######.\n"
;
cin>
>
x;
y;
z;
t.setTime(x,y,z);
while(t.check_n())
{
Negative!
!
\n"
Pleaseinputthetime(######).\n"
cin>
t.setTime(x,y,z);
}
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
Location(double,double);
//构造函数
doubleGetx();
//成员函数,取x坐标的值
doubleGety();
//成员函数,取y坐标的值
doubledistance1(Location&
);
//成员函数,求两坐标点之间的距离
frienddoubledistance2(Location&
Location&
//友元函数,求两坐标点之间的距离
doublex,y;
代码:
#include<
cmath>
classLocation
doublesetx(double);
doublesety(double);
doublefrienddistance2(Location&
Location&
doublex;
doubley;
Location:
Location(doublex1,doubley1)
x=x1;
y=y1;
doubleLocation:
setx(doublex1)
returnx;
sety(doubley1)
returny;
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));
Locationa(-1.0,-1.0),b(-1.0,1.0);
调用成员函数输出结果:
distance1="
a.distance1(b)<
调用友元函数输出结果:
distance2="
distance2(a,b)<
3.
使用C++的string类,将5个字符串按逆转后的顺序显示出来。
例如,逆转前的5个字符串是:
GermanyJapanAmericaBritainFrance
逆转后的顺序输出字符串是:
FranceBritainAmericaJapanGermany
string>
strings[5];
for(inti=0;
i<
5;
i++)
//cout<
s[i];
for(inti=4;
i>
=0;
i--)
s[i]<
"
4.
设计一个模仿类magic来实现4*4的魔方阵。
类的框架如下:
classmagic//声明魔方阵类magic
voidgetdata();
//输入初值成员函数
voidsetfirstmagic();
//设置初始魔方成员函数
intgenerate();
//生成最终魔方成员函数
intprintmagic();
//显示魔方成员函数
intm[4][4];
intstep;
intfirst;
intsumj;
所谓4*4的魔方阵,是指该矩阵的各行值的和等于各列值的和,并等于两对角线值的和,如:
,其各行、各列以及对角线值的和都是64。
求4*4的魔方阵的一般步骤提示如下:
(1)设置初始魔方的起始值和相邻元素之间的差值。
例如上述魔方阵的初始魔方的起始值和相邻元素之间的差值分别为:
first=1,step=2
(2)设置初始魔方元素的值。
例如上述魔方的初始魔方阵为:
(3)生成最终魔方阵。
方法如下:
求最大元素和最小元素的和sum,本例为1+31=32。
用32减去初始魔方阵所有对角线上元素的值,然后将结果放在原来的位置,即可求得最终的魔方阵。
本例最终魔方阵为:
(4)编写主程序,通过对象指针或引用调用上述成员函数以测试所设计的魔方类的正确性.
classMagic
voidgetdata();
voidsetfirstmagic();
intgenerate();
intprintmagic();
intm[4][4];
intstep;
intfirst;
intsumj;
voidMagic:
getdata()
Pleaseinputthefirst>
first;
Pleaseinputthestep>
step;
setfirstmagic()
4;
for(intj=0;
j<
j++)
m[i][j]=first+(i*4+j)*step;
sumj=m[0][0]+m[3][3];
intMagic:
generate()
i+=3)
j+=3)
m[i][j]=sumj-m[i][j];
for(inti=1;
3;
for(intj=1;
printmagic()
cout<
m[i][j]<
'
Magicm;
m.getdata();
m.setfirstmagic();
m.generate();
m.printmagic();
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类
Employee(long,char*,char*,double);
//构造函数
~Employee();
//析构函数
//setfunctions
voidset_id(long);
//设置id
voidset_salary(double);
//设置salary
voidset_name(char*);
//设置name
voidset_address(char*);
//设置adress
longget_id();
//返回id
doubleget_salary();
//返回salary
char*get_name();
//返回name
char*get_addressSecond();
//返回address
voidprint();
//打印输出Employee的相关信息
longid;
//工号
char*name;
//姓名
char*address;
//家庭住址
doublesalary;
//月薪
//Employee类声明结束
cstring>
classEmployee
Employee(long,char*,char*,double);
~Employee();
voidset_id(long);
voidset_salary(double);
longget_id();
char*get_addressSecond();
voidprint();
longid;
char*name;
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()
delete[]name;
delete[]address;
voidEmployee:
set_id(longid0)
}
set_salary(doublesalary0)
}
set_name(char*name0)
}
set_address(char*address0)
longEmployee:
get_id()
returnid;
doubleEmployee:
get_salary()
returnsalary;
char*Employee:
get_name()
returnname;
get_addressSecond()
returnaddress;
print()
id<
name<
address<
salary<
Employeee(1,"
liziye"
"
qianan"
100000.00);
e.print();
6.
声明一个Student类,在该类中包括一个数据成员:
score(代表课程成绩)、两个静态数据成员:
total_score(代表总分),count(代表学生总人数)。
构造函数、析构函数、account(int)用于设置分数、静态成员函数sum()返回所有学生的总成绩、静态成员函数average()返回所有学生的平均成绩、print()用于打印当前学生对象的成绩等。
在主程序中,输入某班同学的成绩,并调用上述函数打印全班同学的成绩、求出全班学生的成绩之和与平均分等。
Student类的框架可参考:
classStudent//声明Student类
Student(int);
~Student();
voidaccount(int);
//设置分数
staticintsum();
//返回总成绩
staticdoubleaverage();
//返回平均成绩
//打印输出Student的相关信息
intscore;
//分数
staticinttotal_score;
//总分
staticintcount;
//总人数
//Student类声明结束
classStudent
Student(int=0);
intid;
Student:
Student(ints)
id=count;
score=s;
total_score=total_score+s;
count++;
~Student(){}
intStudent:
sum()
returntotal_score;
doubleStudent:
average()
returndouble(total_score)/count;
count=0;
total_score=0;
voidStudent:
account(ints)
total_score=total_score-score;
score<
intm
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 复杂 形式 对象