第5章习题参考答案.docx
《第5章习题参考答案.docx》由会员分享,可在线阅读,更多相关《第5章习题参考答案.docx(16页珍藏版)》请在冰豆网上搜索。
第5章习题参考答案
9.假定车可分为货车和客车,客车又可分为轿车、面包车和公共汽车。
请设计相应的类层次结构。
说明:
可以把轿车、面包车和公共汽车定义为客车类的对象
参考程序:
#include
usingnamespacestd;
classvehicle//定义基类vehicle
{
public:
//公有函数成员
vehicle(intin_wheels,floatin_weight);//给数据成员初始化
intget_wheels();//获取车轮数
floatget_weight();//获取汽车重量
voidsetWeels(intwls);
voidsetWeight(floatwt);
voiddisplay(){
cout<<"车轮数:
"<<<"汽车重量:
"<}
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<<"汽车承重"<}
};
//车和客车,客车又可分为轿车、面包车和公共汽车
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<<"载客数:
"<}
};
voidmain(){
trucktruck1(8,400,100000);//货车
carcar1(4,20);//客车
carsaloon_car(4,10,5);//轿车
carmicrobus(6,10,18);//面包车
carbus(6,20,30);//公共汽车
//显示相关信息
truck1.display();
cout<<"---------------------"<car1.display();
cout<<"---------------------"<saloon_car.display();
cout<<"---------------------"<microbus.display();
cout<<"---------------------"<bus.display();
}
程序的运行结果:
车轮数:
8汽车重量:
400
汽车承重100000
---------------------
车轮数:
4汽车重量:
20
载客数:
4
---------------------
车轮数:
4汽车重量:
10
载客数:
5
---------------------
车轮数:
6汽车重量:
10
载客数:
18
---------------------
车轮数:
6汽车重量:
20
载客数:
30
10.设计一个能细分为矩形、三角形、圆形和椭圆形的“图形”类。
使用继承将这些图形分类,找出能作为基类部分的共同特征(如宽、高、中心点等)和方法(如初始化、求面积等),并看看这些图形是否能进一步划分为子类。
参考程序:
#include
usingnamespacestd;
classFigure//定义基类图形类
{
public:
//公有函数成员
Figure(intwid){width=wid;}
floatarea(){}
intgetWidth(){returnwidth;}
private:
//私有数据成员
intwidth;//宽度或半径
};
classRectangle:
publicFigure{//定义矩形类
intheight;
public:
Rectangle(intwid,inthei):
Figure(wid){
height=hei;
}
floatarea(){returngetWidth()*height;}
};
classTriangle:
publicFigure{//定义三角形类
intheight;
public:
Triangle(intwid,inthei):
Figure(wid){
height=hei;
}
floatarea(){return1.0/2*getWidth()*height;}
};
classCircle:
publicFigure{//定义圆类
public:
Circle(intwid):
Figure(wid){
}
floatarea(){return3.14*getWidth()*getWidth();}
};
voidmain(){
Rectanglerect(5,4);
Triangletri(5,4);
Circlecir(5);
cout<<"矩形的面积是:
"<<<"三角形的面积是:
"<<<"圆的面积是:
"<}
程序的运行结果为:
矩形的面积是:
20
三角形的面积是:
10
圆的面积是:
78.5
11.考虑大学的学生情况,试利用单继承来实现学生和毕业生两个类,设计相关的数据成员及函数,编程对继承情况进行测试。
参考程序:
#include
#include
classStudent//定义基类vehicle
{
public:
//公有函数成员
Student(intn,char*na,intg):
number(n),grade(g){
strcpy(name,na);
}
intgetNumber(){
returnnumber;
}
char*getName(){
returnname;
}
intgetGrade(){
returngrade;
}
voiddisplay(){
cout<<"学号:
"<<<"姓名:
"<<<"年级:
"<}
private:
//私有数据成员
intnumber;//学号
charname[20];//姓名
intgrade;//年级
};
classGraduate:
publicStudent{//定义毕业生类
chardesignSubject[20];
public:
Graduate(intn,char*na,char*deSub,intg=4):
Student(n,na,g)
{strcpy(designSubject,deSub);}
voiddisplay(){
Student:
:
display();
cout<<"设计题目:
"<}
};
voidmain(){
//创建对象
Studentli(2,"LiMing",3);
Graduatezhang(3,"ZhangGang","学生成绩管理系统");
//显示对象的相关信息
li.display();
cout<zhang.display();
}
程序的运行结果:
学号:
2
姓名:
LiMing
年级:
3
-----------------------------------
学号:
3
姓名:
ZhangGang
年级:
4
设计题目:
学生成绩管理系统
12.定义一个哺乳动物类,再由此派生出人类、狗类和猫类,这些类中均有speak()函数,观察在调用过程中,到底使用了谁的speak()函数。
参考程序:
#include
#include
classAnimal{
floatweight;
public:
voidspeak(){}
voidsetWeight(floatwt){weight=wt;}
floatgetWeight(){returnweight;}
};
classHuman:
publicAnimal{
public:
voidspeak(){
cout<<"说话"<}
};
classCat:
publicAnimal{
public:
voidspeak(){
cout<<"喵喵"<}
};
classDog:
publicAnimal{
public:
voidspeak(){
cout<<"汪汪"<}
};
voidmain(){
//定义三个对象
Humanhm;
Catcat;
Dogdog;
//调用不同类的speak函数
cout<<"人:
";
hm.speak();
cout<<"猫:
";
cat.speak();
cout<<"狗:
";
dog.speak();
}
程序的运行结果:
人:
说话
猫:
喵喵
狗:
汪汪
13.通过多重继承定义研究生类,研究生既有学生的属性,又有教师的属性。
参考程序:
#include
#include
classPerson{
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<<"姓名:
"<cout<<"性别:
"<<(m_nSex==0?
"男":
"女")<cout<<"年龄:
"<}
};
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<<"工作单位:
"<cout<<"月薪:
"<}
voidsetSalary(intsalary){
m_fSalary=salary;
}
char*getDept(){
returnm_strDept;
}
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<<"学号:
"<Person:
:
ShowMe();
cout<<"班级:
"<}
voidsetID(char*ID){
strcpy(m_strID,ID);
}
voidsetClass(char*Class){
strcpy(m_strClass,Class);
}
char*getID(){
returnm_strID;
}
char*getClass(){
returnm_strClass;
}
};
classMaster:
publicStudent,publicTeacher{
public:
Master(char*name,intage,charsex,char*ID,char*Class,char*dept,intsalary)
:
Student(name,age,sex,ID,Class),Teacher(name,age,sex,dept,salary){
}
voidShowMe(){
Student:
:
ShowMe();
cout<<"工作单位:
"<cout<<"月薪:
"<}
};
voidmain(){
//定义三个不同类的对象
Teacherteacher1("刘馨",38,'m',"计算机系",3800);
Studentstd1("刘丽",20,'f',"03016003","计算机03");
Mastermaster("张鑫",25,'f',"003","机械052","机械系",1000);
//显示各类人员的属性
teacher1.ShowMe();
cout<<"--------------------"<std1.ShowMe();
cout<<"--------------------"<master.ShowMe();
}
程序的运行结果为:
姓名:
刘馨
性别:
男
年龄:
38
工作单位:
计算机系
月薪:
3800
--------------------
学号:
03016003
姓名:
刘丽
性别:
女
年龄:
20
班级:
计算机03
--------------------
学号:
003
姓名:
张鑫
性别:
女
年龄:
25
班级:
机械052
工作单位:
机械系
月薪:
1000