-VC++程序设计实验报告实验九题目:继承与派生专业学生姓名班级学号指导教师指导单位日期-教师评语教师签名:年月日成绩评定备注-一、实验目的1.理解类的继承概念,能够定义和使用类的继承关系2.掌握派生类的声明与定义方法3.掌握公有、私有和保护派生的访问特性二、实验内容1.设计一个大学的类系统,学校有学生、老师、职员,每种人员都有自己的特性,他们之间又有相同的地方。利用继承机制定义这个系统中的各个类及类上的必须操作。2.假定车可分为货车、客车又可分为轿车、面包车和公共汽车。请设计相应的类层次结构并加以实现。三、实验结果及其结果分析1.(1)源程序:#includeiostream.h#includestring.hclassPerson{protected:charm_strName[10];intm_nSex;intm_nAge;public:Person(char*name,intage,charsex){strcpy(m_strName,name);-m_nSex=(sex=='m'?0:1);m_nAge=age;}voidsetName(char*name){strcpy(m_strName,name);}voidsetSex(intsex){m_nSex=(sex=='m'?0:1);}voidsetAge(intage){m_nAge=age;}char*getName(){returnm_strName;}intgetAge(){returnm_nAge;}intgetSex(){returnm_nSex;}voidShowMe(){-cout姓名:m_strNameendl;cout性别:(m_nSex==0?男:女)endl;cout年龄:m_nAgeendl;}};classTeacher:publicPerson{charm_strDept[20];intm_fSalary;public:Teacher(char*name,intage,charsex,char*dept,intsalary):Person(name,age,sex){strcpy(m_strDept,dept);m_fSalary=salary;}voidShowMe(){Person::ShowMe();cout工作单位:m_strDeptendl;cout月薪:m_fSalaryendl;}voidsetSalary(intsalary){m_fSalary=salary;}-intgetSalary(){returnm_fSalary;}};classStudent:publicPerson{charm_strID[12];charm_strClass[12];public:Student(char*name,intage,charsex,char*ID,char*Class):Person(name,age,sex){strcpy(m_strID,ID);strcpy(m_strClass,Class);}voidShowMe(){cout学号:m_strIDendl;Person::ShowMe();cout班级:m_strClass\n;}voidsetID(char*ID){strcpy(m_strID,ID);}voidsetClass(char*Class){-strcpy(m_strClass,Class);}char*getID(){returnm_strID;}char*getClass(){returnm_strClass;}};classEmployee:publicPerson{intm_fSalary;public:Employee(char*name,intage,charsex,intsalary):Person(name,age,sex){m_fSalary=salary;}voidsetSalary(intsalary){m_fSalary=salary;}intgetSalary(){returnm_fSalary;}-voidShowMe(){Person::ShowMe();cout工资:m_fSalary\n;}};voidmain(){Teacherteacher1(周明,38,'m',计算机系,3800);Studentstd1(王芳,20,'f',03016003,计算机03);EmployeeemPloyee1(李鑫,25,'f',2000);teacher1.ShowMe();cout--------------------endl;std1.ShowMe();cout--------------------endl;emPloyee1.ShowMe();teacher1.setAge(40);teacher1.setSalary(4500);std1.setAge(21);emPloyee1.setAge(26);emPloyee1.setSalary(2000);cout--------------------endl;cout修改各类人员的属性后:endl;teacher1.ShowMe();-cout--------------------endl;std1.ShowMe();cout--------------------endl;emPloyee1.ShowMe();}(2)实验结果:-2.(1)源程序:#includeiostream.hclassvehicle//定义基类vehicle{public://公有函数成员vehicle(intin_wheels,floatin_weight);//给数据成员初始化intget_wheels();//获取车轮数floatget_weight();//获取汽车重量voidsetWeels(intwls);voidsetWeight(floatwt);voiddisplay(){cout车轮数:wheels汽车重量:weightendl;}private://私有数据成员intwheels;//车轮数floatweight;//表示汽车承重};vehicle::vehicle(intin_wheels,floatin_weight){wheels=in_wheels;weight=in_weight;}floatvehicle::get_weight(){-returnweight;}intvehicle::get_wheels(){returnwheels;}voidvehicle::setWeels(intwls){wheels=wls;}voidvehicle::setWeight(floatwt){weight=wt;}classtruck:publicvehicle//定义货车类truck{private://新增私有数据成员floatweight_load;//承重public://新增公有成员函数truck(intwheel,floatwt,floatwl):vehicle(wheel,wt){weight_load=wl;}floatgetLoads(){returnweight_load;-}voiddisplay(){vehicle::display();cout汽车承重weight_loadendl;}};//车和客车,客车又可分为轿车、面包车和公共汽车classcar:publicvehicle//定义客车类car{intpassenger_load;//新增私有数据成员,表示载客数public://新增公有成员函数car(intin_wheels,floatin_weight,intpeople=4):vehicle(in_wheels,in_weight){passenger_load=people;}intgetPassengers(){returnpassenger_load;}voidsetPassengers(intpeople){passenger_load=people;}-voiddisplay(){vehicle::display();cout载客数:passenger_loadendl;}};voidmain(){trucktruck1(8,400,100000);//货车carcar1(4,20);//客车carsaloon_car(4,10,5);//轿车carmicrobus(6,10,18);//面包车carbus(6,20,30);//公共汽车//显示相关信息truck1.display();cout---------------------endl;car1.display();cout---------------------endl;saloon_car.display();cout---------------------endl;microbus.display();cout---------------------endl;bus.display();}-(2)实验结果:四、实验收获与体会通过本次试验,我加深了对继承与派生的进一步理解。此次实验编写了有关类的继承与派生的两道程序,我更加了解了类的继承概念。此次试验题目感觉有点难度,我自己编写的时候不是那么顺利,总是出错。后来在网上搜了相关的题目研究了别人的方法,在自己的程序中找出了错误,修改了相关的继承调用,最终正确地调试出了程序。