欢迎来到天天文库
浏览记录
ID:31798301
大小:72.28 KB
页数:6页
时间:2019-01-18
《实验四类的继承与派生》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验四、类的继承与派生09计算机1白杨09292100281、定义1个CPeople类,其属性(保护类型)有:姓名、性别和年龄。2、从CPeople类派生岀CStudent类,增加属性:学号、入学时间和入学成绩;3^从CPeople类派生LbCTeacher类,添加属性:职务、部门和工作吋I'可;4^由CStudent类派生出CGraduate类,添加属性:研究方向和导师;5、由CGraduate和CTeacher共同派生出在职研究生类CGradonWork6、对上述类分別定义其中的构造函数和输出函数,并在主函数中定义对象,进行测试,测试的代码大致如下:CGradonW
2、orkgradonWork("tzcerM,"男”,32,T5“,”2003”,335,“computer”,”tangmin“,”jsj”,“sxxyT2002-8“);gradonWork.Print();其中CGradonWork的构造函数参数分别对应的属性是一个在职的研究生对象,英姓名为tzcer,性别为男,32岁,学号15,2003年读在职,入学成绩335,就读计算机方向,其导师tangmin,同时tzcer也是sxxy部门,jsj专业的老师,2002-8月入职。要求记录输出信息并说明实验步骤和解决方法!7、问题:何为继承二义性,在本实验中哪个地方存在二义性,
3、你是如何解决的?继承的概念:派生类拥有基类的数据成员和成员函数。继承方式:公有继承、保护继承、私有继承。派主类对基类成员的访问形式:通过派生类对象直接访问基类成员,在派生类成员函数中直接访问基类成员,通过基类名字限定访问被重载的基类成员名。类对象成员的构造:先构造成员,再构造自身(调用构造函数)。派生类对象的构造:先构造基类,再构造成员,最后构造自身(调用构造函数)。基类构造顺序由派生层次决定:最远的基类最先构造。成员构造顺序和定义顺序相同。析构函数的析构顺序与构造相反。虚拟基类在派生类屮只存在一份拷贝,解决了基类数据成员的二义性问题。#include4、>usingnamespacestd;constintSIZE=1001;classCPeopleprotected:char*name;char*sex;intage;public:CPeople(){name=newchar[SIZE];sex=newchar[S!ZEJ;尸/〃与下面的构造函数重复下同CPeople(char*_name="Non,char*_sex="NoH,int_age=0){name=newchar[strlen(_name)+1];if(name)strcpy(name,_name);sex=newchar[strlen(_sex)+1]5、;if(sex)strcpy(sex,_sex);age=_age;}-CPeople(){delete[Iname;delete[]sex;}char*Get_name(){returnname;}char*Get_sex(){returnsex;}intGet_age(){returnage;}voidPrint(){cout«Hname:n«name«endl;cout«Msex:H«sex«endl;cout«nage:M«age«endl;}};classCStudent:publicvirtualCPeople〃虚拟继承CPeople为虚拟基类{protec6、ted:intstu_num;char*intime;intinscore;public://CStudent(){intime=newchar[SIZE];}CStudent(char*_name=”No蔦char*_sex=HNon,int_age=O,int_stu_num=O,_intime二”0000",int_inscore=0):CPeople(_name,_sex,_age){stu_num=_stu_num;intime=newchar[strlen(_intime)+l];if(intime)strcpy(intime,_intime);inscor7、e=_inscore;}〜CStudentO(delete[jinlime;}char*Get_intime(){returnintime;}intGet_stu_num(){returnstu_num;)intGet_inscore()(returninscore;}voidPrint(){CPeople::Print();cout«Hstudentnumber:°«stu_num«endl;cout«Henrolltime:n«intime«endl;cout«Henrollscore:n«inscore«endl;}};classC
4、>usingnamespacestd;constintSIZE=1001;classCPeopleprotected:char*name;char*sex;intage;public:CPeople(){name=newchar[SIZE];sex=newchar[S!ZEJ;尸/〃与下面的构造函数重复下同CPeople(char*_name="Non,char*_sex="NoH,int_age=0){name=newchar[strlen(_name)+1];if(name)strcpy(name,_name);sex=newchar[strlen(_sex)+1]
5、;if(sex)strcpy(sex,_sex);age=_age;}-CPeople(){delete[Iname;delete[]sex;}char*Get_name(){returnname;}char*Get_sex(){returnsex;}intGet_age(){returnage;}voidPrint(){cout«Hname:n«name«endl;cout«Msex:H«sex«endl;cout«nage:M«age«endl;}};classCStudent:publicvirtualCPeople〃虚拟继承CPeople为虚拟基类{protec
6、ted:intstu_num;char*intime;intinscore;public://CStudent(){intime=newchar[SIZE];}CStudent(char*_name=”No蔦char*_sex=HNon,int_age=O,int_stu_num=O,_intime二”0000",int_inscore=0):CPeople(_name,_sex,_age){stu_num=_stu_num;intime=newchar[strlen(_intime)+l];if(intime)strcpy(intime,_intime);inscor
7、e=_inscore;}〜CStudentO(delete[jinlime;}char*Get_intime(){returnintime;}intGet_stu_num(){returnstu_num;)intGet_inscore()(returninscore;}voidPrint(){CPeople::Print();cout«Hstudentnumber:°«stu_num«endl;cout«Henrolltime:n«intime«endl;cout«Henrollscore:n«inscore«endl;}};classC
此文档下载收益归作者所有