西安交大C++程序设计第九章作业.doc

上传人:wj 文档编号:4903140 上传时间:2023-05-07 格式:DOC 页数:24 大小:286KB
下载 相关 举报
西安交大C++程序设计第九章作业.doc_第1页
第1页 / 共24页
西安交大C++程序设计第九章作业.doc_第2页
第2页 / 共24页
西安交大C++程序设计第九章作业.doc_第3页
第3页 / 共24页
西安交大C++程序设计第九章作业.doc_第4页
第4页 / 共24页
西安交大C++程序设计第九章作业.doc_第5页
第5页 / 共24页
西安交大C++程序设计第九章作业.doc_第6页
第6页 / 共24页
西安交大C++程序设计第九章作业.doc_第7页
第7页 / 共24页
西安交大C++程序设计第九章作业.doc_第8页
第8页 / 共24页
西安交大C++程序设计第九章作业.doc_第9页
第9页 / 共24页
西安交大C++程序设计第九章作业.doc_第10页
第10页 / 共24页
西安交大C++程序设计第九章作业.doc_第11页
第11页 / 共24页
西安交大C++程序设计第九章作业.doc_第12页
第12页 / 共24页
西安交大C++程序设计第九章作业.doc_第13页
第13页 / 共24页
西安交大C++程序设计第九章作业.doc_第14页
第14页 / 共24页
西安交大C++程序设计第九章作业.doc_第15页
第15页 / 共24页
西安交大C++程序设计第九章作业.doc_第16页
第16页 / 共24页
西安交大C++程序设计第九章作业.doc_第17页
第17页 / 共24页
西安交大C++程序设计第九章作业.doc_第18页
第18页 / 共24页
西安交大C++程序设计第九章作业.doc_第19页
第19页 / 共24页
西安交大C++程序设计第九章作业.doc_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

西安交大C++程序设计第九章作业.doc

《西安交大C++程序设计第九章作业.doc》由会员分享,可在线阅读,更多相关《西安交大C++程序设计第九章作业.doc(24页珍藏版)》请在冰点文库上搜索。

西安交大C++程序设计第九章作业.doc

西安交通大学实验报告

课程__计算机程序设计__实验名称__继承__第1页共页

系别__________实验日期2014年5月日

专业班级_____组别_____________实验报告日期2014年月日

姓名_________学号__报告退发(订正、重做)

同组人_________________________________教师审批签字

一、实验目的

熟练继承的用法,进一步训练类的编程,并练习不同继承方式下对基类的成员的访问的控制。

二、实验内容

(一)第一题:

从类Person中派生出一个教师类,新增的属性有:

专业、职称和主讲课程(一门),并为这些属性定义相应的方法。

1.源程序代码:

#include

usingnamespacestd;

classPerson

{

protected:

char*Name;

charSex;

intAge;

public:

Person()

{

Name="csj";

Sex='M';

Age=18;

}

Person(char*name,charsex,intage)

{

Register(name,sex,age);

}

~Person()

{

delete[]Name;

}

voidRegister(char*name,charsex,intage)

{

intm=strlen(name);

Name=newchar[m+1];

strcpy(Name,name);

Sex=sex;;

Age=age;

}

voidprint()

{

cout<<"姓名:

"<

"<

"<

}

};

classTeacher:

publicPerson

{

protected:

char*zhuanye;

char*zhicheng;

char*course;

public:

Teacher();

Teacher(char*,char,int,char*,char*,char*);

voidRegister(char*,char,int,char*,char*,char*);

~Teacher();

voidprint();

};

Teacher:

:

Teacher()

{

Person:

:

Register("罗先觉",'M',61);

strcpy(zhuanye,"电气工程及其自动化");

strcpy(zhicheng,"教授");

strcpy(course,"电路");

}

Teacher:

:

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)

{

Person:

:

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:

:

~Teacher()

{

delete[]zhuanye;

delete[]zhicheng;

delete[]course;

}

voidTeacher:

:

print()

{

cout<<"姓名:

"<

"<

"<

"<

"<

}

intmain()

{

Personperson;

person.Register("csj",'M',18);

person.print();

Teacherteacher("lll",'M',56,"math","jiaoshou","gaoshu");

teacher.print();

teacher.Register("某某",'M',58,"数学","教授","高等数学");

teacher.print();

return0;

}

2.实验结果:

(二)第二题:

第二题:

许多研究生既有学生的属性,又有教师的属性。

试通过多重继承说明一个研究生类。

1.源程序代码:

//许多研究生既有学生的属性,又有教师的属性。

试通过多重继承说明一个研究生类。

#include

usingnamespacestd;

classPerson

{

protected:

charName[10]; //姓名

int Age; //年龄

charSex; //性别

public:

Person()

{

strcpy(Name,"人某某");

Age=34;

Sex='m';

}

Person(char*name,intage,charsex)

{

Registerper(name,age,sex);

}

voidRegisterper(char*name,intage,charsex)//设置数据成员

{

strcpy(Name,name);

Age=age;

Sex=(sex=='m'?

'm':

'f');

}

voidprintper() //输出数据成员

{

cout<<"姓名:

"<

"<

"<

}

};

classStudent:

publicPerson

{

protected:

charClassname[20];

charNumber[20];

charZhuanye[20];

public:

Student()

{

Registerper("学生某某",18,'f');

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)

{

Registerper(name,age,sex);

strcpy(Classname,classname);

strcpy(Number,number);

strcpy(Zhuanye,zhuanye);

}

voidprintstu()

{

printper();

cout<<"\t班级:

"<

"<

"<

}

};

classTeacher:

publicPerson

{

protected:

charZhicheng[20];

charKeti[20];

charKecheng[20];

public:

Teacher()

{

Registerper("老师某某",45,'m');

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)

{

Registerper(name,age,sex);

strcpy(Zhicheng,zhicheng);

strcpy(Keti,keti);

strcpy(Kecheng,kecheng);

}

voidprinttea()

{

printper();

cout<<"\t职称:

"<

"<

"<

}

};

classGraduate:

publicStudent,publicTeacher

{

public:

Graduate()

{

Registerstu("研究生某某",23,'f',"电气某班","1130506060","电气");

strcpy(Keti,"电力系统研究");

strcpy(Kecheng,"电路");

}

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)

{

Registerstu(name,age,sex,classname,number,zhuanye);

strcpy(Keti,keti);

strcpy(Zhuanye,zhuanye);

}

voidprintgra()

{

printstu();

cout<<"\t课题:

"<

"<

}

};

intmain()

{

Personperson;

cout<<"显示结果:

\t";

person.printper();

person.Registerper("王某某",34,'m');

cout<<"\n初始化后显示结果:

\n";

person.printper();

Studentstudent;

cout<<"\n显示结果:

\t";

student.printstu();

student.Registerstu("张某某",18,'m',"qianxuesheng66","1234500034","dianqigongcheng");

cout<<"\n初始化后显示结果:

\n";

student.printstu();

Teacherteacher;

cout<<"\n显示结果:

\t";

teacher.printtea();

teacher.Registertea("刘某某",56,'m',"教授","陶艺历史研究","中国传统文化");

cout<<"\n初始化后显示结果:

\n";

teacher.printtea();

Graduategraduate;

cout<<"\n显示结果:

\n";

graduate.printgra();

graduate.Registergra("勒某某",23,'f',"21班","1120303030","电气","高压输电系统","信号与系统");

cout<<"\n初始化后显示结果:

\n";

graduate.printgra();

cout<

return0;

}

2.实验结果:

(三)第三题:

修改例9-5,从Point类中派生出一个Line类。

Line类增加一个数据成员EndPoint计算线的长度。

试比较一下与直接使用Point类来构造Line类的不同之处。

1.源程序代码:

(用了三个文件)

//Point.h文件Point类

#ifndefPOINT_H

#definePOINT_H

usingnamespacestd;

classPoint//Point类中定义内容:

x,y为坐标;setpoint为录入坐标函数;

//GetX,GetY是为了将x,y这两个私有成员能够用于line类而定义的,print为输出函数。

{

intx,y;

public:

Point()

{

x=0;

y=0;

}

Point(inta,intb)

{

SetPoint(a,b);

}

voidSetPoint(inta,intb)

{

x=a;

y=b;

}

voidPrint()

{

cout<<'['<

}

intGetX()

{

returnx;

}

intGetY()

{

returny;

}

};

#endif

//line.h文件line类

#ifndefCIRCLE_H

#defineCIRCLE_H

#include

#include

usingnamespacestd;

#include"point.h"

classLine:

publicPoint

{

doublelength;

public:

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);

}

voidPrint()

{

cout<

}

};

#endif

//main文件执行测试

#include

#include"point.h"

#include"line.h"

usingnamespacestd;

intmain()

{

Pointp(30,50);

Linel(12,34,56,78);

cout<<"Pointp:

";

p.Print();

cout<<"\nThelocationofLinel:

";

cout<<"\tPoint1:

";

l.point1.Print();

cout<<"\tPoint2:

";

l.point2.Print();

cout<<"\nThelengthoflinel:

";

l.EndPoint();

l.Print();

cout<

return0;

}

2.实验结果:

四、第四题:

从Date类和Time类派生出一个DateAndTime类,修改相应的成员函数,当时间递增到新的一天时,能够修改日期值。

1.程序源代码:

#include

#include

usingnamespacestd;

classDate

{

protected:

intyear,month,day;

public:

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>=1&&dd<=31)?

dd:

1;

};

voidDate:

:

print_ymd()

{ cout<

classTime

{

protected:

inthour,minute,second;

public:

Time(inth,intm,ints)

{

hour=h,minute=m,second=s;

}

voidRegister(inth=0,intm=0,ints=0)

{

hour=h,minute=m,second=s;

}

voidShowme_hms()

{

cout<<""<

}

};

classDateAndTime:

publicDate,publicTime

{

public:

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)

{

hour=h,minute=m,second=s;

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>=60)

{

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<0)

{

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<0)

{

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<0)

{

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&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;

}

voidshow()

{

Normal();

print_ymd();

Showme_hms();

}

};

voidmain()

{

DateAndTimed1,d2;

cout<<"初始化:

\n";

d1.show();

d2.show();

d1.input(2014,5,23,22,4,50);

d2.input(0,0,0,6,58,29);

cout<<"第一个时间:

\n";

d1.show();

cout<<"第二个时间:

\n";

d2.sho

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2