您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > c++面向对象课后答案第8章
1.1在C++中,三种派生方式的说明符号为public、private、protected不加说明,则默认的派生方式为private。1.2当公有派生时,基类的公有成员成为派生类的公有成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。当保护派生时,基类的公有成员成为派生类的保护成员;保护成员成为派生类的保护成员;私有成员成为派生类的不能直接访问成员。1.3派生类的构造函数一般有3项工作要完成:首先基类初始化,其次成员对象初始化,最后执行派生类构造函数体。1.4多继承时,多个基类中的同名的成员在派生类中由于标识符不唯一而出现二义性。在派生类中采用虚基类或作用域分辨符来消除该问题。2.简答题2.1派生类如何实现对基类私有成员的访问?2.2什么是类型兼容规则?2.3派生类的构造函数是怎样的执行顺序,析构函数的执行顺序是如何实现的?2.4继承与组合之间的区别与联系是什么?2.5什么是虚基类?它有什么作用?含有虚基类的派生类的构造函数有什么要求,什么是最远派生类,建立一个含有虚基类的派生类的对象时,为什么由最远派生类的构造函数负责虚基类的初始化?3.选择题3.1下面对派生类的描述中,错误的是(D)。A.一个派生类可以作为另外一个派生类的基类B.派生类至少有一个基类C.派生类的成员除了它自己的成员外,还包含了它的基类的成员D.派生类中继承的基类成员的访问权限到派生类中保持不变3.2下列对友元关系叙述正确的是(A)。A.不能继承B.是类与类的关系C.是一个类的成员函数与另一个类的关系D.提高程序的运行效率3.3当保护继承时,基类的(B)在派生类中成为保护成员,不能通过派生类的对象来直接访问。A.任何成员B.公有成员和保护成员C.公有成员和私有成员D.私有成员3.4设置虚基类的目的是(B)。A.简化程序B.消除二义性C.提高运行效率D.减少目标代码3.5在公有派生情况下,有关派生类对象和基类对象的关系,不正确的叙述是(C)。A.派生类的对象可以赋给基类的对象B.派生类的对象可以初始化基类的引用C.派生类的对象可以直接访问基类中的成员D.派生类的对象的地址可以赋给指向基类的指针3.6有如下类定义: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.4B.3C.2D.13.7程序如下:#includeiostreamusingnamespacestd;classA{public:A(){cout”A”;}};classB{public:B(){cout”B”;}};classC:publicA{Bb;public:C(){cout”C”;}};intmain(){Cobj;return0;}执行后的输出结果是(D)。A.CBAB.BACC.ACBD.ABC3.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;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){}3.10有如下程序:#includeiostreamusingnamespacestd;classBase{private:voidfun1()const{cout”fun1”;}protected:voidfun2()const{cout”fun2”;}public:voidfun3()const{cout”fun3”;}};classDerived:protectedBase{public:voidfun4()const{cout”fun4”;}};intmain(){Derivedobj;obj.fun1();//①obj.fun2();//②obj.fun3();//③obj.fun4();//④}其中没有语法错误的语句是(D)。A.①B.②C.③D.④4.写出程序运行结果4.l#includeiostreamusingnamespacestd;classB1{public:B1(inti){cout”constructingB1“iendl;}~B1(){cout”destructingB1“endl;}};classB2{public:B2(){cout”constructingB3*”endl;}~B2(){cout”destructingB3”endl;}};classC:publicB2,virtualpublicB1{intj;public:C(inta,intb,intc):B1(a),memberB1(b),j(c){}private:B1memberB1;B2memberB2;};intmain(){Cobj(1,2,3);}constructingB11constructingB3*constructingB12constructingB3*destructingB3destructingB1destructingB3destructingB14.2#includeiostreamusingnamespacestd;classB{public:voidf1(){coutB::f1endl;}};classD:publicB{public:voidf1(){coutD::f1endl;}};voidf(B&rb){rb.f1();}intmain(){Dd;Bb,&rb1=b,&rb2=d;f(rb1);f(rb2);return0;}B::f1B::f15.编程题5.1定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。#includeiostreamusingnamespacestd;constdoublePI=3.14159;classPoint{public:Point(doublex=0,doubley=0){X=x;Y=y;}voidShowPoint(){cout(X,Y)endl;}private:doubleX,Y;};classRectangle:publicPoint{public:Rectangle(doublew,doubleh,doublex,doubley):Point(x,y){width=w,height=h;Area();}voidArea(){area=width*height;}voidShowArea(){coutRectangleArea=areaendl;}private:doublewidth,height,area;};classCircle:publicPoint{public:Circle(doubler,doublex,doubley):Point(x,y){radius=r;Area();}voidArea(){area=PI*radius*radius;}voidShowArea(){coutCircleArea=areaendl;}private:doubleradius,area;};intmain(){Rectangler(10,8,0,0);Circlec(4,3,5);r.ShowArea();c.ShowArea();return0;}5.2设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。#includeiostream#includecstringusingnamespacestd;enumAMPM{AM=1,PM};classBuilding{public:Building(char*);voidShowBuilding(){coutName;}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();coutNo:No;coutFloors:Floors;coutClassRooms:ClassRooms;coutArea:Areaendl;}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();coutNo:No;coutFloors:Floors;coutDormRooms:DormRooms;coutArea:Area;coutStudentNum:StudentNumendl;}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);tb.ShowTeach_Building();db.ShowDorm_Building();return0;}5.3定义并描述一个Table类和一个Circle类,由它们共同派生出RoundTable类。(这题做得不太满意)#includeiostreamusingnamespacestd;classTable{public:Table(intlegs){Legs=legs;}protected:intLegs;};classCircle{public:Circle(intradius){Radius=radius;}protected
本文标题:c++面向对象课后答案第8章
链接地址:https://www.777doc.com/doc-6384413 .html