




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C++程序設(shè)計(jì)習(xí)題集
上機(jī)題:先定義“高度”類Hight和“圓”類Circle,再由Hight和
Circle多重派生出“圓柱體”類Cylinder。在主函數(shù)中定義一個(gè)圓柱
體對(duì)象,調(diào)用成員函數(shù)求出圓柱體的體積和表面積。
第一部分:
選擇填空題
1.類成員缺省的訪問(wèn)特征是/一結(jié)構(gòu)體缺省的訪問(wèn)權(quán)限是_B_.
A.protected
B.public
C.private
D.friend2.下列關(guān)于類的成員的訪問(wèn)特征的描述中,正確的—
A.必須首先說(shuō)明具有私有特征的成員
B.數(shù)據(jù)成員必須說(shuō)明成私有的,成員函數(shù)必須說(shuō)明成私有的
C.每個(gè)成員前都必須有標(biāo)明訪問(wèn)特性的關(guān)鍵字
D.在同一類中,說(shuō)明訪問(wèn)特性的關(guān)鍵字可以多次使用
3.關(guān)于類和對(duì)象,下列說(shuō)法不正確的是—
A.類與對(duì)象的關(guān)系類似于數(shù)據(jù)類型與變量的關(guān)系
B.對(duì)象是類的一個(gè)實(shí)例
C.任何一個(gè)對(duì)象必定屬于一個(gè)特定的類
D.一個(gè)類只能有一個(gè)對(duì)象
4.關(guān)于類的一個(gè)成員函數(shù),下列說(shuō)法中正確的是()
A.必須在類內(nèi)定義
B.一定是內(nèi)聯(lián)函數(shù)
C.不可以重載
D.可以設(shè)置參數(shù)的缺省植
5.以下不可以作為類的成員的是()
A.自身類對(duì)象的指針
B.自身類的對(duì)象
C自身類對(duì)象的引用
D.另一個(gè)類對(duì)象的引用
6.已知一個(gè)類,類名為A,可以定義A類的對(duì)象或定義A類的指針,下
列一定不正確的形式為—
A.Aal
B.Aa2(16)
C.AA3()
D.A*P=newA
7.已知一個(gè)類COMPLEX,有下述兩行:
Complexc;//A
Complexc()//B
以下描述中錯(cuò)誤的是一
A.A行定義了一個(gè)對(duì)象,并調(diào)用缺省的構(gòu)造函數(shù)
B.B行定義了一個(gè)對(duì)象,并調(diào)用缺省的構(gòu)造函數(shù)
C.B行是一個(gè)函數(shù)的原型說(shuō)明,函數(shù)名為C,函數(shù)沒(méi)有參數(shù),返回值
類型是Complex
D.A行和B行兩行的意義不一樣
8.設(shè)有下列對(duì)象定義;
classA
(
public:
inta,b;
}al={1,2},a2,a3;
class
(
public:
inta,b;
}a4;
則以下正確的定義或賦值形式是一
A.a3=a2=al;
B.a4=al;
C.A*p=&a4;
D.A&re=a4
9.下列描述中,—不是構(gòu)造函數(shù)的特征
A.構(gòu)造函數(shù)可以重載
B.必須顯示為類定義一個(gè)構(gòu)造函數(shù)
C.無(wú)須為構(gòu)造函數(shù)指定返回值類型
D.構(gòu)造函數(shù)在產(chǎn)生對(duì)象時(shí)由系統(tǒng)自己調(diào)用
10.下列有關(guān)析構(gòu)函數(shù)的描述中,正確的是—
A.析構(gòu)函數(shù)的定義只能在類體內(nèi)
B.析構(gòu)函數(shù)可以有一個(gè)或多個(gè)參數(shù)
C.析構(gòu)函數(shù)是在撤消對(duì)象時(shí),系統(tǒng)自動(dòng)調(diào)用的
D.析構(gòu)函數(shù)可以重載
11.以下關(guān)于構(gòu)造函數(shù)和析構(gòu)函數(shù)的描述中錯(cuò)誤的是一
A.用戶不定義缺省的構(gòu)造函數(shù),則系統(tǒng)自動(dòng)生成一個(gè)缺省的構(gòu)造函
數(shù)
B.若用戶不定義拷貝的構(gòu)造函數(shù),則系統(tǒng)自動(dòng)生成一個(gè)拷貝的構(gòu)造
函數(shù)
C.若用戶不定義析構(gòu)函數(shù),則系統(tǒng)自動(dòng)生成一個(gè)析構(gòu)函數(shù)
D.以上A錯(cuò)誤,而B,C正確
12.定義復(fù)數(shù)類如下;
classComplex
{doublereal,image;
public:
Complex(doubler=0,doublei=0){real=r;image=i;}
Complex(Complex&c){real=c.real;image=c.image;}};
若有Complexcl;//A
Complexc2(3,5);//B
Complexc3(c2);//C
c2=cl;//D
則下列描述中正確的是—
A.C行和D行均調(diào)用了拷貝構(gòu)造函數(shù)
B.C行調(diào)用了拷貝構(gòu)造函數(shù)
C.B行調(diào)用了拷貝構(gòu)造函數(shù)
D.A行調(diào)用了拷貝構(gòu)造函數(shù)
13.classA
{intx,inty;
public;
A(inta,intb){x=a;y=b;}
voidshow(){cout?x<
voidset(inta,intb){x=a;y=b;}
);
voidmain()
{Aobj;
obj.set(3,5);
obj.show();
)
對(duì)上述過(guò)程段,以下說(shuō)法中,正確的是—
A.編譯時(shí)報(bào)錯(cuò),程序無(wú)法運(yùn)行
B.編譯時(shí)無(wú)錯(cuò),運(yùn)行正常,輸出3,5
C.編譯無(wú)錯(cuò),運(yùn)行時(shí)報(bào)錯(cuò)
D.編譯時(shí)報(bào)警告錯(cuò),但運(yùn)行正常,輸出3,5
14.在下列程序中,C類的數(shù)據(jù)成員中有一個(gè)A類對(duì)象和一個(gè)B類
對(duì)象。
^include
classA
{inta;
public:
A(intx=10){a=x;}
intGetA(){returna;}
);
classB
{intb;
public:
B(intx){b=x;}
intGetB(){returnb;}
);
classC{
intc;
Aaobj;
Bbobj;
public:
C(intx,intyjntz):aobj(y),bobj(z)//E
{c=x;}
voidPrint()
{cout?aobj.geta()<
};
voidmain()
(
Ccobj(3,6,8);
cobj.Print();
)
程序在E行定義了構(gòu)造函數(shù),如果將該行改為下列中選項(xiàng)時(shí),
便會(huì)出現(xiàn)編譯錯(cuò)誤C
A.C(intx,inty,intz):aobj(z),bobj(y)
B.C(intxjnty,intz):bobj(z)
C.C(intxjnty,intz):aobj(y)
D.C(intxjnty,intz):aobj(x),bobj(y+z)
二.找錯(cuò),運(yùn)行結(jié)果
1.指出下列程序的錯(cuò)誤:
#include
classA
{intx,y;
public:
A(){x=y=O;}
A(inti=0Jntj=O){x=i,y=j;}
voidshow(){cout?x<
);
voidmain()
(
Aal,a2(6,8);錯(cuò)誤:〃該程序有兩個(gè)默認(rèn)的構(gòu)造函數(shù)
al.show();
a2.show();
)
2.寫出下列程序的運(yùn)行結(jié)果:
#include
classA
{intx,y;
public:
A(){x=y=0;cout?z,Defaultconstructorcalled.\n;}
A(inti,intj)
{x=i,y=jcout?=""p="">
~A(){show();cout?//Destructorcalled.\n,/;}
voidshow(){cout?x<
);
voidmain()
{A31,32(6,8);
al.show();
a2.show();
)
Defaultconstructorcalled.
constructorcalled.
0,0
6,8
6,8
Destructorcalled.
0,0
Destructorcalled.
3.根據(jù)以下兩個(gè)方框中的內(nèi)容,寫出兩個(gè)版本的運(yùn)行結(jié)果:
#include
classA
{intx,y;
public:
A(inti=0Jntj=0)
{x=i;y=j;
show();
cout?,/Constructorcalled.\nw;
)
A(A&a);
~A(){show();cout?,/Destructorcalled.\n/#;}
voidset(inti=0,intj=O){x=i;y=j;}
voidshow(){cout?x<
};
A::A(A&a)
{x=a.x;y=a.y;show();cout?//Copyconstructorcalled.\n/,;}
Afun(Aa)
{a.show();cout?endl;<p="">
a.set(3,7);returna;}A&fun(A&a){a.show();
cout?endl;<p="">
a.set(3,7);
returna;}
voidmain()
{Aal,a2(6,8),a3(a2),a4;
a4=fun(a3);
a4.Show();
a3.Show();
cout?endl;<p="">
)
左邊的答案:
0,0,Constructorcalled.
6,8/Constructorcalled.
6,8,Copyconstructorcalled.
0,0,Constructorcalled.
6,8,Copyconstructorcalled.
6,8,
3,7,Copyconstructorcalled.
3,7,Destructorcalled.
3,7,Destructorcalled.
3,7,6,8,
3,7,Destructorcalled.
6,8,Destructorcalled.
6,8,Destructorcalled.
0,0,Destructorcalled.
右邊的答案:
0,0,Constructorcalled.
6,8,Constructorcalled.
6,8,Copyconstructorcalled.
0,0,Constructorcalled.
6,8,
3,7,3],
3,7,Destructorcalled.
3,7,Destructorcalled.
6,8,Destructorcalled.
0,0,Destructorcalled.
4,寫出下列程序的運(yùn)行結(jié)果#indudeclassA
{intx,y;
public:
A(inti=0Jntj=0)
/x
{x=i/y=j;Show();cout?constructorcalled.\n"}~A(){Show
();cout?wdestructorcalled.\n”;}
voidSet(inti=0,intj=0){x=i,y=j;}
voidShow(){cout?x<
);
A&fun(A&a)
{a.Show();
cout?endl;<p="">
a.Set(3,7);
returna;
)
voidmain()
{Aal,a2(6,8)za3(a2),a4=al;
a4=fun(a3);
a4.show();
cout?endl;<p="">
)
0,0,constructorcalled.
6,8,constructorcalled.
6,8,
3,7,
3,7,destructorcalled.
3,7,destructorcalled.
6,8,destructorcalled.
0,0,destructorcalled.
5.寫出下列程序的運(yùn)行結(jié)果
#include
classComplex
{intreal,image;
public:
Complex(intx=0,inty=0)
{real=x;image=y;
Show();
cout?//construct\nw;
)
~Complex()
{Show();cout<
voidShow()
{cout<
voidPrint()
{Show();cout?endl;}<p="">
};
voidmain()
{Complexc[5]={3,Complex(4),Complex(),Complex(5,6)};for(int
i=0;i<5;i++)c[i].Print();
cout?wexitmain()\n";
)
(3,0)construct
(4,0)construct
(O,0)construct
(5,6)construct
(O,0)construct
(3,0)
(4,0)
(0,0)
(5,6)
(0,0)
exitmain()
(O,O)destruct
.(5,6)destruct
.(O,O)destruct
.(4,0)destruct
.(3,0)destruct
6.寫出下列程序的運(yùn)行結(jié)果
#include
classTest
{intx;
public:
Test(inti=0){x=i;}
voidprint()
{cout?x<
);
voidmain()
{Testtl(8),t2(9);
cout?&tl<
若A行的輸出是0x0012FF7C,0X0012FF78,則B行和C行的輸出是_8.
0012FF7C/9.0X0012FF78
7.寫出下列程序的運(yùn)行結(jié)果
#include
classList
(
inta[10],n;
public:
List(inta[lOIJntn)
List::n=n;//List::n表示數(shù)據(jù)成員n,以便與參數(shù)n區(qū)別
for(inti=0;i<n;i++)<=n"p="">
List::a[i]=a[i];
)
voidCopy(ListL)
{if(this==&L)
cout?wsame1notcopy!\n";
else
{n=L.n;
for(inti=0;i<n;i++)<p="">
a[i]=L.a[i];
cout?wdifferent!copied!\n";
)
)
voidShow()
{for(inti=0;i<n-l;i++)cout?a[i]<
cout?a[i]?endl;<p="">
)
);
voidmain()
{intal[10]={l,2,3J,a2[10]={4,5,6,7,8};
ListLI(al,3),L2(a2,5);
Ll.Show();L2.Show();
L2.Copy(Ll);
LI.Show();
L2.Show();
LI.Copy(LI);
L2.Copy(L2);
Ll.Show();
L2.Show();
)
1.2.3
4.5.6.7.8
different!copied!
1.2.3
1.2.3
different!copied!
different!copied!
1.2.3
1.2.3
第二部分:
1.下列各函數(shù)不是類成員函數(shù)
A.構(gòu)造函數(shù)
B.析構(gòu)函數(shù)
C.友員函數(shù)
D.拷貝初始化函數(shù)
2.下列有關(guān)靜態(tài)數(shù)據(jù)成員的描述中.—是錯(cuò)誤的
A.說(shuō)明靜態(tài)數(shù)據(jù)成員時(shí),前面要加修飾符static
B.靜態(tài)數(shù)據(jù)成員要在類體外進(jìn)行初始化
C.引用靜態(tài)數(shù)據(jù)成員時(shí),要在靜態(tài)數(shù)據(jù)成員名前加〈類名〉和作用域
運(yùn)算符
D.靜態(tài)數(shù)據(jù)成員不是所屬類的所有對(duì)象共有的
3.以下關(guān)于友員函數(shù)的描述錯(cuò)誤的是—
A.友員函數(shù)不是成員函數(shù)
B.友員函數(shù)加強(qiáng)了類的封裝性
C.友員函數(shù)可以訪問(wèn)所屬類的私有成員
D.友員函數(shù)的作用是提高程序的運(yùn)行效率
4.關(guān)于成員函數(shù)特征的下列描述中錯(cuò)誤的是—
A.成員函數(shù)一定是內(nèi)聯(lián)函數(shù)
B.成員函數(shù)可以重載
C.成員函數(shù)可以設(shè)置參數(shù)的缺省值
D.成員函數(shù)可以是靜態(tài)的
5.寫出以下程序的運(yùn)行結(jié)果
#include
classCount
staticintcount;
public:
Count(){count++;cout?count?/\tp=""
staticintGetC(){returncount;}
^CountJ){count-;cout?count?〃\t";}<二""p="n>
);
intCount::count;
voidmain()
(
Countcl,c2,c3,c4;
cout?endl<?endl;<=n"p="">
cout?/,exitmain\n";
)
1234
4
exitmain
3210
題8.分析以下程序執(zhí)行的結(jié)果
#include
classSample
intA;
staticintB;
public:
Sample(inta){A=a,B+=a;}
staticvoidfunc(Samples);
);
voidSample::func(Samples)
(
cout?"A=,,?s.a?",b="?b?endl;<p=n">
)
intSample::B=0;
voidmain()
(
Samplesl(2),s2(5);
Sample::func(sl);
Sample::func(s2);
}
解:
本題說(shuō)明了靜態(tài)成員函數(shù)的使用方法。其中的數(shù)據(jù)成員B是靜態(tài)數(shù)
據(jù)成員,求B之值是在構(gòu)造函數(shù)中進(jìn)行的。所以輸出為:
A=2,B=7
A=5,B=7
注意:靜態(tài)成員函數(shù)與靜態(tài)數(shù)據(jù)成員一樣,也不是對(duì)象成員。靜態(tài)成
員函數(shù)的調(diào)用不同于普通的成員函數(shù)。在靜態(tài)成員函數(shù)的實(shí)現(xiàn)中,引
用類的非靜態(tài)數(shù)據(jù)成員是通過(guò)對(duì)象進(jìn)行的,如本題中s.A,引用類的
靜態(tài)數(shù)據(jù)成員是直接進(jìn)行的,如本題中的B。
#include
classSample
(
intx,y;
public:
Sample(){x=y=O;}
Sample(inta,intb){x=a;y=b;}
voiddisp()
(
cout?"x="?x?"/y="?y?endl;<p="">
)
);
voidmain()
(
Samplesl,s2(l,2),s3(10,20);
Sample*pa[3]={&sl/&s2,&s3};
for(inti=0;i<3;i++)
pa[i]->disp();
)
解:
本題說(shuō)明對(duì)象指針數(shù)組賦初值方法。定義三個(gè)對(duì)象和一個(gè)對(duì)象指針數(shù)
組,通過(guò)賦初值使后者的元素分別指向前面三個(gè)對(duì)象,然后使用for
語(yǔ)句循環(huán)調(diào)用各個(gè)對(duì)象的disp()成員函數(shù)。所以輸出為:
x=0,y=0
x=l,y=2
x=10,y=20
題7.分析以下程序的執(zhí)行結(jié)果
#include
classSample
(
intn;
staticintsum;
public:
Sample(intx){n=x;}
voidadd(){sum+=n;}
voiddisp()
(
cout?"n="?n?"/sum="?sum?endl;<p="">
)
);
intSample::sum=O;//靜態(tài)數(shù)據(jù)成員賦初值
voidmain()
(
Samplea(2),b(3),c(5);
a.add();
a.dispf);
b.add();
b.disp();
c.add();
c.disp();
)
解:
本題說(shuō)明靜態(tài)數(shù)據(jù)成員的使用方法。在類中定義了一個(gè)靜態(tài)數(shù)據(jù)成員
sum,在main。之前給它賦初值0,然后在main。中定義了三個(gè)對(duì)象,
并執(zhí)行各自的add。和disp()方法。所以輸出為:
n=2/sum=2
n=3,sum=5
n=5/sum=10
注意:靜態(tài)數(shù)據(jù)成員脫離具體的對(duì)象獨(dú)立存在,其存儲(chǔ)單元不是任何
對(duì)象存儲(chǔ)空間的一部分,但邏輯上所有對(duì)象都共享這一存儲(chǔ)單元,對(duì)
靜態(tài)數(shù)據(jù)成員的任何操作都會(huì)訪問(wèn)這一存儲(chǔ)單元,從而影響這一存儲(chǔ)
單元的所有對(duì)象。所以說(shuō)靜態(tài)數(shù)據(jù)成員不是對(duì)象成員,在引用時(shí)不需
要用對(duì)象名。
03.編寫程序,調(diào)用傳遞引用的參數(shù),實(shí)現(xiàn)兩個(gè)字符串變量的交換。
例如開始:
char*ap="hello";
char*bp="howareyou?";
交換的結(jié)果使得ap和bp指向的內(nèi)容分別為:
ap:"howareyou?"
bp:"hello"
解:
本題使用引用調(diào)用(實(shí)現(xiàn)由于字符串指針本身就是地址,這里可不使
用引用參數(shù),效果是一樣的)。
程序如下:
#include
#include
voidswap(char*&x,char*&y)//引用作為參數(shù)
(
char*temp;
temp=x;x=y;y=temp;
)
voidmain()
char*ap="hello";
char*bp="howareyou?";
cout?"ap:,,?ap?endl;<p="">
cout?"bp:n?bp?endl;<p="">
swap(ap,bp);
cout?"swapap,bp"?endl;<p="">
cout?"ap:"?ap?endl;<p="">
cout?"bp:"?bp?endl;<p="">
)
本程序的執(zhí)行結(jié)果如下:
ap:hello
bp:hoeareyou?
swapap,bp
ap:howareyou?
bp:hello
題2.分析以下程序的執(zhí)行結(jié)果
#include
voidmain()
(
inta[]={10/20/30,40}/*pa=a;
int*&pb=pa;
pb++;
cout?*pa?endl;<p="">
)
解:
pa為數(shù)組的指針,首先指向a[0],pb是pa的引用,當(dāng)執(zhí)行pb++時(shí),
也使pa指向了a[l],所以輸出為:20
題3.分析以下程序的執(zhí)行結(jié)果
#include
classSample
(
intx;
public:
Sample。。;
Sample(inta){x=a;}
Sample(Sample&a){x=a.x+++10;}
voiddisp(){cout?"x="?x?endl;}<p="">
);
voidmain()
(
Samplesl(2),s2(sl);
sl.disp();
s2.disp();
)
解:
Sample類的Sample(Sample&a)構(gòu)造函數(shù)是一個(gè)拷貝構(gòu)造函數(shù),將a
對(duì)象的x增1然后加上10后賦給當(dāng)前對(duì)象的x,由于a是引用對(duì)象,
所以輸出為:
x=3〃++運(yùn)算的結(jié)果
x=12//2+10
題4.分析以下程序的執(zhí)行結(jié)果
#include
classSample
(
intx,y;
public:
Sample(){x=y=0;}
Sample(intijntj){x=i;y=j;}
voidcopy(Sample&s);
voidsetxy(intijntj){x=i;y=j;}
n
voidprint(){cout?"x="?x?",y="?y?endl;}<p=">
);
voidSample::copy(Sample&s)
(
x=s.x;y=s.y;
)
voidfunc(Samplesi,Sample&s2)
sl.setxy(10,20);
s2.setxy(30,40);
)
voidmain()
(
Samplep(l,2),q;
q.copy(p);
func(p,q);
pprint();
q.print();
)
解:
本題說(shuō)明對(duì)象引用作為函數(shù)參數(shù)的作用。Sample類中的copy()成員
函數(shù)進(jìn)行對(duì)象拷貝c在main()中先建立對(duì)象p和q,p與q對(duì)象的x,y
值相同,調(diào)用func()函數(shù),由于第2個(gè)參數(shù)為引用類型,故實(shí)參發(fā)生改
變;而第1個(gè)參數(shù)不是引用類型,實(shí)參不發(fā)生改變。所以輸出為:
x=l,y=2
x=30,y=40
03.編寫一個(gè)對(duì)具有n個(gè)元素的數(shù)組x[]求最大值的程序,要求將求最
大值的函數(shù)設(shè)計(jì)成函數(shù)模板。
解:
將max()函數(shù)設(shè)計(jì)成一個(gè)函數(shù)模板。
本題程序如下:
#include
template
Tmax(Tx[],intn)
(
inti;
Tmaxv=x[O];
for(i=l;i<n;i++)<p=,,">
if(maxv<x[i])<p="">
maxv=x[i];
returnmaxv;
)
voidmain()
(
inta[]={4,5,2,8,9,3};
doubleb[]={356.7,2,5.2,9.2};
cout?"a數(shù)組最大值:"v<max(a,6)<<endl;<p="">
cout?"b數(shù)組最大值:"<<max(b,5)?endl;<p=H">
)
本程序的執(zhí)行結(jié)果如下:
a數(shù)組最大值:9
b數(shù)組最大值:9.2
04.編寫一個(gè)使用類模板對(duì)數(shù)組進(jìn)行排序、查找和求元素和的程序。
解:
設(shè)計(jì)一個(gè)類模板templateclassArray,用于對(duì)T類型的數(shù)組進(jìn)行排序、
查找和求元素和,然后由此產(chǎn)生模板類Array和Array。
本題程序如下:
#include
#include
template
classArray
(
T*set;
intn;
public:
Array(T*data,inti){set=data;n=i;}
?Array(){}
voidsort();//排序
intseek(Tkey);//查找指定的元素
Tsum();//求和
voiddisp();//顯示所有的元素
);
template
voidArray::sort()
(
intij;
Ttemp;
for(i=l;i<n;i++)<p="">
for(j=n-l;j>=i;j-)
if(set[j-l]>set[j])
(
temp=set[j-l];set[j-l]=set[j];set[j]=temp;
)
)
template
intArray::seek(Tkey)
(
inti;
for(i=0;i<n;i++)<p=n">
if(set[i]==key)
returni;
return-1;
)
template
TArray::sum()
(
Ts=O;inti;
for(i=0;i<n;i++)<p="">
s+=set[i];
returns;
)
template
voidArray::disp()
(
inti;
for(i=0;i<n;i++)<p="">
cout?set[i]?"";<=""p="">
cout?endl;<p="">
)
voidmain()
(
ima[]={6,3,8,l,9,4,7,5,2};
doubleb[]={2.3,6.l,L5,8.4,6.7,3.8};
Arrayarrl(a,9);
Arrayarr2(b,6);
cout?"arrl:"?endl;<p="">
cout?"原序歹U:“;arrl.disp();
cout?"8在arrl中的位置:"<<airLseek(8)<<endl;arrl.sort();<=""
cout?"排序后:";arrl.disp();
cout?"arr2:"?endl;<p="">
cout?"原序歹arr2,disp();
cout?"8.4在arr2中的位置:"<<arr2.seek(8.4)?endl;arr2.sort();<=""
cout?"排序后arr2.disp();
)
本程序的執(zhí)行結(jié)果如下:
arrl:
原序列:638194752
8在anl中的位置:2
排序后:123456789
arr2:
原序歹2.36.11.58.46.73.8
8.4在arr2中的位置:3
排序后:1.52.33.86.16.78.4
題1.分析以下程序的執(zhí)行結(jié)果
#include
classSample
intn;
public:
Sample(){}
Sample(intm){n=m;}
friendvoidsquare(Sample&s)
(
s.n=s.n*s.n;
)
voiddisp()
(
cout?"n="?n?endl;<p="">
)
};
voidmain()
(
Samplea(10);
square(a);
a.dispO;
)
解:
本題應(yīng)用友元函數(shù)修改對(duì)象的數(shù)據(jù)成員。square。是一個(gè)友元函數(shù),
它將引用對(duì)象的n值進(jìn)行平方計(jì)算。
所以輸出為:100
題2.分析以下程序的執(zhí)行結(jié)果
#include
classB;
classA
friendB;
voiddisp(){cout?i?endl;}<p="">
);
classB
(
public:
voidset(intn)
Aa;
a.i=n;〃i是對(duì)象a的私有數(shù)據(jù)成員,在友元類可以使用
a.disp();〃disp()是對(duì)象a的私有成員函數(shù),在友元類可以使用
)
);
voidmain()
Bb;
b.set(2);
)
解:
本題說(shuō)明友元類的設(shè)計(jì)方法。這里將類B設(shè)置為類A的友元類,因
此,在設(shè)計(jì)類B時(shí)可以直接使用類A的私有數(shù)據(jù)成員和成員函數(shù)。
所以輸出為:2
題3.分析以下程序的執(zhí)行結(jié)果
#include
classteacher;
classstudent
(
char*name;
public:
student(char*s){name=s;}
friendvoidprint(student&zteacher&);
);
classteacher
char*name;
public:
teacher(char*s){name=s;}
friendvoidprint(student&,teacher&);
);
voidprint(student&a,teacher&b)
(
cout?"thestudentis:"<?endl;<=""p="">
cout?"theteacheris:H<?endl;<=""p="">
)
voidmain()
(
students(MLiHu");
teachert("WangPing");
print(s,t);
)
解:
student和teacher類共用一個(gè)友元函數(shù)的實(shí)現(xiàn)。
所以輸出為:
thestudentisLiHu
theteacherisWanPing
第三部分:
一,選擇填空題
1.派生類對(duì)象可直接訪問(wèn)基類中的_A_;在派生類的成員函數(shù)中,不
可以直接訪問(wèn)的是_B_
A.公有繼承的公有成員
B.公有繼承的私有函數(shù)
C.公有繼承的保護(hù)成員
D.私有繼承的公有成員
2.關(guān)于繼承中二義性的描述錯(cuò)誤的是一
A.一個(gè)派生類的兩個(gè)基類中有同名成員,在派生類中對(duì)這個(gè)成員的
訪
問(wèn)可能出現(xiàn)二義性
B.解決二義性最常用的方法是對(duì)成員名的限定法.
C基類和派生類中同時(shí)出現(xiàn)的同名函數(shù),也存在二義性問(wèn)題
D.派生類D是從基類B和基類C多重派生出來(lái)的,而B類和C類
又有一個(gè)共同的基類A,在派生類D中,訪問(wèn)基類A的成員時(shí)也可能
出現(xiàn)二義性
3.以下說(shuō)法錯(cuò)誤的是—
A.單一繼承情況下,派生類對(duì)基類的同名成員的訪問(wèn)不會(huì)出現(xiàn)二義
性
B.多繼承情況下,基的構(gòu)造函數(shù)的執(zhí)行順序取決與定義派生類時(shí)所
指定的個(gè)基類的繼承順序
c.多繼承函數(shù)情況E基類的構(gòu)造函數(shù)的執(zhí)行順序取決于定義派生類
構(gòu)造函數(shù)時(shí),在其后所列出的多個(gè)基類構(gòu)造函數(shù)的順序
D.虛基類是用來(lái)解決多繼承中公共基類在派生類中只產(chǎn)生一個(gè)基類
成員的拷貝問(wèn)題
4.設(shè)有以下類的定義
#include
classA
(
protected:
inta;
public:
A(inti=0){a=i;}
intGeta(){returna;}
);
classB:publicA
(
Aal;
public:
______________________________//B類構(gòu)造函數(shù)的定義voidShow()
{cout<
);
則關(guān)于B類構(gòu)造函數(shù),下列定義形式一定不確定的是
A.可缺省
B.B(inti=O,intj=O):A(i),al(j){}
C.B(inti=O,intj=O):A(i),A(i){}
D.B(inti=OJntj=O):A(i){}
5.設(shè)有如下類的定義:
classA
{inta;
public:
A(inti=O){a=i;}
};
classB:publicA
(
intb;
public:
B(inti=OJntj=O):A(j)
{b=i;}
};
則下列語(yǔ)句組中錯(cuò)誤的是一
A.A*pa=newB;
B.Aal=B(2,9);
C.Bbl(l,2);
D.Aal(8);
A*pal=&bl;Bb2=al;
二,寫出以下程序的運(yùn)行結(jié)果.并完善程序
l.#include
classA
{inta;
public:
A(inti=10){a=i;}
voidshow(){cout?,JA::a=w?a?endl;}};<=""p="">
classB:virtualpublicA
{intb;
public:
B(inti=20):A(8)
{b=i;}
voidshow(){cout?/,B::b=/,?b?endl;}};<=""p="">
classC:publicvirtualA
{intc;
public:
C(inti=30):A(8)
{C=i;}
voidshow(){cout?,/C::c=>/<<c?endl;}<p="">
);
classD:publicB,publicC
{intd;
public:
D(inti=l,intj=2,intk=3):B(i),c(j){d=k;}
voidshow()
(
A::show();
B::show();
C::show();
cout?,,D::d=,,?d?endl;<p="">
)
);
voidmain()
(
Ddl,d2(66,77,99);
dl.show();
d2.show();
)
A::a=10
B::b=l
C::c=2
D::d=3
A::a=10
B::b=66
C::c=77
D::d=99
2.#include
classA
{protected:
intx;
public:
A(inti=10){x=i;}
voidshow(){cout?wA::x=n?x?=""p=""
classB:publicA
{protected:
intx;
public:
B(inti=20)
{x=i}
voidshow(){cout?wB::x=p="">
classC:publicB
{intc;
public:
C(inti=30){c=i;}
voidprint()
{cout?wA::x="<?=""p="">
?=""p="">cout?,,B::x=//<?endl<=""p=""
?=""p="">show();
?="np="">cout?x?endl;<p="">
?='H,p=,H,>}
?=,M,p="">};
?="np="">voidmain()
?=""p="">{Cc;
?=,"'p="">c.print();
?=,,,,p="">c.show();
?=nnp=n">}
?=nnp=n">A::x=10
?=,,Mp=n">B::x=20
?=nnp="">B::x=
?="np="">20
?=,'"p="">B::x=
?=??p=?">3分析以下程序:
?=',np="">#include<iostream.h>
?=nnp="">dassA
?=,,,,p="">{inta;//lfi:
?=,,np=n">public:
?="np="">A(inti=0)
?=,,nP="">{a=i;}//2行};
?=,,Mp=,,Hstyle="color:rgb(51,51,51);font-family:Arial,宋體;
font-size:18px;font-style:normal;font-variant:normal;font-weight:
normal;letter-spacing:normal;line-height:32.4px;orphans:auto;
text-align:start;text-indent:Opx;text-transform:none;white-space:
normal;widows:1;word-spacing:Opx;-webkit-text-stroke-width:Opx;
background-color:rgb(255,255,255);">
?=""p=""style="text-align:center;color:rgb(O,102,0);border:Ipx
solidrgb(234,234,234);padding:3px16px;text-decoration:none;
margin:Opx5px;display:inline-block;text-indent:0em;">
classB
{intb;〃3行
public:
B(inti=0)
{b=i;}〃4行
);
classC:publicA
{Bbl;//5行
public:
voidshow()
cout?a<
cout?
)
};
voidmain()
{Ccl;cl.show();}
程序編譯時(shí)出現(xiàn)的語(yǔ)法錯(cuò)誤的行為;
(1)―第1行,最簡(jiǎn)單的修改方法是_public最合理的
修改方法是_protected
(2)―第3行,最簡(jiǎn)單的修改方法是—public最合理的
修改方法是_public
,,最簡(jiǎn)單〃的原則是做最少的改動(dòng)。〃最合理〃的原則是在保護(hù)數(shù)據(jù)成員
說(shuō)(即不能通過(guò)該類的對(duì)象直接訪問(wèn)數(shù)據(jù)成員)的前提下,做最少的
改動(dòng)。
4分析以下程序,根據(jù)輸出結(jié)果完善程序,
要求:(1)在主函數(shù)中不可以通過(guò)對(duì)象訪問(wèn)類中的所有數(shù)據(jù)成員。
(2)程序的運(yùn)行結(jié)果為;3,6,9o
#include
classA
(
⑴protected:〃最合理的訪問(wèn)特征
inta;
public;
A(inti=0){a=i;}
};
classB
(
(2)_public:〃最合理的訪問(wèn)特征
intb;
public:
B(inti=0){b=i;}
);
classC:publicA
(
intc;
Bbl;
public:
(3LC(inti,intj,intk)_:A(i),bl(j)一〃根據(jù)運(yùn)行結(jié)果定義構(gòu)造函數(shù)
{(4)_c=k;
voidshow()
(
(5)_cout?a<
)
);
voidmain()
cc1(3,6,9,);
cl.show();
)
第四部分:
—、簡(jiǎn)答題
1.什么是運(yùn)算符重載,實(shí)現(xiàn)運(yùn)算符重載有幾種方法?如何實(shí)現(xiàn)?
2.如何實(shí)現(xiàn)本類對(duì)象與其他類對(duì)象之間的相互類型轉(zhuǎn)換?
3.什么是虛函數(shù)?什么是純虛函數(shù)?他們的作用是什么?
4.試比較虛函數(shù)與虛基類的概念及作用
二、選擇題
1、編譯器無(wú)法根據(jù)下列條件中的區(qū)分調(diào)用哪一個(gè)重載函數(shù)
A.函數(shù)參數(shù)的個(gè)數(shù)B.函數(shù)參數(shù)的類型
C.函數(shù)的返回值類型
D.是不是有參數(shù)
2.下列函數(shù)中不能重載
A.成員函數(shù)
B.非成員函數(shù)
C.構(gòu)造函數(shù)
D.析構(gòu)函數(shù)
3.下列關(guān)于重載函數(shù)的描述錯(cuò)誤的是
A.重載函數(shù)不允許使用缺省參數(shù)
B.編譯器根據(jù)重載函數(shù)的參數(shù)表抉擇執(zhí)行不同的重載函數(shù)
C.不要使用重載函數(shù)來(lái)描述毫不相關(guān)的函數(shù)
D.構(gòu)造函數(shù)的重載將會(huì)給初始化帶來(lái)多種方式
4.下列關(guān)于運(yùn)算符重載的描述中正確的是:
A.運(yùn)算符重載可以改變操作數(shù)的個(gè)數(shù)
B.運(yùn)算符重載可以改變優(yōu)先級(jí)
C.運(yùn)算符重載利用改變結(jié)合性
D.運(yùn)算符重載不可以改變語(yǔ)法結(jié)構(gòu)
5.關(guān)于動(dòng)態(tài)聯(lián)編的描述中錯(cuò)誤的是:
A.動(dòng)態(tài)聯(lián)編是以虛函數(shù)為基礎(chǔ)的
B.動(dòng)態(tài)聯(lián)編是在編譯時(shí)確定所調(diào)用的
C動(dòng)太聯(lián)編是通過(guò)對(duì)象的指針或?qū)ο蟮囊谜{(diào)用虛函數(shù)來(lái)實(shí)現(xiàn)的
D.動(dòng)態(tài)聯(lián)編是在運(yùn)行時(shí)確定所調(diào)運(yùn)的函數(shù)的
6.下列關(guān)于虛函數(shù)的描述中,是正確的。
A.虛函數(shù)是一個(gè)非成員函數(shù)
B.虛函數(shù)是一個(gè)static類型的成員函數(shù)
C.派生類的虛函數(shù)與基類的虛函數(shù)具有不同的參數(shù)個(gè)數(shù)和類型
D.基類中說(shuō)明了虛函數(shù)后,派生類中與其對(duì)應(yīng)的虛函數(shù)可以不加
virtual說(shuō)明
7.以下關(guān)于析構(gòu)函數(shù)的描述錯(cuò)誤的是
A.析構(gòu)函數(shù)可以被定義成純虛函數(shù)
B.一個(gè)類中只能定義一個(gè)析構(gòu)函數(shù)
C析構(gòu)函數(shù)不能有參數(shù)
D.析構(gòu)函數(shù)可以重載。
8.關(guān)于純虛函數(shù)和抽象類的描述錯(cuò)誤的是
A.純虛函數(shù)是一種特殊的虛函數(shù)
B.具有純虛函數(shù)的類是抽性類
C.一個(gè)基類中說(shuō)明有純虛函數(shù),該基類的派生類一定不再是抽象
類
D.抽象類只能作為基類來(lái)使用,其純虛函數(shù)的實(shí)現(xiàn)由派生類供給
三、運(yùn)行結(jié)果,完善程序
1.請(qǐng)寫出運(yùn)行結(jié)果
#include
classSample
{intn;
public:
Sample(inti=0){n=i;}
Sampleoperator++(){n++;return*this;}
Sampleoperator++(int){samplet=*this;n+=2;returnt;}
operatorint(){returnn;}
voidshow(){cout<<//n=/,?n?endl;}<p="">
);
voidmain()
{SampleA(2),B(2);
inta,b;
a=A++;B++;
A.show();
B.show();
cout?a?b=,H,?="np="">
)
n=4
n=4
22
2.寫出運(yùn)行結(jié)果
#include
clas
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 臨聘合同終止協(xié)議書模板
- 訂單合同如何簽署協(xié)議書
- 黃金積存合同協(xié)議書范本
- 刮瓷工程合同協(xié)議書
- 素食店創(chuàng)業(yè)計(jì)劃書syb
- 花束銷售計(jì)劃書
- 清潔能源創(chuàng)業(yè)計(jì)劃書可持續(xù)發(fā)展的新能源解決方案
- 醫(yī)療項(xiàng)目可行性研究報(bào)告
- 統(tǒng)編版-2025秋四年級(jí)語(yǔ)文上冊(cè)-【16 麻雀】交互課件
- 健身房前期方案
- 初三中考宣誓誓詞82060
- 觸電事故桌面推演方案
- 護(hù)理風(fēng)險(xiǎn)評(píng)估及填寫要求
- 《中興通訊績(jī)效管理制度》-人事制度表格【管理資料】
- 微邦生物技術(shù)生活污水處理中的應(yīng)用
- 鐵路工務(wù)技術(shù)手冊(cè)
- (完整版)硬件測(cè)試規(guī)范
- 2006年工資標(biāo)準(zhǔn)及套改對(duì)應(yīng)表
- DBJ∕T 13-183-2014 基樁豎向承載力自平衡法靜載試驗(yàn)技術(shù)規(guī)程
- 張雙樓煤礦安全評(píng)價(jià)報(bào)告(出版稿10.14)
- [模板]健康教育處方
評(píng)論
0/150
提交評(píng)論