《C++实验资料报告材料九》由会员分享,可在线阅读,更多相关《C++实验资料报告材料九(14页珍藏版)》请在金锄头文库上搜索。
1、wordVC+程序设计实验报告 实验九题目:继承与派生专 业学 生 姓 名班 级 学 号指 导 教 师指 导 单 位 日 期教师评语 教师签名:年月日成绩评定备 注一、实验目的1. 理解类的继承概念,能够定义和使用类的继承关系2掌握派生类的声明与定义方法3. 掌握公有、私有和保护派生的访问特性二、实验容1. 设计一个大学的类系统,学校有学生、教师、职员,每种人员都有自己的特性,他们之间又有一样的地方。利用继承机制定义这个系统中的各个类与类上的必须操作。2. 假定车可分为货车、客车又可分为轿车、面包车和公共汽车。请设计相应的类层次结构并加以实现。三、实验结果与其结果分析1.(1)源程序:#inc
2、lude #include class Personprotected:char m_strName10;int m_nSex;int m_nAge;public:Person(char *name,int age,char sex)strcpy(m_strName, name);m_nSex= (sex=m?0:1 );m_nAge = age;void setName(char *name)strcpy(m_strName, name);void setSex(int sex)m_nSex= (sex=m?0:1 );void setAge(int age)m_nAge = age;cha
3、r * getName()return m_strName;int getAge()return m_nAge;int getSex()return m_nSex;void ShowMe()cout 姓 名:m_strNameendl;cout 性 别:(m_nSex=0?男:女)endl;cout 年 龄:m_nAgeendl;class Teacher : public Personchar m_strDept20;int m_fSalary;public:Teacher(char *name,int age,char sex,char *dept,int salary):Person(n
4、ame,age,sex) strcpy(m_strDept, dept); m_fSalary = salary;void ShowMe() Person:ShowMe();cout 工作单位:m_strDeptendl;cout 月 薪:m_fSalaryendl;void setSalary(int salary)m_fSalary = salary;int getSalary()return m_fSalary;class Student : public Personchar m_strID12;char m_strClass12;public:Student(char *name,i
5、nt age,char sex,char *ID,char *Class):Person(name,age,sex)strcpy(m_strID, ID); strcpy(m_strClass, Class);void ShowMe()cout 学 号:m_strIDendl;Person:ShowMe();cout 班 级:m_strClassn;void setID(char * ID)strcpy(m_strID, ID);void setClass(char *Class) strcpy(m_strClass, Class);char* getID()return m_strID;ch
6、ar* getClass()return m_strClass;class Employee:public Personint m_fSalary;public:Employee(char *name,int age,char sex,int salary):Person(name,age,sex)m_fSalary = salary;void setSalary(int salary)m_fSalary = salary;int getSalary()return m_fSalary;void ShowMe()Person:ShowMe();cout 工 资:m_fSalaryn;void
7、main()Teacher teacher1(周明,38,m,计算机系,3800);Student std1(王芳,20,f,03016003,计算机03);Employee emPloyee1(鑫,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;
8、cout修改各类人员的属性后:endl;teacher1.ShowMe();cout-endl;std1.ShowMe();cout-endl;emPloyee1.ShowMe();2实验结果:2.(1)源程序:#includeclass vehicle/ 定义基类vehiclepublic: / 公有函数成员 vehicle(int in_wheels,float in_weight); / 给数据成员初始化 int get_wheels(); / 获取车轮数 float get_weight(); / 获取汽车重量 void setWeels(int wls); void setWeigh
9、t(float wt); void display() cout车轮数:wheels 汽车重量:weightendl; private: / 私有数据成员 int wheels; / 车轮数 float weight; / 表示汽车承重;vehicle:vehicle(int in_wheels,float in_weight)wheels = in_wheels;weight = in_weight;float vehicle:get_weight()return weight;int vehicle:get_wheels()return wheels;void vehicle:setWee
10、ls(int wls)wheels = wls;void vehicle:setWeight(float wt)weight = wt;class truck:public vehicle / 定义货车类truckprivate: / 新增私有数据成员 float weight_load; / 承重public: / 新增公有成员函数 truck(int wheel,float wt,float wl):vehicle(wheel,wt) weight_load = wl; float getLoads() return weight_load; void display() vehicle:
11、display(); cout汽车承重weight_loadendl; ;/车和客车,客车又可分为轿车、面包车和公共汽车class car:public vehicle/ 定义客车类car int passenger_load; / 新增私有数据成员,表示载客数public: / 新增公有成员函数car(int in_wheels,float in_weight,int people=4):vehicle(in_wheels,in_weight)passenger_load = people;int getPassengers()return passenger_load;void setPassengers(int people)passenger_load = people;void display() vehicle:display(); cout载客数:passenger_loadendl;void main()truck truck1(8,400,100000);/ 货车car car1(4,20);/ 客车car saloon_car(4,10,5);/ 轿车car microbus(6,10,18);/ 面包车car bus(6,20,30);/ 公共汽车/ 显示相关信息truck1.display();cout-