c++继承与派生习题答案Word格式.docx
- 文档编号:7025315
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:20
- 大小:98.23KB
c++继承与派生习题答案Word格式.docx
《c++继承与派生习题答案Word格式.docx》由会员分享,可在线阅读,更多相关《c++继承与派生习题答案Word格式.docx(20页珍藏版)》请在冰点文库上搜索。
”A”;
classB{public:
B(){cout<
”B”;
}};
classC:
publicA{
Bb;
C(){cout<
”C”;
intmain(){Cobj;
return0;
执行后的输出结果是(D)。
A.CBAB.BACC.ACBD.ABC
3.8类O定义了私有函数F1。
P和Q为O的派生类,定义为classP:
protectedO{…};
classQ:
publicO{…}。
(C)可以访问Fl。
A.O的对象B.P类内C.O类内D.Q类内
3.9有如下类定义:
classXA{
intx;
public:
XA(intn){x=n;
};
classXB:
publicXA{
inty;
XB(inta,intb);
在构造函数XB的下列定义中,正确的是(B)。
A.XB:
XB(inta,intb):
x(a),y(b){}
B.XB:
XA(a),y(b){}
C.XB:
x(a),XB(b){}
D.XB:
XA(a),XB(b){}
3.10有如下程序:
classBase{
private:
voidfun1()const{cout<
”fun1”;
protected:
voidfun2()const{cout<
”fun2”;
voidfun3()const{cout<
”fun3”;
classDerived:
protectedBase{
voidfun4()const{cout<
”fun4”;
intmain(){
Derivedobj;
obj.fun1();
//①
obj.fun2();
//②
obj.fun3();
//③
obj.fun4();
//④
其中没有语法错误的语句是(D)。
A.①B.②C.③D.④
4.写出程序运行结果
4.l#include<
classB1{
B1(inti){cout<
”constructingB1“<
i<
endl;
}
~B1(){cout<
”destructingB1“<
classB2{
B2(){cout<
”constructingB3*”<
~B2(){cout<
”destructingB3”<
publicB2,virtualpublicB1{
C(inta,intb,intc):
B1(a),memberB1(b),j(c){}
B1memberB1;
B2memberB2;
intmain(){
Cobj(1,2,3);
constructingB11
constructingB3*
constructingB12
destructingB3
destructingB1
4.2#include<
classB{
voidf1(){cout<
"
B:
f1"
classD:
publicB{
D:
voidf(B&
rb){
rb.f1();
Dd;
Bb,&
rb1=b,&
rb2=d;
f(rb1);
f(rb2);
B:
f1
5.编程题
5.1定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。
constdoublePI=3.14159;
classPoint{
Point(doublex=0,doubley=0){X=x;
Y=y;
voidShowPoint(){cout<
("
X<
"
Y<
)"
doubleX,Y;
classRectangle:
publicPoint{
Rectangle(doublew,doubleh,doublex,doubley):
Point(x,y)
{width=w,height=h;
Area();
}
voidArea(){area=width*height;
voidShowArea(){
cout<
RectangleArea="
area<
doublewidth,height,area;
classCircle:
Circle(doubler,doublex,doubley):
{radius=r;
voidArea(){area=PI*radius*radius;
CircleArea="
doubleradius,area;
intmain(){
Rectangler(10,8,0,0);
Circlec(4,3,5);
r.ShowArea();
c.ShowArea();
5.2设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。
cstring>
enumAMPM{AM=1,PM};
classBuilding{
Building(char*);
voidShowBuilding(){
Name;
charName[30];
Building:
Building(char*name){
strcpy(Name,name);
classTeach_Building:
publicBuilding{
Teach_Building(char*,int,int,int,int);
voidShowTeach_Building(){
Building:
ShowBuilding();
No:
No;
Floors:
Floors;
ClassRooms:
ClassRooms;
Area:
Area<
intNo,Floors,ClassRooms,Area;
Teach_Building:
Teach_Building(char*name,intno,intfl,intcr,intar):
Building(name){
No=no;
Floors=fl;
ClassRooms=cr;
Area=ar;
classDorm_Building:
publicBuilding{
Dorm_Building(char*,int,int,int,int,int);
voidShowDorm_Building(){
DormRooms:
DormRooms;
Area;
StudentNum:
StudentNum<
intNo,Floors,DormRooms,Area,StudentNum;
Dorm_Building:
Dorm_Building(char*name,intno,intfl,intdr,intar,intsn):
DormRooms=dr;
StudentNum=sn;
Teach_Buildingtb("
主教学楼"
59,6,66,18000);
Dorm_Buildingdb("
北苑男生宿舍"
41,5,75,3750,300);
tb.ShowTeach_Building();
db.ShowDorm_Building();
5.3定义并描述一个Table类和一个Circle类,由它们共同派生出RoundTable类。
(这题做得不太满意)
classTable{
Table(intlegs){
Legs=legs;
intLegs;
classCircle{
Circle(intradius){
Radius=radius;
intRadius;
classRoundTable:
publicTable,publicCircle{
RoundTable(intlegs,intradius):
Table(legs),Circle(radius){}
5.4利用第四章习题5.2Clock类,派生一个带“AM”、“PM”的新时钟类NewClock。
classClock{
Clock(int=0,int=0,ints=0);
voidShowTime(){
Hour<
Minute<
Second;
intHour,Minute,Second;
Clock:
Clock(inth,intm,ints){
Hour=h;
Minute=m;
Second=s;
classNewClock:
publicClock{
NewClock();
NewClock(Clockc,AMPMap):
Clock(c){
Ap=ap;
Clock:
ShowTime();
(Ap==AM?
"
AM"
PM"
)<
AMPMAp;
NewClocknc(Clock(8,23,34),AMPM
(2));
nc.ShowTime();
5.5利用NewClock类与Date类定义一个带日期的时钟类ClockwithDate。
对该类对象能进行增加减少秒数操作。
classDate{
intYear,Month,Day;
staticconstintdays[];
boolLeapYear();
boolEndofMonth();
Date(int=1900,int=1,int=1);
voidIncrement(int);
voidDecrement(int);
voidSetDate(int,int,int);
voidShowDate();
constintDate:
days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
Date:
Date(inty,intm,intd){
SetDate(y,m,d);
voidDate:
SetDate(inty,intm,intd){
Year=(y>
=1900&
&
y<
=9999)?
y:
1900;
Month=(m>
=1&
m<
=12)?
m:
1;
if(Month==2&
LeapYear())
Day=(d>
d<
=29)?
d:
else
=days[Month])?
boolDate:
LeapYear(){
return((Year%400==0)||(Year%4==0&
Year%100!
=0))?
true:
false;
EndofMonth(){
returnDay==29;
returnDay==days[Month];
Increment(intn){
inti;
for(i=1;
=n;
i++)
if(EndofMonth()&
Month==12){
Year++;
Month=Day=1;
elseif(EndofMonth()){
Month++;
Day=1;
Day++;
Decrement(intn){
if(Day==1){
if(Month==1){
Year--;
Month=12;
Day=31;
}
elseif(Month==3){
Day=LeapYear()?
29:
28;
Month=2;
else
Day=days[--Month];
Day--;
ShowDate(){
cout<
Year<
-"
Month<
Day<
classClockwithDate:
publicDate,publicNewClock{
ClockwithDate(Date,Clock,AMPM);
voidIncrementSecond(int);
voidDecrementSecond(int);
voidShowDateandTime(){
Date:
ShowDate();
NewClock:
ShowTime();
ClockwithDate:
ClockwithDate(Dated,Clockc,AMPMap):
Date(d),NewClock(c,ap){}
voidClockwithDate:
IncrementSecond(intn){
i++){
Second++;
if(Second==60){
Second=0;
Minute++;
if(Minute==60){
Minute=0;
Hour++;
if(Hour==24){
Hour=0;
Date:
Increment
(1);
}
}
DecrementSecond(intn){inti;
Second--;
if(Second==-1){
Second=59;
Minute--;
if(Minute==-1){
Minute=59;
Hour--;
if(Hour==-1){
Hour=23;
Decrement
(1);
ClockwithDatecd(Date(2007,2,28),Clock(23,59,34),AMPM
(2));
cd.ShowDateandTime();
cd.IncrementSecond(27);
cd.DecrementSecond(27);
5.6编写一个程序实现小型公司的工资管理。
该公司主要有4类人员:
经理(main-ager)、技术人员(technician)、销售员(salesman)、销售经理(salesmanager)。
这些人员都是职员(employee),有编号、姓名、月工资信息。
月工资的计算方法是:
经理固定月薪8000元,技术人员每小时100元,销售员按当月销售额4%提成,销售经理既拿固定月工资5000元也拿销售提成,销售提成为所管辖部门当月销售额的5‰。
要求编程计算职员的月工资并显示全部信息。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 继承 派生 习题 答案