c++继承与派生习题答案.docx

上传人:b****1 文档编号:14719616 上传时间:2023-06-26 格式:DOCX 页数:19 大小:19.31KB
下载 相关 举报
c++继承与派生习题答案.docx_第1页
第1页 / 共19页
c++继承与派生习题答案.docx_第2页
第2页 / 共19页
c++继承与派生习题答案.docx_第3页
第3页 / 共19页
c++继承与派生习题答案.docx_第4页
第4页 / 共19页
c++继承与派生习题答案.docx_第5页
第5页 / 共19页
c++继承与派生习题答案.docx_第6页
第6页 / 共19页
c++继承与派生习题答案.docx_第7页
第7页 / 共19页
c++继承与派生习题答案.docx_第8页
第8页 / 共19页
c++继承与派生习题答案.docx_第9页
第9页 / 共19页
c++继承与派生习题答案.docx_第10页
第10页 / 共19页
c++继承与派生习题答案.docx_第11页
第11页 / 共19页
c++继承与派生习题答案.docx_第12页
第12页 / 共19页
c++继承与派生习题答案.docx_第13页
第13页 / 共19页
c++继承与派生习题答案.docx_第14页
第14页 / 共19页
c++继承与派生习题答案.docx_第15页
第15页 / 共19页
c++继承与派生习题答案.docx_第16页
第16页 / 共19页
c++继承与派生习题答案.docx_第17页
第17页 / 共19页
c++继承与派生习题答案.docx_第18页
第18页 / 共19页
c++继承与派生习题答案.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

c++继承与派生习题答案.docx

《c++继承与派生习题答案.docx》由会员分享,可在线阅读,更多相关《c++继承与派生习题答案.docx(19页珍藏版)》请在冰点文库上搜索。

c++继承与派生习题答案.docx

c++继承与派生习题答案

1.概念填空题

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.1

3.7程序如下:

#include

usingnamespacestd;

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.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;

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有如下程序:

#include

usingnamespacestd;

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#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

4.2#include

usingnamespacestd;

classB{

public:

voidf1(){cout<<"B:

:

f1"<

};

classD:

publicB{

public:

voidf1(){cout<<"D:

:

f1"<

};

voidf(B&rb){

rb.f1();

}

intmain(){

Dd;

Bb,&rb1=b,&rb2=d;

f(rb1);f(rb2);

return0;

}

B:

:

f1

B:

:

f1

5.编程题

5.1定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。

#include

usingnamespacestd;

constdoublePI=3.14159;

classPoint{

public:

Point(doublex=0,doubley=0){X=x;Y=y;}

voidShowPoint(){cout<<"("<

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);

r.ShowArea();

c.ShowArea();

return0;

}

5.2设计一个建筑物类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);

tb.ShowTeach_Building();

db.ShowDorm_Building();

return0;

}

5.3定义并描述一个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;

}

5.4利用第四章习题5.2Clock类,派生一个带“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));

nc.ShowTime();

return0;

}

5.5利用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));

cd.ShowDateandTime();

cd.IncrementSecond(27);

cd.ShowDateandTime();

cd.DecrementSecond(27);

cd.ShowDateandTime();

return0;

}

5.6编写一个程序实现小型公司的工资管理。

该公司主要有4类人员:

经理(main-ager)、技术人员(technician)、销售员(salesman)、销售经理(salesmanager)。

这些人员都是职员(employee),有编号、姓名、月工资信息。

月工资的计算方法是:

经理固定月薪8000

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2