類和對象 測試練習(xí)題_第1頁
類和對象 測試練習(xí)題_第2頁
類和對象 測試練習(xí)題_第3頁
類和對象 測試練習(xí)題_第4頁
類和對象 測試練習(xí)題_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

類和對象單項選擇題1.在聲明類時,關(guān)鍵字private、public和protected出現(xiàn)____B__。A).至少一次B).至多一次C).public至少一次D).任意次數(shù)2.面向?qū)ο蟪绦蛟O(shè)計將數(shù)據(jù)和A封裝在一起,作為一個相互依存,不可分割的整體來處理。A)對數(shù)據(jù)的操作B)信息C)數(shù)組D)數(shù)據(jù)抽象3.下列說法中錯誤的是___C__。A).構(gòu)造函數(shù)沒有類型B).創(chuàng)建對象時構(gòu)造函數(shù)自動被調(diào)用C).在一個類中,只能定義一個構(gòu)造函數(shù)D).構(gòu)造函數(shù)的函數(shù)名與類同名4..以下敘述中不正確的是A____。A).類中的數(shù)據(jù)成員可以是私有或公有的,而類中的成員函數(shù)必須是公有的B).拷貝構(gòu)造函數(shù)的作用是使用一個已經(jīng)存在的對象去初始化一個新的同類的對象C).類中的構(gòu)造函數(shù)可以重載,而析構(gòu)函數(shù)不能重載D).構(gòu)造函數(shù)和析構(gòu)函數(shù)都應(yīng)是類的公有成員函數(shù)5.下列___C___不是類的成員函數(shù)。A).構(gòu)造函數(shù)B).析構(gòu)函數(shù)C).友元函數(shù)D).拷貝構(gòu)造函數(shù)6.通??截惓跏蓟瘶?gòu)造函數(shù)的參數(shù)是__C___。A)某個對象名B)某個對象的成員名C)某個對象的引用名D)某個對象的指針名7.類Mclass的拷貝初始化構(gòu)造函數(shù)是___D____。A)Mclass()B)Mclass(Mclass)C)Mclass(Mclass*)D)Mclass(Mclass&)8.下列對析構(gòu)函數(shù)的描述中,正確的是A___。A)一個類中只能定義一個析構(gòu)函數(shù)B)析構(gòu)函數(shù)名與類名不同;C)析構(gòu)函數(shù)的定義只能在類體內(nèi)D)析構(gòu)函數(shù)可以有一個或多個參數(shù)9.以下對析構(gòu)函數(shù)描述正確的是___C___。A).析構(gòu)函數(shù)返回類型應(yīng)是voidB).函數(shù)體內(nèi)不能有循環(huán)語句C)無形參,也不能重載D).函數(shù)體內(nèi)必須有delete語句10.關(guān)于成員函數(shù),以下敘述中不正確的是___A____。A).成員函數(shù)一定是內(nèi)聯(lián)函數(shù)B).成員函數(shù)可以重載C).成員函數(shù)的參數(shù)可以設(shè)置默認(rèn)值D).成員函數(shù)可以是另一個類的友元函數(shù)11.在面向?qū)ο蟮某绦蛟O(shè)計中,作用域符(::)的功能是___D__。A).標(biāo)識作用域的級別B).指出作用域的大小C)定義作用域的大小D).標(biāo)識某個成員函數(shù)是屬于哪個類的12.靜態(tài)成員函數(shù)沒有_C_____。A).返回類型B).返回值C).this指針D).局部變量13..關(guān)于類的靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù),以下不正確的敘述是A___。A).類外可以直接引用類的靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)B).類的靜態(tài)數(shù)據(jù)成員不能在類的構(gòu)造函數(shù)中初始化C).類的其他函數(shù)可以調(diào)用類的靜態(tài)成員函數(shù)D).類的靜態(tài)數(shù)據(jù)成員被類的所有對象共享14.以下關(guān)于類和對象敘述正確的是C___。A).一個類只能有一個對象B).一個對象可以屬于幾個不同的類C)對象是類的一個具體實例D)一個類中可以有本類的對象18.下面關(guān)于友員函數(shù)的說法中,正確的是_____B___。A)友員函數(shù)是本類的一個成員函數(shù)B)友員函數(shù)可以通過對象訪問本類的所有成員C)友員函數(shù)有this指針D)友員函數(shù)可以直接訪問本類的所有成員20.假定AB為一個類,則執(zhí)行“ABa(4),b[5],*p[2];”語句時,自動調(diào)用該類構(gòu)造函數(shù)的次數(shù)為A。A)6B)7C)9D)11閱讀程序1.寫出以下程序運行結(jié)果。#include<iostream.h>classcube{public:cube(intht=2,intwd=5,intdp=3){height=ht;width=wd;depth=dp;cout<<″depth=″<<depth<<″construnctedcalled″<<endl;}~cube(){cout<<″depth=″<<depth<<″Destructedcalled″<<endl;}intvolume(){returnheight*width*depth;}private:intheight,width,depth;};voidmain(){cubecone(10,20,15),ctwo;cout<<cone.volume()<<endl;cout<<ctwo.volume()<<endl;}運行結(jié)果:depth=15construnctedcalled450depth=3construnctedcalled30depth=3construnctedcalleddepth=15construnctedcalled2.寫出以下程序運行結(jié)果。#include<iostream.h>classA{inta,b;public:A(){}A(inti,intj):a(i),b(j){cout<<"constructor\n";}~A(){cout<<a<<"distructor\n";}};voidmain(){Aob2(2,5);Aob1(ob2);}運行結(jié)果:ConstructorConstructor2distructor2distructor3.當(dāng)輸入3、4和輸入10、20時,寫出以下程序輸出結(jié)果。#include<iostream.h>classTC{private:doublelead1,lead2,total;intp1,p2;public:TC(){p1=15;p2=20;}TC(intx,inty){p1=x;p2=y;}voiddatap(){cout<<″輸入2個整數(shù):″;cin>>lead1>>lead2;total=p1*lead1+p2*lead2;}voiddisplay(){cout<<″total=″<<total<<endl;}};voidmain(){TCA,B(6,8);A.datap();A.display();}運行結(jié)果:,輸入2個整數(shù):3,4total=125輸入2個整數(shù):10,20total=2204.寫出以下程序運行結(jié)果。#include<iostream.h>classvalue{private:inta;public:value(){a=1;}value(inti){a=i;}voidlist(){cout<<a<<”“;}};valuedata1[4];valuedata2[4]={10,20,30};voidmain(){inti;for(i=0;i<4;i++)data1[i].list();cout<<endl;for(i=0;i<4;i++)data2[i].list();cout<<endl;}運行結(jié)果:111110203015.寫出以下程序運行結(jié)果。#include<iostream.h>classA{public:A(inti,intj){a=i;b=j;cout<<″constructor.\n″;}~A(){cout<<″Destructor″<<a<<b<<endl;}private:inta,b;};voidmain(){A*pa,*pb;pa=newA(4,6);pb=newA(10,15);deletepa;deletepb;}運行結(jié)果:constructor.constructor.Destructor1015Destructor466.寫出以下程序運行結(jié)果。#include<iostream.h>classcount{public:count(){count1++;}staticintdata(){returncount1;}~count(){count1--;}private:staticintcount1;};intcount::count1=60;voidmain(){counta,b,c,d,e;cout<<count::data()<<endl;cout<<a.data()<<endl;cout<<e.data()<<endl;}運行結(jié)果:6565657.寫出以下程序運行結(jié)果。#include<iostream.h>classmyclass{private:intx,y;staticlongsum;public:myclass(inta,intb){x=a;y=b;}voidgetxy(){sum*=x*y;cout<<″sum=″<<sum<<endl;}};longmyclass::sum=1;voidmain(){myclassob1(1,2);ob1·getxy();myclassob2(3,4);ob2·getxy();myclassob3(5,6);ob3·getxy();}運行結(jié)果:sum=2sum=24sum=7208.寫出以下程序運行結(jié)果。#include<iostream.h>classsample{intx,y;public:sample(){x=0;y=0;}sample(inti,intj){x=i;y=j;}voidcopy(sample&A){x=A.x;y=A.y;}voidadd(){x+=y++;y+=x++;}voiddisplay(){cout<<″x=″<<x<<endl;cout<<″y=″<<y<<endl;}};voidmain(){sampleob1,ob2(1,2);ob2.add();ob1.copy(ob2);ob1.add();ob1.display();}運行結(jié)果:X=11Y=179.寫出以下程序運行結(jié)果。#include<iostream.h>classLocation{public:intx,y;intgetX();intgetY();voidsetXY(int,int);};intLocation::getX(){returnx;}intLocation::getY(){returny;}voidLocation::setXY(intsetX,intsetY){x=setX;y=setY;}voiddisplay(Location&ra){cout<<ra.getX()<<”“<<ra.getY()<<endl;}intmain(){Locationa[]={{1,1},{2,2},{3,3},{4,4},{5,5}};Location&ra=a[0];a[2].setXY(6,6);ra.setXY(7,7);for(inti=0;i<5;i++)display(a[i]);return1;}運行結(jié)果:7722664455程序填空1.以下程序能根據(jù)對象提供的長方形的長和寬得到該長方形的面積和周長。#include<iostream.h>classrectangle{private:floatledge,sedge;public:rectangle(){ledge=0;sedge=0;}rectangle(floata,floatb){ledge=a;sedge=b;}voidarea(){cout<<”面積:”<<ledge*____sedge____<<endl;}voidlength(){cout<<”周長:”<<___(ledge+_sedge)*2____<<endl;}};voidmain(){rectangleA(3.5,2.0),B(4.2,2.0);A.area();A.length();B.area();B.length();}運行結(jié)果:面積:7周長:11面積:8.4周長:12.42.填空完成以下程序,使該程序的執(zhí)行結(jié)果為10。#include<iostream.h>classMyClass{intx;public:MyClass(intn){x=n;}intGetNum(){returnx;}};voidmain(){MyClassmy(10);cout<<my.GetNum()<<endl;}3.以下程序能實現(xiàn)求a2+b2+c2。其中a、b、c的值由對象的初始化值提供。該程序使用靜態(tài)成員實現(xiàn)。#include<iostream.h>classmyclass{public:myclass(intx);voidgetnumber();staticvoidResult();private:inta;staticlongintsum;//定義靜態(tài)數(shù)據(jù)成員sum};myclass::myclass(intx){a=x;sum+=a*a;}voidmyclass::getnumber(){cout<<″Number=″<<a<<endl;}voidmyclass::Result(){cout<<″Result=″<<sum<<endl;}__longint_myclass::sum=0______;//靜態(tài)數(shù)據(jù)成員再定義并初始化voidmain(){myclassob1(5),ob2(10),ob3(15);ob1.getnumber();ob2.getnumber();ob3.getnumber();__0b1.Result()___________;//調(diào)用Result輸出結(jié)果}運行結(jié)果:Number=5Number=10Number=15Result=3504.某單位經(jīng)銷的貨物,購進(jìn)和賣出以重量為單位,以下程序記下目前庫存貨物的總重量(totalweight),在橫線填空并寫出運行結(jié)果.#include<iostream.h>classgoods{private:staticinttotalweight;intweight;public:goods(intw){weight=w;totalweight+=w;}goods(goods&gd){weight=gd.weight;totalweight+=weight;}~goods(){totalweight-=weight;cout<<totalweight<<endl;}intget(){returnweight;}staticintgettotal(){returntotalweight;}};intgoods::totalweight=0;//靜態(tài)數(shù)據(jù)成員再定義并初始化voidmain(){intw;cout<<”輸入第一種貨物重量:”;cin>>w;goodsg1(w);cout<<”第一種貨物重”<<g1.get()<<endl;cout<<”輸入第二種貨物重量:”;cin>>w;goodsg2(w);cout<<”第二種貨物重”<<g2.get()<<endl;cout<<”貨物總重量是:””;cout<<g1.gettotal()<<endl;}goods::gettotal()運行結(jié)果:輸入第一種貨物重量:

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論