c++面向对象课后答案第8章.docx
- 文档编号:14191159
- 上传时间:2023-06-21
- 格式:DOCX
- 页数:21
- 大小:19.23KB
c++面向对象课后答案第8章.docx
《c++面向对象课后答案第8章.docx》由会员分享,可在线阅读,更多相关《c++面向对象课后答案第8章.docx(21页珍藏版)》请在冰点文库上搜索。
c++面向对象课后答案第8章
在C++中,三种派生方式的说明符号为public、private、protected不加说明,则默认的派生方式为private。
当公有派生时,基类的公有成员成为派生类的公有成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。
当保护派生时,基类的公有成员成为派生类的保护成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。
派生类的构造函数一般有3项工作要完成:
首先基类初始化,其次成员对象初始化,最后执行派生类构造函数体。
多继承时,多个基类中的同名的成员在派生类中由于标识符不唯一而出现二义性。
在派生类中采用虚基类或作用域分辨符来消除该问题。
2.简答题
派生类如何实现对基类私有成员的访问
什么是类型兼容规则
@
派生类的构造函数是怎样的执行顺序,析构函数的执行顺序是如何实现的
继承与组合之间的区别与联系是什么
什么是虚基类它有什么作用含有虚基类的派生类的构造函数有什么要求,什么是最远派生类,建立一个含有虚基类的派生类的对象时,为什么由最远派生类的构造函数负责虚基类的初始化
3.选择题
下面对派生类的描述中,错误的是(D)。
A.一个派生类可以作为另外一个派生类的基类
B.派生类至少有一个基类
《
C.派生类的成员除了它自己的成员外,还包含了它的基类的成员
D.派生类中继承的基类成员的访问权限到派生类中保持不变
下列对友元关系叙述正确的是(A)。
A.不能继承
B.是类与类的关系
C.是一个类的成员函数与另一个类的关系
D.提高程序的运行效率
当保护继承时,基类的(B)在派生类中成为保护成员,不能通过派生类的对象来直接访问。
】
A.任何成员 B.公有成员和保护成员
C.公有成员和私有成员 D.私有成员
设置虚基类的目的是(B)。
A.简化程序 B.消除二义性
C.提高运行效率 D.减少目标代码
在公有派生情况下,有关派生类对象和基类对象的关系,不正确的叙述是(C)。
A.派生类的对象可以赋给基类的对象
B.派生类的对象可以初始化基类的引用
,
C.派生类的对象可以直接访问基类中的成员
D.派生类的对象的地址可以赋给指向基类的指针
有如下类定义:
classMyBASE{
intk;
public:
voidset(intn){k=n;}
intget()const{returnk;}
.
};
classMyDERIVED:
protectedMyBASE{
protected;
intj;
public:
voidset(intm,intn){MyBASE:
:
set(m);j=n;}
intget()const{returnMyBASE:
:
get()+j;}
};
<
则类MyDERIVED中保护成员个数是(B)。
A.4 B.3 C.2 D.1
程序如下:
#include
usingnamespacestd;
classA{
public:
A(){cout<<”A”;}
《
};
classB{public:
B(){cout<<”B”;}};
classC:
publicA{
Bb;
public:
C(){cout<<”C”;}
};
intmain(){Cobj;return0;}
~
执行后的输出结果是(D)。
A.CBA B.BAC C.ACB D.ABC
类O定义了私有函数F1。
P和Q为O的派生类,定义为classP:
protected O{…};classQ:
publicO{…}。
(C)可以访问Fl。
A.O的对象 B.P类内 C.O类内 D.Q类内
有如下类定义:
classXA{
intx;
public:
—
XA(intn){x=n;}
};
classXB:
publicXA{
inty;
public:
XB(inta,intb);
};
在构造函数XB的下列定义中,正确的是(B)。
&
A.XB:
:
XB(inta,intb):
x(a),y(b){}
B.XB:
:
XB(inta,intb):
XA(a),y(b){}
C.XB:
:
XB(inta,intb):
x(a),XB(b){}
D.XB:
:
XB(inta,intb):
XA(a),XB(b){}
有如下程序:
#include
usingnamespacestd;
classBase{
?
private:
voidfun1()const{cout<<”fun1”;}
protected:
voidfun2()const{cout<<”fun2”;}
public:
voidfun3()const{cout<<”fun3”;}
};
classDerived:
protected Base{
…
public:
voidfun4()const{cout<<”fun4”;}
};
intmain(){
Derivedobj;
(); //①
(); //②
(); //③
:
(); //④
}
其中没有语法错误的语句是(D)。
A.① B.② C.③ D.④
4.写出程序运行结果
#include
usingnamespacestd;
、
classB1{
public:
B1(inti){ cout<<”constructingB1“<
~B1(){ cout<<”destructingB1“< }; classB2{ public: B2(){ cout<<”constructingB3*”< 。 ~B2(){ cout<<”destructingB3”< }; classC: publicB2,virtualpublicB1{ intj; public: C(inta,intb,intc): B1(a),memberB1(b),j(c){} private: B1memberB1; ¥ B2memberB2; }; intmain(){ Cobj(1,2,3); } constructingB11 constructingB3* constructingB12 … constructingB3* destructingB3 destructingB1 destructingB3 destructingB1 #include usingnamespacestd; — classB{ public: voidf1(){cout<<"B: : f1"< }; classD: publicB{ public: voidf1(){cout<<"D: : f1"< }; ¥ voidf(B&rb){ (); } intmain(){ Dd; Bb,&rb1=b,&rb2=d; f(rb1); f(rb2); return0; ] } B: : f1 B: : f1 5.编程题 定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。 #include usingnamespacestd; constdoublePI=; ? classPoint{ public: Point(doublex=0,doubley=0){X=x;Y=y;} voidShowPoint(){cout<<"("<< X<<","< private: doubleX,Y; }; classRectangle: publicPoint{ public: Rectangle(doublew,doubleh,doublex,doubley): Point(x,y) {width=w,height=h;Area();} voidArea(){area=width*height;} voidShowArea(){ cout<<"RectangleArea="< } private: ) doublewidth,height,area; }; classCircle: publicPoint{ public: Circle(doubler,doublex,doubley): Point(x,y) {radius=r;Area();} voidArea(){area=PI*radius*radius;} voidShowArea(){ - cout<<"CircleArea="< } private: doubleradius,area; }; intmain(){ Rectangler(10,8,0,0); Circlec(4,3,5); … (); (); return0; } 设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。 #include #include usingnamespacestd; \ enumAMPM{AM=1,PM}; classBuilding{ public: Building(char*); voidShowBuilding(){ cout< } protected: ¥ charName[30]; }; Building: : Building(char*name){ strcpy(Name,name); }; classTeach_Building: publicBuilding{ public: 【 Teach_Building(char*,int,int,int,int); voidShowTeach_Building(){ Building: : ShowBuilding(); cout<<"No: "< cout<<"Floors: "< cout<<"ClassRooms: "< cout<<"Area: "< } ) protected: 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{ public: · Dorm_Building(char*,int,int,int,int,int); voidShowDorm_Building(){ Building: : ShowBuilding(); cout<<"No: "< cout<<"Floors: "< cout<<"DormRooms: "< cout<<"Area: "< cout<<"StudentNum: "< % } protected: intNo,Floors,DormRooms,Area,StudentNum; }; Dorm_Building: : Dorm_Building(char*name,intno,intfl,intdr,intar,intsn): Building(name){ No=no;Floors=fl;DormRooms=dr;Area=ar;StudentNum=sn; } ! intmain(){ Teach_Buildingtb("主教学楼",59,6,66,18000); Dorm_Buildingdb("北苑男生宿舍",41,5,75,3750,300); (); (); return0; } 定义并描述一个Table类和一个Circle类,由它们共同派生出RoundTable类。 ` (这题做得不太满意) #include usingnamespacestd; classTable{ public: Table(intlegs){ Legs=legs; } ) protected: intLegs; }; classCircle{ public: Circle(intradius){ Radius=radius; < } protected: intRadius; }; classRoundTable: publicTable,publicCircle{ public: RoundTable(intlegs,intradius): Table(legs),Circle(radius){} protected: 【 }; intmain(){ return0; } 利用第四章习题Clock类,派生一个带“AM”、“PM”的新时钟类NewClock。 #include usingnamespacestd; enumAMPM{AM=1,PM}; } classClock{ public: Clock(int=0,int=0,ints=0); voidShowTime(){ cout< "< "< } private: intHour,Minute,Second; 、 }; Clock: : Clock(inth,intm,ints){ Hour=h;Minute=m;Second=s; }; classNewClock: publicClock{ public: NewClock(); 【 NewClock(Clockc,AMPMap): Clock(c){ Ap=ap; } voidShowTime(){ Clock: : ShowTime(); cout<<(Ap==AM"AM": "PM")< } private: # AMPMAp; }; intmain(){ NewClocknc(Clock(8,23,34),AMPM (2)); (); return0; } - 利用NewClock类与Date类定义一个带日期的时钟类ClockwithDate。 对该类对象能进行增加减少秒数操作。 #include usingnamespacestd; enumAMPM{AM=1,PM}; classClock{ public: Clock(int=0,int=0,ints=0); voidShowTime(){ > cout< "< "< } protected: intHour,Minute,Second; }; Clock: : Clock(inth,intm,ints){ Hour=h;Minute=m;Second=s; }; 《 classNewClock: publicClock{ public: NewClock(Clockc,AMPMap): Clock(c){ Ap=ap; } voidShowTime(){ Clock: : ShowTime(); ; cout<<(Ap==AM"AM": "PM")< } protected: AMPMAp; }; classDate{ protected: 》 intYear,Month,Day; staticconstintdays[]; boolLeapYear(); boolEndofMonth(); public: 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>=1&&d<=29)d: 1; else Day=(d>=1&&d<=days[Month])d: 1; … } boolDate: : LeapYear(){ return((Year%400==0)||(Year%4==0&&Year%100! =0))true: false; } boolDate: : EndofMonth(){ if(Month==2&&LeapYear()) returnDay==29; ~ else returnDay==days[Month]; } voidDate: : Increment(intn){ inti; for(i=1;i<=n;i++) if(EndofMonth()&&Month==12){ Year++; Month=Day=1; < } elseif(EndofMonth()){ Month++;Day=1; } else Day++; } voidDate: : Decrement(intn){ ) inti; for(i=1;i<=n;i++) if(Day==1){ if(Month==1){ Year--;Month=12;Day=31; } elseif(Month==3){ Day=LeapYear()29: 28;Month=2; > } else Day=days[--Month]; } else Day--; } $ voidDate: : ShowDate(){ cout< } classClockwithDate: publicDate,publicNewClock{ public: 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){ inti; for(i=1;i<=n;i++){ Second++; if(Second==60){ Second=0; Minute++; if(Minute==60){ Minute=0; Hour++; if(Hour==24){ Hour=0; Date: : Increment (1); } } } } } voidClockwithDate: : DecrementSecond(intn){ inti; for(i=1;i<=n;i++){ Second--; if(Second==-1){ Second=59; Minute--; if(Minute==-1){ Minute=59; Hour--; if(Hour==-1){ Hour=23; Date: : Decrement (1); } } } } } intmain(){ ClockwithDatecd(Date(2007,2,28),Clock(23,59,34),AMPM (2)); (); (27); (); (27); (); return0; } 编写一个程序实现小型公司的工资管理。 该公司主要有4类人员: 经理(main-ager)、技术人员(technician)、销售员(salesman)、销售经理(salesmanager)。 这些人员都是职员(employee),有编号、姓名、月工资信息。 月工资的计算方法是: 经理固定月薪8000元,技术人员每小时100元,销售员按当月
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 面向 对象 课后 答案
![提示](https://static.bingdoc.com/images/bang_tan.gif)