面向对象离线作业_第1页
面向对象离线作业_第2页
面向对象离线作业_第3页
面向对象离线作业_第4页
面向对象离线作业_第5页
已阅读5页,还剩56页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第2章

[2.3]测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是

否有效。

//thisisastrange/*waytodoacomment*/

答:有效

[2.4]以下这个简短的C++程序不可能编译通过,为什么?

#include<iostream>

usingnamespacestd;

intmain()

{inta,b,c;

cout«"Entertwonumbers:n;

cin»a»b;

c=sum(a,b);

cout«nsumis:"«c;

return0;

)

sum(inta,intb)

{returna+b;

)

答:不能sum使用前必须声明

[2.5]回答问题。

(1)以下两个函数原型是否等价:

floatfun(inta,floatb,char*c);

floatfun(int,float,char*);

等价

(2)以下两个函数的第一行是否等价:

floatfun(inta,floatb,char*c);

floatfun(int,float,char*);

答:等价

[2.6]下列语句中错误的是(C)。

A.int*p=newint(l0);B.int*p=newint[l0];

C.int*p=newint;D.int*p=newint[40](0);

[2.7]假设已经有定义"constchar*constname二〃chen〃;”下面的语句中

正确的是(D)o

A.name[3]=H;B.name=nlinn;

C.name=newchar[5];D.cout«name[3];

[2.8]假设已经有定义“char,*constname二〃chen〃;”下面的语句中正确的

是(C)o

A.name[3]='q';B.name="linn;

C.name=newchar[5];D.name=newchar('q');

[2.9]假设已经有定义"constchar*name=〃chen〃;”下面的语句中错误的

是(A)o

A.name[3]='q';B.name=nlinn;

C.name=newchar[5];D.name=newchar('q');

[2.10]重载函数在调用时选择的依据中,(B)是错误的。

A.函数名字B.函数的返回类型

C.参数个数D.参数的类型

[2.11]在(A)情况下适宜采用内联函数。

A.函数代码小,频繁调用B.函数代码多,频繁调用

C.函数体含有递归语句D.函数体含有循环语句

[2.12]下列描述中,(C)是错误的。

A.内联函数主要解决程序的运行效率问题

B.内联函数的定义必须出现在内联函数第一次被调用之前

C.内联函数中可以包括各种语句

D.对内联函数不可以进行异常接口声明

[2.13]在C++中,关于下列设置默认参数值的描述中,(B)是正确的。

A.不允许设置默认参数值

B.在指定了默认值的参数右边,不能出现没有指定默认值的参数

C.只能在函数的定义性声明中指定参数的默认值

D.设置默认参数值时,必须全部都设置

[2,14]下面的类型声明中正确是(D)。

A.int&a[4];B.int&*p;

C.int&&q;D.inti,*p=&i;

[2.15]下面有关重载函数的说法中正确的是(B)0

A.重载函数必须具有不同的返回值类型

B.重载函数形参个数必须不同

C.重载函数必须有不同的形参列表

D.重载函数名可以不同

[2.16]关于new运算符的下列描述中,(C)是错误的。

A,它可以用来动态创建对象和对象数组

B.使用它创建的对象或对象数组可以使用运算符delete删除

C.使用它创建对象时要调用构造函数

D.使用它创建对象数组时必须指定初始值

[2.17]关于delete运算符的下列描述中,(C)是错误的。

A.它必须用于new返回的指针

B.使用它删除对象时要调用析构函数

C.对一个指针可以使用多次该运算符

D.指针名前只有一对方括号符号,不管所删除数组的维数

[2.18]写出下列程序的运行结果。

#include<iostream>

usingnamespacestd;

inti=15;

intmain()

{inti;

i=100;

::i=i+l;

cout«::i«endl;

return0;

)

答:101

[2.19]写出下列程序的运行结果。

#include<iostream>

usingnamespacestd;

voidf(int&m,intn)

{inttemp;

temp=m;

m=n;

n=temp;

)

intmain()

{inta=5,b=10;

f(a,b);

cout«a«"n«b«endl;

return0;

)

答:1010

[2.20]分析下面程序的输出结果。

#include<iostream>

usingnamespacestd;

int&f(int&i)

{i+=10;

returni;

)

intmain()

{intk=0;

int&m=f(k);

cout«k«endl;

m=20;

cout«k«endl;

return0;

)

答:

10

20

[2.21]举例说明可以使用const替代#define以消除#define的不安全性。

答:

例如#include<iostream.h>

#defineA2+4

#defineBA*3

voidmain()

(

cout<<B«endl;

)

上面程序的运行结果是14而不是18但很容易被认为是18。用const替代

#define就能得到正确结果从而消除了#define的不安全性。

Sinclude<iostream.h>

constA=2+4;

constB=A*3;

voidmain()

(

cout<<B«endl;

)

运行结果为18。

[2.22]编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci数

列的前20项,并存储到动态分配的空间中。

#include<iostream>

usingnamespacestd;

intmain()

I

inti;

int*p;

p=newint[20];

p[0]=0;

p[l]=l;

cout«*p«/,/,«*(p+l)«,/,z;

for(i=2;i<20;i++)

(

p[i]=p[i-2]+p[i-l];

cout«*(p+i)«,zzz;

)

deletep;

return0;

)

[2.23]编写一个C++风格的程序,建立一个被称为sroot()的函数,返回其参

数的二次方根。重载sroot()3次,让它返回整数、长整数与双精度数

的二次方根(计算二次方根时,可以使用标准库函数sqrt())。

Sinclude<iostream.h>

#include<math.h>

intsroot(intx)

(

return

(int)sqrt(x);

floatsroot(floatx)

return

(float)sqrt(x);

)

doublesroot(doublex)

(

return(double)sqrt(x);

)

voidmain()//test

(

inti=4;

intresultl=sroot(i);

floatj=4.0;

floatresultj=sroot(j);

doublek=4.0000;

doubleresultK=sroot(k);

I

[2.24]编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、

5分的硬币,有多少种换法?

Sinclude<iostream>

usingnamespacestd;

intmain()

(

inti,j,k;

intsum=0;

for(i=0;i<=100;i++)

for(j=0;j<=50;j++)

for(k=0;k<=20;k++)

if(i*l+2*j+5*k=100)sum++;

cout<〈〃总数为〃<<sum〈<endl;

)

[2.25]编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序

输出。要求使用变量的引用。

#include<iostream>

usingnamespacestd;

voidswap(int&x,int&y)

(

inttemp=x;

x=y;

y=temp;

)

intmain()

i

inta,b;

cin>>a»b;

if(a>b)

swap(a,b);

cout«a<<b;

return0;

第三章:

[3.7]在下面有关对构造函数的描述中,正确的是(A)。

A.构造函数可以带有返回值

B.构造函数的名字与类名完全相同

C.构造函数必须带有参数

D.构造函数必须定义,不能默认

[3.8]在声明类时,下面的说法正确的是(C)。

A.可以在类的声明中给数据成员赋初值

B.数据成员的数据类型可以是register

C.private、public>protected可以按任意顺序出现

D.没有用private、public,protected定义的数据成员是公有成员

[3.9]在下面有关析构函数特征的描述中,正确的是(C)。

A.一个类中可以定义多个析构函数

B.析构函数名与类名完全相同

C.析构函数不能指定返回类型

D.析构函数可以有一个或多个参数

[3.10]构造函数是在(B)时被执行的。

A.程序编译B.创建对象

C.创建类D.程序装人内存

[3.11]在下面有关静态成员函数的描述中,正确的是(B)0

A.在静态成员函数中可以使用this指针

B.在建立对象前,就可以为静态数据成员赋值

C.静态成员函数在类外定义时,要用static前缀

D.静态成员函数只能在类外定义

[3.12]在下面有关友元函数的描述中,正确的说法是(A)。

A.友元函数是独立于当前类的外部函数

B.一个友元函数不能同时定义为两个类的友元函数

C.友元函数必须在类的外部定义

D.在外部定义友元函数时,必须加关键字friend

[3.13]友元的作用之一是(A)。

A.提高程序的运行效率B.加强类的封装性

C.实现数据的隐藏性D.增加成员函数的种类

[3.14]以下程序运行的结果是(B)o

#include<iostream>

usingnamespacestd;

classB{

public:

B(){}

B(inti,intj)

{x=i;

y=j;

)

voidprintb()

{cout«x«n,n«y«endl;

)

private:

intx,y;

);

classA{

public:

A()

()

A(intI,intj);

voidprinta();

private:

Bc;

);

A::A(inti,intj):c(i,j)

()

voidA::printa()

{c.printb();

)

intmain()

{Aa(7,8);

a.printa();

return0;

)

A.8,9B.7,8C.5,6D.9,10

[3.15]以下程序的运行结果是(D)o

#include<iostream>

usingnamespacestd;

classA{

public:

voidset(inti,intj)

{x=i;

y=j;

)

intget_y()

{returny;

)

private:

intx,y;

);

classbox{

public:

voidset(intl,intw,ints,intp)

{length=l;

width=w;

label.set(s,p);

intget_area()

{returnlength*width;

private:

intlength,width;

Alabel;

);

intmain()

{boxb;

b.set(4,6,l,20);

cout«b.get_area()«endl;

return0;

)

A.24B.4C.20D.6

[3,16]以下程序的运行结果是(B)0

#include<iostream>

usingnamespacestd;

classSample{

public:

Sample(inti,intj)

{x=i;

y=j;

)

voiddisp()

{cout«Hdisp1H«endl;

voiddisp()const

{cout«ndisp2n«endl;

private:

intx,y;

);

intmain()

{constSamplea(l,2);

a.disp();

return0;

)

A.displB.disp2C.displdisp2D.程序编译出错

[3.17]以下程序的运行结果是(B)。

#include<iostream>

usingnamespacestd;

classR{

public:

R(intrl,intr2)

{Rl=rl;

R2=r2;

)

voidprint();

voidprint()const;

private:

intR1,R2;

);

voidR::print()

{cout«R1«","«R2«endl;

)

voidR::print()const

{cout«R1«",n«R2«endl;

intmain()

{Ra(6,8);

constRb(56,88);

b.print();

return0;

)

A.6,8B.56,88C.0,0D.8,6

[3.18]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classtoy

{public:

toy(intq,intp)

{quan=q;

price=p;

)

intget_quan()

{returnquan;

)

intget_price()

{returnprice;

)

private:

intquan,price;

);

intmain()

{toyop⑶⑵={

toy(10,20),toy(30,48),

toy(50,68),toy(70,80),

toy(90,16),toy(11,120),

for(inti=0;iv3;i++)

{cout«opfiirO].get_quan()«n,M;

cout«op[i][0].get_price()«”\n”;

cout«op[i][l].get_quan()«",n;

cout«op[i][l].get_price()«"\nn;

)

cout«endl;

return0;

)

10,20

30,48

50,68

70,80

90,16

11,120

[3.19]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classexample

{public:

example(intn)

{i=n;

cout«"Constructing\n";

)

~example()

{cout«"Destructing\n";

)

intget_i()

{returni;

private:

inti;

);

intsqr_it(exampleo)

{returno.get_i()*o.get_i();

)

intmain()

{examplex(10);

cout«x.get_i()«endl;

cout«sqr_it(x)«endl;

return0;

)

10

100

Destructing

Destructing

[3.20]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classaClass

{public:

aCIass()

{total++;

)

-aClassQ

{total—;

)

intgettotal()

{returntotal;

private:

staticinttotal;

);

intaClass::total=0;

intmain()

{aClassol,o2,o3;

cout«ol.gettotal()«"objectsinexistence\n";

aClass*p;

p=newaClass;

if(!p)

{cout«"Allocationerror\nn;

return1;

}

cout«ol.gettotal();

cout«nobjectsinexistenceafterallocation\nn;

deletep;

cout«ol.gettotal();

cout«nobjectsinexistenceafterdeletion\n";

return0;

)

3objectsinexistence

4objectsinexistenceafterallocation

3objectsinexistenceafterdeletion

[3.21]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classtest

{public:

test();

〜test(){};

private:

inti;

);

test::test()

{i=25;

for(intctr=O;ctr<10;ctr++)

{cout«"Countingatn«ctr«n\nn;

)

)

testanObject;

intmain()

{return0;

)

CountingatO

Countingatl

Countingat2

Countingat3

Countingat4

Countingat5

Countingat6

Countingat7

Countingat8

Countingat9

[3.22]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classA{

inta,b;

public:

A()

{a=0;

b=0;

cout«HDefaultconstructorcalledAn'1;

)

A(inti,intj)

{a=i;

b=j;

cout«HConstructor:a="«a«n,b="«b«endl;

)

);

intmain()

{Aa[3];

Ab[3]={A(l,2),A(3,4),A(5,6)};

return0;

)

Defaultconstructorcalled.

Defaultconstructorcalled.

Defaultconstructorcalled.

Constructor:a=l,b=2

Constructor:a=3,b=4

Constructor:a=5,b=6

[3.23]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classTest{

private:

intval;

public:

Test()

{cout«"default.n«endl;

)

Test(intn)

{val=n;

cout«nCon.H«endl;

)

Test(constTest&t)

{val=t.val;

cout«"Copycon.n«endl;

)

);

intmain()

{Testtl(6);

Testt2=tl;

Testt3;

t3=tl;

return0;

)

Con.

Copycon.

Default.

[3.24]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classN{

private:

intA;

staticintB;

public:

N(inta)

{A=a;

B+=a;

staticvoidfl(Nm);

);

voidN::fl(Nm)

{cout«',A=,,«m.A«endl;

cout<<"B=n«B«endI;

)

intN::B=0;

intmain()

{NP(5),Q(9);

N::fl(P);

N::fl(Q);

return0;

)

A=5

B=14

A=9

B=14

[3.25]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classM{

intx,y;

public:

M()

{x=y=0;

)

M(inti,intj)

{x=i;

y=j;

voidcopy(M*m);

voidsetxy(inti,intj)

{x=i;

y=j;

)

voidprint()

{cout«x«n,H«y«endl;

)

);

voidM::copy(M*m)

{x=m->x;

y=m->y;

)

voidfun(Mml,M*m2)

{ml.setxy(12,15);

m2->setxy(22,25);

)

intmain()

{Mp(5,7),q;

q.copy(&p);

fun(p,&q);

pprint();

qprint();

return0;

)

5,7

22,25

[3.26]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classM{

intA;

staticintB;

public:

M(inta)

{A=a;

B+=a;

cout«HConstructing"«endl;

)

staticvoidfl(Mm);

〜M()

{cout«"Destructing\n";

)

);

voidm)

{cout«',A=,,«m.A«endl;

cout«nB=M«B«endl;

)

intM::B=0;

intmain()

{Mp(5),QU0);

M::fl(p);

M::fl(Q);

return0;

)

Constructing

Constructing

A=5

B=15

Destructing

A=10

B=15

Destructing

Destructing

Destructing

[3.27]指出下列程序中的错误,并说明为什么。

#include<iostream>

usingnamespacestd;

classStudent{

public:

voidprintStu();

private:

charname[10];

intage;

floataver;

);

intmain()

{Studentpl,p2,p3;

pl.age=30;

return0;

}age为private不能付值

[3.28]指出下列程序中的错误,并说明为什么。

#include<iostream>

usingnamespacestd;

classStudent{

intsno;

intage;

voidprintStu();

voidsetSno(intd);

);

voidprintStuQ;

{cout«n\nSnois,,«sno«'V,;

cout«nageisn«age«n.n«endl;

)

voidsetSno(ints)

{sno=s;

)

voidsetAge(inta)

{age=a;

)

intmain()

{Studentlin;

lin.setSno(20021);

lin.setAge(20);

lin.printStuO;

}*classStudent,不存在一个叫letAge,的方式

[3.29]指出下列程序中的错误,并说明为什么。

#include<iostream>

usingnamespacestd;

classPoint{

public:

intx,y;

private:

Piont()

{x=l;y=2;

intmain()

{Pointcpoint;

cpoint.x=2;

return0;

)

构筑函数错误

[3.30]下面是一个计算器类的定义,请完成该类成员函数的实现。

classcounter{

public:

counter(intnumber);

voidincrement();〃给原值加1

voiddecrement();〃给原值减1

intgetvalue();〃取得计数器值

intprint();〃显示计数

private:

intvalue;

counter::counter(intnumber){

value=number;

voidcounter::increment(intn){

value+=n;

)

voidcounter::decrement(){

value—;

intcounter::getvalue(){

returnvalue;

voidcounter::print(){

printf("%d\n\getvalue());

)

intmain()

(

inta,b;

scanf(n%d%d",&a,&b);

counterct(a);

ct.increment(b);

ct.decrement();

ct.print();

return0;

[3.31]根据注释语句的提示,实现类Date的成员函数。

#include<iostream>

usingnamespacestd;

classDate{

public:

voidprintDate();〃显示日期

voidsetDay(intd);〃设置日的值

voidsetMonth(intm);〃设置月的值

voidsetYear(inty);〃设置年的值

private:

intday,month,year;

)

intDate::setyear(inty)

year=y;

returnyear;

intDate::setmonth(intm)

(

month=m;

returnmonth;

)

intDate::setday(intd)

(

day=d;

returnday;

)

VbidDate::printdate(){

cout«year«n.n«month«M.,,«day«endl;

);

intmain()

{DatetestDay;

testDay.setDay(5);

testDay.setMonth(10);

testDay.setYear(2003);

testDay.printDate();

return0;

)

[3.32]建立类cylinder,cylinder的构造函数被传递了两个double值,分别

表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储

在一个double变量中。在类cylinder中包含一个成员函数vol,用

来显示每个cylinder对象的体积。

#include<iostream>

usingnamespacestd;

#definePI3.14159

classCylinder

public:

Cylinder(double

radius,doubleheight):radius(radius),height(height),volume(PI

*radius*radius*height)

!

total_vol+=volume;

}

doublevol()const;

friendvoidgetParas(double&radius,double&height,double&vol,double&

total_vol,constCylinder&obj);

private:

doubleradius;//radiusdoubleheight;//heightdouble

volume;//volumeofcurrentobject

staticdoubletotal_vol;//totalvolume

);

doubleCylinder::total_vol=0;

doubleCylinder::vol()const

(

returnthis-〉volume;

)

voidgetParas(double&radius,double&height,double&vol,double&

total_vol,constCylinder&obj){//getprivatememberof

Cylinderradius=obj.radius;height=obj.height;vol=

obj.volume;total_vol=obj.total_vol;

intmainO

doublerd=0,hg=0,vol=0,total_vol=0;

Cylindercld_l(2,3.5),cld_2(5.2,7),cld_3(3.9,4.0);//3objects

getParas(rd,hg,vol,total_vol,cld_3);

cout<<z/Cylinder3:〃<Xendl;

cout«z,radius:,z<<rd<<endl;

cout«,,height:/z<<hg<<endl;

cout<<z,volume:z,«vol<<endl;

cout<<z,TotalVolume:,z«totalvol<<endl;

system("pause");

return0;

}

[3.33]构建一个类book,其中含有两个私有数据成员qu和price,将qu初

始化为1〜5,将price初始化为qu的10倍,建立一个有5个元素的

数组对象。显示每个对象数组元素的qu*price值。

ttinclude<iostream.h>

classbook

intqu,price;

public:

book(intq)

(

qu=q;price=10*q;

)

intget_qu()

{returnqu;}

intget_price()

{returnprice;}

);

voidmain()

(

bookobj[5]={1,2,3,4,5};

for(inti=0;i<5;i++)

{cout<<z,数组对象〃<<i+l<<〃的qu*price值

为:〃<<obj[i]・get_qu()*obj[i].get_price()«endl;

)

)

[3.34]修改习题3.33,通过对象指针访问对象数组,使程序以相反的顺序显

示每个对象数组元素的qu*price值。

Sinclude<iostream.h>

classbook

!

intqu,price;

public:

book(intq)

(

qu=q;price=10*q;

}

intget_qu()

{returnqu;}

intget_price()

{returnprice;}

);

voidmain()

(

bookobj[5]={1,2,3,4,5);

book*p;

p=&obj[4];

for(inti=0;i<5;i++)

{cout<<〃数组对象〃<<i+l<<〃的qu*price值

为:,z«p->get_qu()*p->get_price()«endl;

p—;

}

)

[3.35]构建一个类Stock,含字符数组stockcode口及整型数据成员quan、

双精度型数据成员price。构造函数含3个参数:字符数组na口及q、

po当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成

员stockcode,第2和第3个参数分别赋给quan>price。未设置第2

和第3个参数时,quan的值为1000,price的值为8.980成员函数

print没有形参,需使用this指针,显示对象数据成员的内容。假设

类Stock第1个对象的三个参数分别为:“600001”,3000和5.67,第

2个对象的第1个数据成员的值是“600001”,第2和第3个数据成员

的值取默认值。要求编写程序分别显示这两个对象数据成员的值。

ttinclude<iostream>

//constunsignedMAX_LEN=256;

classStock{public:

//这里用的初始化形参列表

Stock(charna[],intq=1000,doublep=8.98):stockcode(na),quan(q),

price(p)

{)

voidprint(void)

(

std::cout<<this->stockcode<<〃〃<<quan<<〃〃«price«

std::endl;

private:

char*stockcode;//或改为charstockcode[MAX_LEN];

//其中MAX_LEN定义在前为一常量:constunsignedMAX_LEN=256;

intquan;doubleprice;};

//mainfunctionintmain(intargc,char*argv口)

I

charszTestString[]="600001〃;//It'sterminatedwith'\0'

intiTestlnteger=3000;

doubledTestDouble=5.67;

StockstObjl(szTestString,iTestlnteger,dTestDouble);

StockstObj2(szTestString);

stObjl.print();

stObj2.print();

return0;

}//endmai

[3.36]编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求

输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数

和总成绩用静态数据成员表示)。

#include<stdio.h>

#include<string.h>

#defineN4

classstudent

(

intno;

charname[10];

intdegl;//语文成绩

intdeg2;//数学成绩

intdeg3;//英语成绩

staticintsuml;//语文总分

staticintsum2;//数学总分

staticintsum3;//英语总分

public:

student(intn,charna[],intdl,intd2,intd3)

{

no=n;

strcpy(name,na);

degl=dl;deg2=d2;deg3=d3;

suml+=degl;sum2+=deg2;sum3+=deg3;

}

doubleavgl(){return(suml*l.0)/N;}

doubleavg2(){return(sum2*l.0)/N;}

doubleavg3(){return(sum3*l.0)/N;}

voiddispO

(

printf(〃%4d%10s%6d%6d%6d\nzz,no,name,degl,deg2,deg3);

)

};

intstudent::suml=0;

intstudent::sum2=0;

intstudent::sum3=0;

voidmain()

(

double(student::*fp)();//定义成员函数指针

studentsi(1,,,Li,z,67,89,90);

students2(2,〃Ma〃,67,89,90);

students3⑶〃Zheng”,67,89,90);

students4(4,〃Chen〃,67,89,90);

printf(〃输出结果\n〃);

si.dispO;

s2.dispO;

s3.dispO;

s4.dispO;

fp=student::avgl;

printf(〃语文平均分%g\n,z,(si.*fp)());

fp二student::avg2;

printfC数学平均分%g\n〃,(sl.*fp)());

fp=student::avg3;

printfC/英语平均分%g\n,z,(si.*fp)());

第四章:

[4.8]使用派中类的主要原因是(c)。

A.提高代码的可重用性

B.提高程序的运行效率

C.加强类的封装性

D.实现数据的隐藏

[4.9]假设已经定义好了一个类student,现在要定义类derived,它是从

student私有派生的,定义类derived的正确写法是(c)。

A.clasederived::studentprivate{,,,};

B.clasederived::studentpublic{,•,};

C.clasederived::privatestudent;

D.clasederived::publicstudent{,,,};

[4.10]在多继承构造函数定义中,儿个基类构造函数用(c)分隔。

A.:B.;C.,D.::

[4.11]设置虚基类的目的是(b)0

A.简化程序B.消除二义性C.提高运行效率D.减少目标代码

[4.12]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classBl(

public:

Bl(inti)

{bl=i;

cout«"ConstructorBl."«endl;

}

voidPrint()

{cout«bl«endl;

private:

intbl;

classB2{

public:

B2(inti)

{b2=i;

cout«,,ConstructorB2."«endl;

)

voidPrint()

{cout«b2«endl;

)

private:

intb2;

);

classA:publicB2,publicBl{

public:

A(inti,intj,int1);

voidPrint();

private:

inta;

);

A::A(inti,intj,intl):Bl(i),B2(j)

{a=l;

cout«"ConstructorA."«endl;

)

voidA::Print()

{Bl::Print();

B2::Print();cout«a«endl;

intmain()

{Aaa(3,2,l);

aa.Print();

return0;

)

3

2

1

[4.13]写出下面程序的运行结果。

#include<iostream>

usingnamespacestd;

classMain{

protected:

char*mainfood;

public:

Main(char*name)

{mainfood=name;

}

);

classSub{

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论