您好,欢迎访问三七文档
实验三学号:姓名:班级:软件工程141继承与派生(4学时)一、实验目的1.学习定义和使用类的继承关系,定义派生类。2.熟悉不同继承方式下对基类成员的访问控制。3.学习利用虚基类解决二义性问题。二、实验任务1.定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(intn)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。2.定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。3.分析并调试以下程序运行结果,并和实际运行结果相对照。#includeiostreamusingnamespacestd;classBase{intx;public:Base(inti){x=i;coutConstuctorofBaseendl;}~Base(){coutDestuctorofBaseendl;}voidshow(){coutx=xendl;}};classDerived:publicBase{Based;public:Derived(inti):Base(i),d(i){coutConstuctorofDerivedendl;}~Derived(){coutDestuctorofDerivedendl;}};intmain(){Derivedobj(5);obj.show();return0;}4.定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。从bicycle和motorcar派生出摩托车(motorcycle)类。在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle设置为虚基类,会有什么问题?编程试试看。5.设计people(人员)类,考虑到通用性,抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。派生出student(学生)类,添加属性:班号charclassNO[7];从people类派生出teacher(教师)类,添加属性:职务charprincipalship[11]、部门chardepartmentE21]。从student类中派生出graduate(研究生)类,添加属性:专业charsubject[21]、导师teacheradviser;从graduate类和teacher类派生出TA(助教生)类,注意虚基类的使用。重载相应的成员函数,测试这些类。三、实验步骤1.若将age设置为私有成员,在其派生类中直接给其赋值,会出现错误提示:将age设置为公有成员时,能正常运行代码:#includeiostreamusingnamespacestd;classAnimal{public:intage;};classdog:publicAnimal{public:intsetAge(intn){age=n;returnage;}};intmain(){dogd;coutd.setAge(3)endl;return0;}运行结果为:2.执行情况为:(1)基类BaseClass的构造函数;(2)派生类DerivedClass的构造函数;(3)派生类DerivedClass的析取函数;(4)基类BaseClass的析取函数;代码:#includeiostreamusingnamespacestd;classBaseClass{public:BaseClass(){Number=90;coutconstructorofBaseClass!endl;}~BaseClass(){coutdestructorofBaseClass!endl;}private:intNumber;};classDerivedClass:publicBaseClass{public:DerivedClass(){coutconstrctorofDerivedClass!endl;}~DerivedClass(){coutdestructorofDerivedClass!endl;}};intmain(){DerivedClassder;return0;}执行结果:3.分析:(1)执行Base的构造函数;(2)执行Derived的对象obj的Base类构造函数,并将obj(5)中参数5赋值给X及Derived类构造函数;(3)执行Derived类的对象obj的Derived的析取函数、在执行Derived类的obj的Base析取函数,最后执行与第一步相对应的Base的析取函数;执行结果:4.虚基类:使得在继承间接共同基类时只保留一份成员。若不使用虚基类,在最终的派生类中会保留该间接共同基类数据成员的多份同名成员。代码:#includeiostreamusingnamespacestd;classvehicle{public:vehicle(floatm,floatw){MaxSpeed=m;Weight=w;}~vehicle(){}voidrun(){coutrunningofvehicle!endl;}voidstop(){coutstoppingofvehicle!endl;}floatMaxSpeed;floatWeight;};classbicycle:virtualpublicvehicle{public:bicycle(floatm,floatw,floath):vehicle(m,w){Height=h;}~bicycle(){}floatHeight;};classmotorcar:virtualpublicvehicle{public:motorcar(floatm,floatw,ints):vehicle(m,w){SeatNum=s;}~motorcar(){}intSeatNum;};classmotorcycle:publicbicycle,publicmotorcar{public:motorcycle(floatm,floatw,floath,ints):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){}~motorcycle(){}voiddisplay(){coutMaxSpeed=MaxSpeedendl;coutWeight=Weightendl;coutHeight=Heightendl;coutSeatNum=SeatNumendl;}};voidmain(){floatm,w,h;ints;coutpleaseinput\nMaxSpeed=;cinm;coutWeight=;cinw;coutHeight=;cinh;coutSeatNum=;cins;motorcyclemoc(m,w,h,s);moc.display();}运行结果:5.代码:#includeiostream#includestringusingnamespacestd;classPeople{public:People(stringn,strings,stringb,stringi){number=n;sex=s;birthday=b;id=i;};~People(){}protected:stringnumber;stringsex;stringbirthday;stringid;};classStudent:virtualpublicPeople{public:Student(stringn,strings,stringb,stringi,stringc):People(n,s,b,i){classNo=c;}~Student(){}protected:stringclassNo;};classTeacher:virtualpublicPeople{public:Teacher(stringn,strings,stringb,stringi,stringp,stringd):People(n,s,b,i){principalship=p;department=d;}~Teacher(){}protected:stringprincipalship;//职务stringdepartment;//部门};classGraduate:publicStudent{public:Graduate(stringn,strings,stringb,stringi,stringc,stringsu,stringt):People(n,s,b,i),Student(n,s,b,i,c){subject=su;teacher_adviser=t;}~Graduate(){}protected:stringsubject;//专业stringteacher_adviser;//导师};classTA:publicGraduate,publicTeacher{public:TA(stringn,strings,stringb,stringi,stringc,stringsu,stringt,stringp,stringd):People(n,s,b,i),Graduate(n,s,b,i,c,su,t),Teacher(n,s,b,i,p,d){}~TA(){}voiddisplay(){cout编号:numberendl;cout性别:sexendl;cout生日:birthdayendl;cout身份证号:idendl;cout班级:classNoendl;cout专业:subjectendl;cout导师:teacher_adviserendl;cout导师职务:principalshipendl;cout导师部门:departmentendl;}};voidmain(){stringn,s,b,i,c,su,t,p,d;cout请输入编号:endl;cinn;cout请输入性别:endl;cins;cout请输入生日:endl;cinb;cout请输入身份证号:endl;cini;cout请输入班级:endl;cinc;cout请输入专业:endl;cinsu;cout请输入导师:endl;cint;cout请输入导师职务:endl;cinp;cout请输入导师部门:endl;cind;TAta(n,s,b,i,c,su,t,p,d);ta.display();}运行结果:四、实验总结:在本次操作中,前四个题操作较顺利,在做第五题时,调试改正了好久,(1)在多次派生后的初始化时,不懂得对基类进行初始化,导致编译错误;(2)在定义性别,身份证号等时,将其定义为字符型,导致输出时,全乱码,后将其改成字符串形式,输出正确;虽然在后面将其改成字符串形式输出正确,但还是不太清楚,若用字符型,怎样才能保证不乱码,字符串与字符数组,在什么情况下,如何选择更恰当,这是后面急需解决的问题。
本文标题:第三次上机报告
链接地址:https://www.777doc.com/doc-2119873 .html