西安交大C程序设计作业Word文件下载.docx
《西安交大C程序设计作业Word文件下载.docx》由会员分享,可在线阅读,更多相关《西安交大C程序设计作业Word文件下载.docx(30页珍藏版)》请在冰豆网上搜索。
![西安交大C程序设计作业Word文件下载.docx](https://file1.bdocx.com/fileroot1/2022-12/7/0787cbbd-9038-4ba8-aa77-d643cf029a79/0787cbbd-9038-4ba8-aa77-d643cf029a791.gif)
<
"
姓名:
Name<
\t性别:
Sex<
\t年龄:
Age<
endl;
};
classTeacher:
publicPerson
char*zhuanye;
char*zhicheng;
char*course;
Teacher();
Teacher(char*,char,int,char*,char*,char*);
voidRegister(char*,char,int,char*,char*,char*);
~Teacher();
voidprint();
Teacher:
:
Teacher()
Person:
Register("
罗先觉"
'
61);
strcpy(zhuanye,"
电气工程及其自动化"
);
strcpy(zhicheng,"
教授"
strcpy(course,"
电路"
}
Teacher(char*name,charsex,intage,char*zy,char*zc,char*cor)
Register(name,sex,age,zy,zc,cor);
voidTeacher:
Register(char*name,charsex,intage,char*zy,char*zc,char*cor)
Register(name,sex,age);
zhuanye=newchar[strlen(zy)+1];
strcpy(zhuanye,zy);
zhicheng=newchar[strlen(zc)+1];
strcpy(zhicheng,zc);
course=newchar[strlen(cor)+1];
strcpy(course,cor);
~Teacher()
delete[]zhuanye;
delete[]zhicheng;
delete[]course;
print()
\t专业"
zhuanye<
\t职称:
zhicheng<
\t主讲课程:
course<
intmain()
Personperson;
person.Register("
18);
person.print();
Teacherteacher("
lll"
56,"
math"
"
jiaoshou"
gaoshu"
teacher.print();
teacher.Register("
某某"
58,"
数学"
高等数学"
return0;
2.实验结果:
(二)第二题:
第二题:
许多研究生既有学生的属性,又有教师的属性。
试通过多重继承说明一个研究生类。
//许多研究生既有学生的属性,又有教师的属性。
charName[10];
//姓名
//年龄
//性别
strcpy(Name,"
人某某"
Age=34;
m'
Person(char*name,intage,charsex)
Registerper(name,age,sex);
voidRegisterper(char*name,intage,charsex)//设置数据成员
{
Age=age;
Sex=(sex=='
?
'
'
f'
voidprintper()//输出数据成员
Sex;
}
classStudent:
charClassname[20];
charNumber[20];
charZhuanye[20];
Student()
Registerper("
学生某某"
18,'
strcpy(Classname,"
钱学森"
strcpy(Number,"
2130405000"
strcpy(Zhuanye,"
电气"
Student(char*name,intage,charsex,char*classname,char*number,char*zhuanye)
Registerstu(name,age,sex,classname,number,zhuanye);
voidRegisterstu(char*name,intage,charsex,char*classname,char*number,char*zhuanye)
strcpy(Classname,classname);
strcpy(Number,number);
strcpy(Zhuanye,zhuanye);
voidprintstu()
printper();
\t班级:
Classname<
\t学号:
Number<
\t专业:
Zhuanye;
charZhicheng[20];
charKeti[20];
charKecheng[20];
Teacher()
老师某某"
45,'
strcpy(Zhicheng,"
strcpy(Keti,"
新型计算机开发"
strcpy(Kecheng,"
计算机基础"
Teacher(char*name,intage,charsex,char*zhicheng,char*keti,char*kecheng)
Registertea(name,age,sex,zhicheng,keti,kecheng);
voidRegistertea(char*name,intage,charsex,char*zhicheng,char*keti,char*kecheng)
strcpy(Zhicheng,zhicheng);
strcpy(Keti,keti);
strcpy(Kecheng,kecheng);
voidprinttea()
Zhicheng<
\t课题:
Keti<
\t课程:
Kecheng;
classGraduate:
publicStudent,publicTeacher
Graduate()
Registerstu("
研究生某某"
23,'
"
电气某班"
1130506060"
电力系统研究"
Graduate(char*name,intage,charsex,char*classname,char*number,char*zhuanye,char*keti,char*kecheng)
Registergra(name,age,sex,classname,number,zhuanye,keti,kecheng);
voidRegistergra(char*name,intage,charsex,char*classname,char*number,char*zhuanye,char*keti,char*kecheng)
voidprintgra()
printstu();
cout<
显示结果:
\t"
person.printper();
person.Registerper("
王某某"
34,'
\n初始化后显示结果:
\n"
Studentstudent;
\n显示结果:
student.printstu();
student.Registerstu("
张某某"
qianxuesheng66"
1234500034"
dianqigongcheng"
Teacherteacher;
\n显示结果:
teacher.printtea();
teacher.Registertea("
刘某某"
56,'
陶艺历史研究"
中国传统文化"
Graduategraduate;
graduate.printgra();
graduate.Registergra("
勒某某"
23,'
21班"
1120303030"
高压输电系统"
信号与系统"
2.实验结果:
(三)第三题:
修改例9-5,从Point类中派生出一个Line类。
Line类增加一个数据成员EndPoint计算线的长度。
试比较一下与直接使用Point类来构造Line类的不同之处。
(用了三个文件)
//Point.h文件Point类
#ifndefPOINT_H
#definePOINT_H
classPoint//Point类中定义内容:
x,y为坐标;
setpoint为录入坐标函数;
//GetX,GetY是为了将x,y这两个私有成员能够用于line类而定义的,print为输出函数。
intx,y;
Point()
x=0;
y=0;
Point(inta,intb)
SetPoint(a,b);
voidSetPoint(inta,intb)
x=a;
y=b;
voidPrint()
['
x<
y<
]'
intGetX()
returnx;
intGetY()
returny;
#endif
//line.h文件line类
#ifndefCIRCLE_H
#defineCIRCLE_H
cmath>
#include"
point.h"
classLine:
publicPoint
doublelength;
Pointpoint1,point2;
Line()
point1.SetPoint(0,0);
point2.SetPoint(0,0);
Line(inta,intb,intc,intd)
SetLine(a,b,c,d);
voidSetLine(inta,intb,intc,intd)
point1.SetPoint(a,b);
point2.SetPoint(c,d);
voidEndPoint()
intx1=point1.GetX(),y1=point1.GetY(),x2=point2.GetX(),y2=point2.GetY();
length=sqrt((x1-x2)*(x1-x2)*1.0+(y1-y2)*(y1-y2)*1.0);
length;
//main文件执行测试
line.h"
Pointp(30,50);
Linel(12,34,56,78);
Pointp:
p.Print();
\nThelocationofLinel:
\tPoint1:
l.point1.Print();
\tPoint2:
l.point2.Print();
\nThelengthoflinel:
l.EndPoint();
l.Print();
四、第四题:
从Date类和Time类派生出一个DateAndTime类,修改相应的成员函数,当时间递增到新的一天时,能够修改日期值。
1.程序源代码:
classDate
intyear,month,day;
Date(inta,intb,intc)
year=a;
month=b;
day=c;
voidinit(intyy,intmm,intdd);
voidprint_ymd();
voidDate:
init(intyy,intmm,intdd)
{month=(mm>
=1&
&
mm<
=12)?
mm:
0;
year=(yy>
=1900&
yy<
=2100)?
yy:
1900;
day=(dd>
dd<
=31)?
dd:
1;
print_ymd()
{cout<
year<
-"
month<
day;
classTime
{
inthour,minute,second;
Time(inth,intm,ints)
hour=h,minute=m,second=s;
voidRegister(inth=0,intm=0,ints=0)
voidShowme_hms()
"
hour<
小时"
minute<
分钟"
second<
秒"
classDateAndTime:
publicDate,publicTime
DateAndTime(inta=1900,intb=1,intc=1,intd=0,inte=0,intf=0):
Date(a,b,c),Time(d,e,f){}
voidinput(intyy,intmm,intdd,inth,intm,ints)
month=mm;
year=yy;
day=dd;
voidNormal()
intn;
if(second>
=60)
n=second/60;
second=second%60;
minute=minute+n;
if(second<
0)
n=second/60+1;
second=second%60+60;
minute=minute-n;
if(minute>
n=minute/60;
minute=minute%60;
hour=hour+n;
if(minute<
0)
n=minute/60+1;
minute=minute%60+60;
hour=hour-n;
if(hour>
=24)
n=hour/24;
hour=hour%24;
day=day+n;
if(hour<
n=hour/24+1;
hour=hour%24+24;
day=day-n;
if(day>
=30)
n=day/30;
day=day%30;
month=month+n;
if(day<
n=day/30+1;
day=day%30+30;
month=month-n;
if(month>
=12)
n=month/12;
month=month%12;
year=year+n;
if(month<
n=month/12+1;
month=month%12+12;
year=year-n;
voidadd(DateAndTime&
d)
year=year+d.year;
month=month+d.month;
day=day+d.day;
hour=hour+d.hour;
minute=minute+d.minute;
second=second+d.second;
voidsub(DateAndTime&
year=year-d.year;
month=month-d.month;
day=day-d.day;
hour=hour-d.hour;
minute=minute-d.minute;
second=second-d.second;
voidshow()
Normal();
print_ymd();
Showme_hms();
voidmain()
DateAndTimed1,d2;
初始化:
d1.show();
d2.show();
d1.input(2014,5,23,22,4,50);
d2.input(0,0,0,6,58,29);
第一个时间:
第二个时间:
第一个时间第二个时间相加为:
d1.add(d2);
2.实验结果:
五、第五题:
采用继承方式定义一个三层生日蛋糕类,不论基类还是派生类都至少包含5个函数:
构造函数、设置数据成员函数、求蛋糕体积、运算符>
重载函数、输出生日祝词与蛋糕形状函数。
并编写主函数测试各成员函数。
继承方式如下:
首先定义圆柱体形状蛋糕类,只有3个数据成员,半径、高、生日祝词。
然后派生出圆柱体_方柱体蛋糕类,即在圆柱体形状上增加方柱体形状。
假定方柱体的正方形面积小于圆柱体的圆形面积,注意方柱体的高与正方形的边长不一定相等。
再用圆柱体