实验四 继承与派生

上传人:第*** 文档编号:34242654 上传时间:2018-02-22 格式:DOC 页数:6 大小:73KB
返回 下载 相关 举报
实验四  继承与派生_第1页
第1页 / 共6页
实验四  继承与派生_第2页
第2页 / 共6页
实验四  继承与派生_第3页
第3页 / 共6页
实验四  继承与派生_第4页
第4页 / 共6页
实验四  继承与派生_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《实验四 继承与派生》由会员分享,可在线阅读,更多相关《实验四 继承与派生(6页珍藏版)》请在金锄头文库上搜索。

1、1实验四 继承与派生班级:软件 1002 姓名:韩晓燕 学号:201013138043 指导老师:汪老师【实验内容】设计一个用于人员管理的“people(人员) ”基类。考虑到通用性,仅只抽象出所有人员都具有的属性:编号、姓名、性别、出生日期、身份证号等;从 people 类派生出 student 类,并添加属性:班号 classno;从 people 类派生出 teacher 类,并添加属性:职务principalship、部门 department;从 student 类派生出 graduate 类,添加属性:专业 subject、导师 teacher adviser(teacher 类)

2、 ;从 graduate 类和 teacher 类派生出 TA 类。设计时注意虚基类的使用,注意重载相应的函数。测试这个类。【实验目的】1. 学习定义和使用类的继承关系,定义派生类;2. 熟悉不同继承方式下对虚基类成员的访问控制;3. 学习利用虚基类解决二义性问题。【UML 图】date-year: int-month: int-day: int+date(y: int, m: int, d: int)+show(): voidPeople-num: int-name: char *-ID: int-birth: date+People(no: int, na: char*, id: int,

3、 d: date&)+People(p: people &)People()+show(): voidstudent-classno: int+student(p: People &, n: int)+student(p: People &, s: student &)student()+show1(): voidteacher-PH: char*-DP: char*+People(p: People &, zh: char *, bu: char *)+People(p: People &, t: teacher &)teacher()+show2(): voidgraduate-sub:

4、char*-t: teacher+graduate(p: People &, s: student &, su: char *, a: teacher &)+graduate(p: people &, s: student &, g: graduate &)graduate()+show3(): voidTA+TA(p: People &, g: graduate &, t: teacher &)+TA(p: People &, g: graduate &, t: teacher, TA &)TA()+SHOW(): void2【程序清单】#includeusing namespace std

5、;class dateprivate:int year;int month;int day;public:date(int y,int m,int d):year(y),month(m),day(d)void show();void date:show()coutbirthday:;coutyear-month-dayendl;class Peoplepublic:People(int no,char *na,int id,date People(People People();void show();private:date birth;int num;char *name;int ID;v

6、oid People:show()coutnum:numendl;coutname:nameendl;birth.show();coutid:IDendl;3class student:virtual public Peoplepublic:student(People &p,int n):People(p),classno(n)student(People &p,student &s):People(p),classno(s.classno)student();void show1();private:int classno;void student:show1()show();coutcl

7、assno:classnoendl;class teacher:virtual public Peoplepublic:teacher(People &p,char *zh,char *bu):People(p),PH(zh),DP(bu)teacher(People &p,teacher &t):People(p),PH(t.PH),DP(t.DP)teacher();void show2();void show2(int);private:char *PH;char *DP;void teacher:show2()show();coutprincipalship:PHendl;coutde

8、partment:DPendl;void teacher:show2(int)coutprincipalship:PHendl;coutdepartment:DPendl;4class graduate: public studentpublic:graduate(People &p,student &s,char *su,teacher &a):People(p),student(s),sub(su),t(a)graduate(People &p,student &s,graduate &g):People(p),student(s),sub(g.sub),t(g.t)graduate();

9、void show3();private:char *sub;teacher t;void graduate:show3()show1();coutsubject: subendl;coutteacheradviser: endl;t.show2(0);class TA: public graduate, public teacherpublic:TA(People &p,graduate &g,teacher &t):People(p),graduate(g),teacher(t)TA(People &p,graduate &g,teacher &t,TA &):People(p),grad

10、uate(g),teacher(t)TA();void SHOW();void TA:SHOW()show3();int main()coutTHE INFORMATION OF PEOPLE:endl;date bir1(1990,1,2);People a(101,zhangsan,111,bir1);a.show ();coutendl;coutTHE INFORMATION OF STUDENT:endl;date bir2(1988,2,3);5People m(102,lisi,222,bir2);student b(m,1002);b.show1();coutendl;coutT

11、HE INFORMATION OF TEACHER:endl;date bir3(1986,3,5);People n(103,wangwu,333,bir3);teacher c(n,math,IT);c.show2();coutendl;coutTHE INFORMATION OF GRADUATE:endl;date bir4(1989,2,3);People t(104,fang,444,bir4);teacher tt(t,math,IT);graduate d(t,b,software,tt);d.show3();coutendl;coutTHE INFORMATION OF TA:endl;date bir5(1987,6,5);People f(105,mali,555,bir5);TA e(f,d,c) ;e.SHOW();coutendl;return 0;6【运行结果】【小结】1. 重载函数的使用使相应的同名函数实现不同的功能,从而是程序用一定的简化;2. 继承时将共同基类设置为虚继承,使从不同路径继承过来的同名数据成员在内存中就只有一个副本,同一个函数名也只有一个映射,解决了二义性问题。3. UML 图使相应的程序直观的表示出来。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号