C继承与派生.docx
《C继承与派生.docx》由会员分享,可在线阅读,更多相关《C继承与派生.docx(24页珍藏版)》请在冰豆网上搜索。
C继承与派生
第4章继承与派生
一、简答题
1.有以下程序结构,请分析访问属性。
classA//A为基类
{public:
voidfunc1();
inti;
protected:
voidfunc2();
intj;
private:
intk;
};
classB:
publicA//B为A的公用派生类
{public:
voidfunc3();
protected:
intm;
private:
intn;
};
classC:
publicB//C为B的公用派生类
{public:
voidfunc4();
private:
intp;
};
intmain()
{Aa;//a是基类A的对象
Bb;//b是派生类B的对象
Cc;//c是派生类C的对象
return0;
}
问:
(1)在main函数中能否用b.i,b.j和b.k访问派生类B对象b中基类A的成员?
(2)派生类B中的成员函数能否调用基类A中的成员函数func1和func2?
(3)派生类B中的成员函数能否访问基类A中的数据成员i,j,k?
(4)能否在main函数中用c.i,c.j,c.k,c.m,c.n,c.p访问基类A的成员i,j,k,派生类B的成员m,n,以及派生类C的成员p?
(5)能否在main函数中用c.func1(),c.func2(),c.func3()和c.func4()调用func1,func2,func3,func4成员函数?
(6)派生类C的成员函数func4能否调用基类A中的成员函数func1,func2和派生类B中的成员函数func3?
【答案要点】各成员在各类的范围内的访问权限如下表:
类的范围
func1
i
func2
j
k
func3
m
n
func4
p
基类A
公用
公用
保护
保护
私有
公用派生类B
公用
公用
保护
保护
不可访问
公用
保护
私有
公用派生类C
公用
公用
保护
保护
不可访问
公用
保护
不可访问
公用
私有
(1)在main函数中能用b.i访问派生类B对象b中基类A的成员i,因为它在派生类B中是公用数据成员。
不能用b.j访问派生类B对象b中基类A的成员j,因为它在派生类B中是保护数据成员,不能被类外访问。
不能用b.k访问派生类B对象b中基类A的成员k,因为它是基类A的私用数据成员,只有基类A的成员函数可以访问,不能被类外访问。
(2)派生类B中的成员函数能调用基类A中的成员函数func1和func2,因为func1、func2在派生类B中是公用成员和保护成员,可以被派生类的成员函数访问。
(3)派生类B中的成员函数能访问基类A中的数据成员i、j,因为i、j在派生类B中是公用成员和保护成员,可以被派生类的成员函数访问。
派生类B中的成员函数不能访问基类A中的数据成员k,因为它在派生类B中是不可访问的成员。
(4)能在main函数中用c.i访问基类A的成员i,不能用c.j、c.k访问基类A的成员j、k,因为它们在派生类C中是保护成员和私有成员,不能被类外访问。
也不能用c.m、c.n访问派生类B的成员m、n,因为它们在派生类C中也是保护成员和私有成员,不能被类外访问。
也不能用c.p访问派生类C中的私用成员p。
(5)能在main函数中用c.func1()、c.func3()和c.func4()调用func1、func3、func4成员函数,因为它们在派生类C中是公用成员函数,可以在类外被访问。
不能在main函数中用c.func2()调用func2成员函数,因为它在派生类C中是保护成员函数,不能在类外被访问。
(6)派生类C的成员函数func4能调用基类A中的成员函数func1、func2和派生类中的成员函数func3,因为func1、func3在派生类C中是公用成员函数,func2在派生类C中是保护成员函数,都可以被派生类C的成员函数调用。
2.已给商品类及其多层的派生类。
以商品类为基类。
第一层派生出服装类、家电类、车辆类。
第二层派生出衬衣类、外衣类、帽子类、鞋子类;空调类、电视类、音响类;自行车类、轿车类、摩托车类。
请给出商品类及其多层派生类的基本属性和派生过程中增加的属性。
【答案要点】
按题意没有操作,所以只列出各个类的数据成员,也不再在main函数中对各类进行测试。
#include
usingnamespacestd;
classCommodity{
doubleprice;//价格
charname[20];//商品名
charmanufacturer[20];//生产厂家
intitems;//数量
};
classClothing:
publicCommodity{//服装类
chartexture[20];//材料质地
};
classElectricAppliance:
publicCommodity{//家电类
enum{Black,White}type;//黑白家电
};
classVehicle:
publicCommodity{//车辆类
intwheelNum;//车轮数量
};
classShirt:
publicClothing{//衬衣类
enum{Formal,Casual}style;//式样:
正式、休闲
};
classGarment:
publicClothing{//外衣类
enum{Jacket,Coat}style;//式样:
夹克、外套
};
classHat:
publicClothing{//帽子类
enum{Winter,Summer,Spring,Autumn}style;//季节风格
};
classShoes:
publicClothing{//鞋子类
enum{Winter,Summer,Spring,Autumn}style;//季节风格
};
classAirCondition:
publicElectricAppliance{//空调
boolwarmCool;//是否冷暖
floatpower;//功率
};
classTelevision:
publicElectricAppliance{//电视类
intsize;//尺寸
boolisColor;//是否彩色
};
classAcoustics:
publicElectricAppliance{//音响类
intspeakerNum;//喇叭数目
floatpower;//功率
};
classBicycle:
publicVehicle{//自行车类
intspeedGrades;//调速级数
intwheelSize;//轮子大小
};
classCar:
publicVehicle{//轿车类
floatvolume;//排气量
boolisSkyLight;//是否有天窗
intboxNum;//厢数
};
classMotorcycle:
publicVehicle{//摩托车类
floatvolume;//排气量
};
intmain(){return0;}
二、编程题
1.定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。
【程序参考代码】
#include
usingnamespacestd;
#include
classCountry
{public:
Country(char*n,char*c,doublep)
{strcpy(name,n);strcpy(capital,c);population=p;}
voidprint(){cout<private:
charname[10],capital[50];
doublepopulation;
};
classProvince:
privateCountry
{public:
Province(char*n,char*c,doublep,char*cc,doublepp):
Country(n,c,p)
{strcpy(provinceCapital,cc);provincePopulation=pp;}
voiddisplay()
{print();
cout<}
private:
charprovinceCapital[50];
doubleprovincePopulation;
};
intmain()
{
Provincep("China","Beijing",1.36e+010,"guangdong",1.05e+009);
p.display();
return0;
}
2.定义一个基类——Person类,有姓名、性别、年龄,再由基类派生出学生类——Student类和教师类——Teacher类,学生类增加学号、班级、专业和入学成绩,教师类增加工号、职称和工资。
【程序参考代码】
#include
#include
usingnamespacestd;
classPerson//声明公共基类Person
{public:
voidset(){cin>>name>>sex>>age;}//姓名、性别、年龄的输入
voiddisplay(){cout<<"name="<//姓名、性别、年龄的显示
private:
stringname;//姓名
charsex;//性别
intage;//年龄
};
classStudent:
publicPerson//声明Student类为Person类的公用派生类
{public:
voidinput()
{Person:
:
set();//姓名、性别、年龄的输入
cin>>stuId>>stuClass>>profession>>score;//学号、班级、专业、入学成绩的输入
}
voiddisplay()
{Person:
:
display();//姓名、性别、年龄的显示
cout<<"\tstuId="<<<"\tprofession="<//学号、班级、专业、入学成绩的显示
}
private:
stringstuId;//学号
stringstuClass;//班级
stringprofession;//专业
floatscore;//入学成绩
};
classTeacher:
publicPerson//声明Teacher类为Person类的公用派生类
{public:
voidset()
{Person:
:
set();//姓名、性别、年龄的输入
cin>>teachId>>title>>wage;//工号、职称、工资的输入
}
voiddisplay()
{Person:
:
display();//姓名、性别、年龄的显示
cout<<"\tteachId="<<<"\twage="<//工号、职称、工资的显示
}
private:
stringteachId;//工号
stringtitle;//职称
floatwage;//工资
};
intmain()
{Studentstudent;
Teacherteacher;
cout<<"Pleaseenterstudent'sname,sex,age,stuId,stuClass,professionandscore:
"<student.input();
cout<<"Displaystudent'sname,sex,age,stuId,stuClass,professionandscore:
"<student.display();
cout<<"Pleaseenterteacher'sname,sex,age,teachId,titleandwage:
"<teacher.set();
cout<<"Displayteacher'sname,sex,age,teachId,titleandwage:
"<teacher.display();
return0;
}
3.设计一个基类——Building类,有楼房的层数、房间数和总面积,再由基类派生出教学楼——TeachBuilding类和宿舍楼类——DormBuilding类,教学楼类增加教室数,宿舍楼类增加宿舍数、容纳学生总人数。
【程序参考代码】
#include
usingnamespacestd;
classBuilding{
public:
Building(intf,intr,doublea){floors=f;rooms=r;area=a;}
protected:
intfloors;
introoms;
doublearea;
};
classTeachBuilding:
publicBuilding{
public:
TeachBuilding(intf,intr,doublea,intcr):
Building(f,r,a){classrooms=cr;}
voidshow()
{
cout<<"floors="<<<"classrooms="<}
private:
intclassrooms;
};
classDormBuilding:
publicBuilding{
public:
DormBuilding(intf,intr,doublea,intd,intsc):
Building(f,r,a)
{dormitories=d;studcount=sc;}
voidshow()
{
cout<<"floors="<<<"dormitories="<<<"studcount="<}
private:
intdormitories;
intstudcount;
};
intmain()
{
TeachBuildingTB(6,80,200,35);
TB.show();
DormBuildingDB(6,80,200,35,300);
DB.show();
return0;
}
4.定义一个基类——汽车类,有型号、颜色、发动机功率、车速、重量、车牌号码,再由汽车类派生出客车类和货车类,客车类增加客车座位数、客运公司,货车类增加载货重量、货运公司。
【程序参考代码】
#include
#include
usingnamespacestd;
classCar
{public:
voidshow();
protected:
stringmodel;//型号
intcolor;//颜色
unsignedintmotopower;//发动机功率
unsignedintspeed;//车速
unsignedintweight;//重量
unsignedintid;//车牌号码
};
classBus:
publicCar
{public:
voidshow();
Bus(string_model,int_color,unsignedint_motopower,unsignedint_speed,
unsignedint_weight,unsignedint_id,unsignedint_seatnum,string_corp)
{
model=_model;color=_color;motopower=_motopower;speed=_speed;
weight=_weight;id=id;_seatnum=_seatnum;corp=_corp;
}
private:
unsignedintseatnum;//客车座位数
stringcorp;//客运公司
};
classTruck:
publicCar
{public:
voidshow();
Truck(string_model,int_color,unsignedint_motopower,
unsignedint_speed,unsignedint_weight,unsignedint_id,
unsignedint_load,string_corp)
{model=_model;color=_color;motopower=_motopower;speed=_speed;
weight=_weight;id=_id;load=_load;corp=_corp;}
private:
unsignedintload;//载货重量
stringcorp;//货运公司
};
voidCar:
:
show()
{
cout<<"型号:
"<cout<<"颜色:
"<cout<<"发动机功率:
"<cout<<"车速:
"<cout<<"重量:
"<cout<<"车牌号码:
"<}
voidBus:
:
show()
{
cout<<"型号:
"<cout<<"颜色:
"<cout<<"发动机功率:
"<cout<<"车速:
"<cout<<"重量:
"<cout<<"车牌号码:
"<cout<<"客车座位数:
"<cout<<"客运公司:
"<}
voidTruck:
:
show(){
cout<<"型号:
"<cout<<"颜色:
"<cout<<"发动机功率:
"<cout<<"车速:
"<cout<<"重量:
"<cout<<"车牌号码:
"<cout<<"载货重量:
"<cout<<"货运公司:
"<}
intmain(intargc,char**argv)
{
Car*a=newBus("黄海",3,300,100,2000,10101010,50,"北京客运");
Car*b=newTruck("东风",1,400,200,5000,10101011,3000,"北京货运");
cout<<"==============="<a->show();
cout<<"==============="<b->show();
system("pause");
return0;
}
5.定义一个Table类和Circle类,再由它们共同派生出RoundTable类。
【程序参考代码】
#include
usingnamespacestd;
#include
classCircle
{public:
Circle(doubler){radius=r;}
doublegetArea()
{return3.1416*radius*radius;}
private:
doubleradius;
};
classTable
{public:
Table(doubleh){height=h;}
doublegetHeight(){returnheight;}
private:
doubleheight;
};
classRoundTable:
publicTable,publicCircle
{public:
RoundTable(doubleh,doubler,charc[]):
Table(h),Circle(r)
{
color=newchar[strlen(c)+1];
strcpy(color,c);
}
char*getColor(){returncolor;}
private:
char*color;
};
intmain()
{
RoundTablert(0.8,1.0,"白色");
cout<<"圆桌的高:
"<cout<<"圆桌面积:
"<cout<<"圆桌颜色:
"<return0;
}
6.在第4题的基础上,由客车类和货车类再派生出一个客货两用车类,一辆客货两用车既具有客车的特征(有座位,可以载客),又具有货车的特征(有装载车厢,可以载货)。
要求将客货两用车类的间接共同基类——汽车类声明为虚基类。
【程序参考代码】
#include