




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
9. 假定车可分为货车和客车,客车又可分为轿车、面包车和公共汽车。请设计相应的类层次结构。说明:可以把轿车、面包车和公共汽车定义为客车类的对象参考程序:#includeusing namespace std;class vehicle/ 定义基类vehiclepublic: / 公有函数成员 vehicle(int in_wheels,float in_weight); / 给数据成员初始化 int get_wheels(); / 获取车轮数 float get_weight(); / 获取汽车重量 void setWeels(int wls); void setWeight(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:setWeels(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: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-endl;car1.display();cout-endl;saloon_car.display();cout-endl;microbus.display();cout-endl;bus.display();程序的运行结果:车轮数:8 汽车重量:400汽车承重100000-车轮数:4 汽车重量:20载客数:4-车轮数:4 汽车重量:10载客数:5-车轮数:6 汽车重量:10载客数:18-车轮数:6 汽车重量:20载客数:3010.设计一个能细分为矩形、三角形、圆形和椭圆形的“图形”类。使用继承将这些图形分类,找出能作为基类部分的共同特征(如宽、高、中心点等)和方法(如初始化、求面积等),并看看这些图形是否能进一步划分为子类。参考程序:#includeusing namespace std;class Figure/ 定义基类图形类public:/ 公有函数成员 Figure(int wid)width = wid; float area() int getWidth()return width;private: / 私有数据成员 int width; / 宽度或半径;class Rectangle:public Figure / 定义矩形类int height;public:Rectangle(int wid, int hei):Figure(wid)height = hei; float area()return getWidth() * height;class Triangle:public Figure / 定义三角形类int height;public:Triangle(int wid, int hei):Figure(wid)height = hei;float area()return 1.0/2* getWidth() * height;class Circle:public Figure / 定义圆类public:Circle(int wid):Figure(wid)float area()return 3.14 * getWidth() * getWidth();void main()Rectangle rect(5,4);Triangle tri(5,4);Circle cir(5);cout矩形的面积是:rect.area()endl三角形的面积是:tri.area()endl圆的面积是:cir.area()endl;程序的运行结果为:矩形的面积是:20三角形的面积是:10圆的面积是:78.511.考虑大学的学生情况,试利用单继承来实现学生和毕业生两个类,设计相关的数据成员及函数,编程对继承情况进行测试。参考程序:#include#includeclass Student/ 定义基类vehiclepublic: / 公有函数成员Student(int n,char *na,int g):number(n),grade(g)strcpy(name,na);int getNumber()return number;char * getName()return name;int getGrade()return grade;void display()cout学号:numberendl姓名:nameendl年级:gradeendl;private: / 私有数据成员 int number; / 学号 char name20;/ 姓名 int grade;/ 年级;class Graduate:public Student /定义毕业生类char designSubject20;public:Graduate(int n,char *na,char * deSub,int g=4):Student(n,na,g) strcpy(designSubject,deSub);void display()Student:display();cout设计题目:designSubjectendl;void main()/创建对象Student li(2,Li Ming,3);Graduate zhang(3,Zhang Gang,学生成绩管理系统);/ 显示对象的相关信息li.display();cout endl- endl;zhang.display();程序的运行结果:学号:2姓名:Li Ming年级:3-学号:3姓名:Zhang Gang年级:4设计题目:学生成绩管理系统12. 定义一个哺乳动物类,再由此派生出人类、狗类和猫类,这些类中均有speak()函数,观察在调用过程中,到底使用了谁的speak()函数。参考程序:#include#includeclass Animalfloat weight;public:void speak()void setWeight(float wt)weight = wt;float getWeight()return weight;class Human:public Animalpublic:void speak()cout说话endl;class Cat:public Animalpublic:void speak()cout喵喵endl;class Dog:public Animalpublic:void speak()cout汪汪endl;void main()/ 定义三个对象Human hm;Cat cat;Dog dog;/ 调用不同类的speak函数cout人:;hm.speak();cout猫:;cat.speak();cout狗:;dog.speak();程序的运行结果:人:说话猫:喵喵狗:汪汪13. 通过多重继承定义研究生类,研究生既有学生的属性,又有教师的属性。参考程序:#include #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;char * 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(name,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;char * getDept()return m_strDept;int getSalary()return m_fSalary;class Student : public Personchar m_strID12;char m_strClass12;public:Student(char *name,int 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;char* getClass()return m_strClass;class Master:public Student, public Teacherpublic:Master(char *name,int age,char sex,char *ID,char *Class,char *dept,int salary):Student(name,age,sex,ID,Class),Teacher(name, age, sex, dept, salary)void ShowMe()Student:ShowMe();cout 工作单位:getDept()endl;cout 月 薪:getSalary()endl;void main()/ 定义三个不同类的对象Teacher teacher1(刘馨,38,m,计算机系,3800);Student std1(刘丽,2
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年闭式冷却塔合作协议书
- 2025年力与变形检测仪合作协议书
- 2025年离子风棒合作协议书
- 2025年静脉注射丙种球蛋白合作协议书
- 2025年氯氟氰菊酯项目合作计划书
- 2025年粉体无筛分离设备合作协议书
- 电商物流领域工作背景证明(7篇)
- 2025年新乡危险品考试模拟试题
- 商业合作补充条款协议
- 月度收入及年终奖金详细证明(8篇)
- CSC-300系列数字式发变组保护装置的调试说明
- 硫酸应急预案-硫酸泄漏应急预案演练总结
- 2025年中考英语复习1600词背诵单-按字母排序
- 线路安规培训
- 比亚迪秦EV新能源汽车电机驱动系统
- 大风天气下的物流运输安全措施
- 老旧小区加装电梯使用公约协议
- 新生儿护理安全用药
- 2025年湖北省新华书店集团有限公司招聘笔试参考题库含答案解析
- 西医骨科发展简史
- 医疗护理医学培训 临时起搏器的使用及参数调整课件
评论
0/150
提交评论