




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1 C+語(yǔ)言基礎(chǔ)及過(guò)程化程序設(shè)計(jì)1.1 基礎(chǔ)概念1.函數(shù)聲明和函數(shù)定義有什么區(qū)別?答:1)函數(shù)聲明是函數(shù)的原型,強(qiáng)調(diào)函數(shù)如何被使用,不包含函數(shù)的實(shí)現(xiàn)代碼;2) 函數(shù)定義給出函數(shù)的實(shí)現(xiàn)代碼。2 .constchar*p1;char*constp2的區(qū)別答:1)const位于星號(hào)的左側(cè),const用來(lái)修飾指針?biāo)赶虻淖兞浚粗羔樦赶驗(yàn)槌A浚?)const位于星號(hào)的右側(cè),const用來(lái)修飾指針本身,即指針本身是常量。3 .delete與delete區(qū)別答:delete只會(huì)調(diào)用一次析構(gòu)函數(shù),而delete口會(huì)調(diào)用動(dòng)態(tài)分配的多個(gè)對(duì)象的析構(gòu)函數(shù)4 .解釋堆和棧的區(qū)別答:1)棧:由編譯器自動(dòng)分配釋放,存放函
2、數(shù)的參數(shù)、局部變量等。通常在超出作用域后由系統(tǒng)自動(dòng)釋放。2)堆:一般由程序員負(fù)責(zé)分配與釋放,若程序員不釋放,占用的內(nèi)存直到程序結(jié)束才由OS回收。5 .在什么時(shí)候需要使用“常引用”?答:如果既要利用引用提高程序的效率,又要保護(hù)傳遞給函數(shù)的數(shù)據(jù)不在函數(shù)中被改變,就應(yīng)使用常引用。6 .全局變量和局部變量在內(nèi)存中的區(qū)別。答:1)全局變量?jī)?chǔ)存在靜態(tài)數(shù)據(jù)區(qū),程序加載時(shí)分配并初始化,程序結(jié)束時(shí)釋放;2)局部變量在棧中,進(jìn)入變量作用域后分配,超出其作用域后釋放;3) 全局變量不初始化會(huì)執(zhí)行缺省初始化,如整型變量缺省初始化為0,局部變量不初始化不會(huì)執(zhí)行缺省初始化,往往為垃圾值。7.簡(jiǎn)述內(nèi)存的分配方式。答:1)靜
3、態(tài)存儲(chǔ)區(qū),是在程序編譯時(shí)就已經(jīng)分配好的,在整個(gè)運(yùn)行期間都存在,如全局變量、常量。2)棧上分配,函數(shù)內(nèi)的局部變量和形參在棧上分配。3)堆上分配,動(dòng)態(tài)分配,用new分配內(nèi)存,用delete來(lái)釋放內(nèi)存。8 .指針的幾種典型應(yīng)用情況。int*pn;指針數(shù)組,每個(gè)元素均為指向整型數(shù)據(jù)的指針。int(*p)n;指向一維數(shù)組的指針,這個(gè)一維數(shù)組含有n個(gè)整型數(shù)據(jù)。int*p();返回指向整型指針的函數(shù)。int(*p)();指向函數(shù)的指針,要求函數(shù)無(wú)參并返回整型。9 .說(shuō)明0、0、0、“0的區(qū)別”答:0表示整數(shù)常量,值為0;0表示ASCII碼值為0的字符常量;0表示ASCII碼值為48的字符常量;“0為字符串常
4、量,其中包含”0和0兩個(gè)字符。10 .說(shuō)明下面程序中存在的問(wèn)題#include<iostream>intmain()intarr10,*p=arr;inti;for(;p<arr+10;+p)cin>>*p;for(;a<p;+arr)cout<<*arr<<0;return0;答:arr為數(shù)組名,對(duì)應(yīng)地址不可修改,不能應(yīng)用+arr運(yùn)算。11 .有如下定義,請(qǐng)寫出訪問(wèn)a23元素的不同方法inta45;int(*p)5=a;答:a23、p23、*(a2+3)、*(p2+3)、*(*(a+2)+3)、*(*(p+2)+3)閱讀程序12 寫
5、出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intmain()doublenumOne=2.5;intnumTwo=3;doublequotient=numOne/2;cout<<"Quotient:"<<quotient<<endl;quotient=numTwo/2;cout<<"Quotient:"<<quotient<<endl;return0;13 寫出下面程序的運(yùn)行結(jié)果。#include<iostream
6、>usingnamespacestd;intnumber=103;intdigit,tens,hundreds;digit=number%10;tens=(number/10)%10;hundreds=(number/100)%10;cout<<"Hundreds:"<<hundreds<<",Tens:"<<tens<<",Digit:"<<digit<<endl;return0;14 運(yùn)行下面的程序3次,分別輸入90、78、60,寫出每次程序
7、執(zhí)行的輸出結(jié)果。#include<iostream>usingnamespacestd;intmain()intgrade;cout<<"Enteragrade(1-100):"cin>>grade;if(grade>=85)cout<<"Excellentn"elseif(70<=grade<85)cout<<"Passn"elsecout<<"Failn"return0;15 寫出下面程序的運(yùn)行結(jié)果。#include<
8、;iostream>usingnamespacestd;boolcheck(intscore,intbaseLine)if(score>=baseLine)returntrue;returnfalse;boolcheck(intscore,intbaseLine=60);intmain()intscore=65;if(check(score)=true)cout<<"Passed!n"elsecout<<"Failed!n"if(check(score,70)=true)cout<<"Passed
9、!n"elsecout<<"Failed!n"return0;16 寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intfun(inta);doublefun(doublea);charfun(chara);intmain()cout<<fun(3)<<endl;cout<<fun(3.6)<<endl;cout<<fun('A')<<endl;cout<<fun('g')&l
10、t;<endl;return0;intfun(inta)returna/2;doublefun(doublea)returna/2;charfun(chara)charresult=a;if(a>='a'&&a<='z')result=a-32;if(a>='A'&&a<='Z')result=a+32;returnresult;17 寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intgcd(intm,in
11、tn)if(n=0)returnm;returngcd(n,m%n);intmain()cout<<"1:"<<gcd(20,8)<<endl;cout<<"2:"<<gcd(36,64)<<endl;return0;18 寫出下面程序的運(yùn)行結(jié)果,假定輸入"Hello_123"#include<iostream>usingnamespacestd;intmain()charword50;cout<<"Enteraword:&quo
12、t;cin>>word;for(inti=0;wordi!='0'+i)if(wordi>='a'&&wordi<='z')wordi-=32;cout<<"Uppercase:"<<word<<endl;return0;19 寫出下面程序的運(yùn)行結(jié)果,假定輸入"Hello123_World#include<iostream>usingnamespacestd;intmain()charword50;cout<<&quo
13、t;Enterastring:"cin>>word;intpos=0;for(inti=0;wordi!='0'+i)if(wordi<'0'|wordi>'9')wordpos=wordi;+pos;wordpos='0'cout<<"result:"<<word<<endl;return0;20 寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intmain()inti,j;fo
14、r(i=0;i<5;i+)for(j=i;j<5;j+)cout<<"*"cout<<endl;return0;21 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intsum(inta,intb=1,intc=3)returna+b+c;intmain()intsum(inta,intb=3,intc=4);cout<<sum(2)<<endl;cout<<sum(2,5)<<endl;cout<<sum(2,3,
15、6)<<endl;return0;22 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;char&elem(char*s,intn)returnsn;intmain()charstr="HelloWorld"elem(str,1)='A'cout<<str<<endl;return0;23 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intx=10;intmain()intx=15;cou
16、t<<x<<endl;cout<<:x<<endl;return0;24 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;voidxhg(int*a,int*b)int*tmp;tmp=b;b=a;a=tmp;cout«*a«''«*b«endl;)intmain()(intx(5),y(4);xhg(&x,&y);cout«x«''«y«endl;retur
17、n0;)25 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;voidxhg(int&a,int&b)(inttmp;tmp=b;b=a;a=tmp;cout«a«''«b«endl;)intmain()intx(5),y(4);xhg(x,y);cout<<x<<''<<y<<endl;return0;26 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnam
18、espacestd;intff(int*a,intsize)if(size=1)returna0;returnasize-1+ff(a,size-1);intmain()inta5=1,2,3,4,5;cout<<“result:”<<ff(a,5)<<endl;return0;27 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;voidf(conststring&s,intn)cout<<sn-1;if(n>1)f(s,n-1);intmain()f("an
19、imal",6);cout<<endl;f("hello",3);return0;28 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>usingnamespacestd;intfunc(intdata,intsize)inta=data0;intb=data0;for(inti=1;i<size;+i)if(datai>a)a=datai;if(datai<b)b=datai;returna-b;intmain()inta=9,3,2,-1,8,0,4;cout<<func(a,7)<&l
20、t;endl;cout<<func(a+2,4)<<endl;return0;29 .寫出下面程序的運(yùn)行結(jié)果。#include<iostream>#include<string>usingnamespacestd;voidf(conststring&s,intn)cout<<sn-1;if(n>1)f(s,n-1);intmain()f("animal",6);cout<<endl;f("hello",3);return0;30 .寫出下面程序的執(zhí)行結(jié)果。#includ
21、e<iostream>usingnamespacestd;intfun(intinterval)intsum=0,i=0;for(i=0;i<100;i+=interval)sum+=i;returnsum;intmain()cout<<"Result:"<<fun(2)<<endl;return0;31 .寫出下面程序的執(zhí)行結(jié)果。#include<iostream>usingnamespacestd;doublefunc(doublepData,intsize);intmain()doublearray=2
22、.2,3.8,6,5.4;cout<<"Result:"<<func(array,4)<<endl;cout<<"Result:"<<func(array,3)<<endl;return0;doublefunc(doublepData,intsize)doubleresult=0;inti;for(i=0;i<size;+i)result+=pDatai;result/=size;returnresult;面向?qū)ο蟪绦蛟O(shè)計(jì)基礎(chǔ)概念1、定義形式如下的類,C+編譯器自動(dòng)為該類產(chǎn)生的
23、四個(gè)缺省函數(shù)是什么?寫出其原型。classMyClasspublic:voidSetX(int);private:intx;答:無(wú)參構(gòu)造函數(shù),拷貝構(gòu)造函數(shù),析構(gòu)函數(shù),賦值運(yùn)算符函數(shù)。MyClass();MyClass(constMyClass&);MyClass();MyClass&operator=(constMyClass&);2、定義形式如下的類,寫出C+編譯器自動(dòng)生成的拷貝構(gòu)造函數(shù)和賦值運(yùn)算符函數(shù)的定義。classMyClasspublic:voidSetX(int);private:intx;答:MyClass(constMyClass&a)x=a.x
24、;MyClass&MyClass:operator=(constMyClass&a)x=a.x;return*this;3、拷貝構(gòu)造函數(shù)在哪幾種情況下會(huì)被調(diào)用?答:1)當(dāng)類的一個(gè)對(duì)象去初始化該類的另一個(gè)對(duì)象時(shí);2)如果函數(shù)的形參是類的對(duì)象,調(diào)用函數(shù)進(jìn)行形參和實(shí)參結(jié)合時(shí);3)如果函數(shù)的返回值是類對(duì)象,函數(shù)調(diào)用完成返回時(shí)。4、構(gòu)造函數(shù)與普通成員函數(shù)相比有什么不同?答:1)構(gòu)造函數(shù)是類的一種特殊成員函數(shù),一般情況下,它是專門用來(lái)初始化數(shù)據(jù)成員的。2)構(gòu)造函數(shù)的名字必須與類名相同,它不具有任何返回類型。構(gòu)造函數(shù)在創(chuàng)建對(duì)象時(shí)由系統(tǒng)自動(dòng)調(diào)用。5、創(chuàng)建派生類對(duì)象時(shí),構(gòu)造函數(shù)的調(diào)用順序是什么?
25、答:1)先調(diào)用基類構(gòu)造函數(shù);2)按定義順序初始化對(duì)象數(shù)據(jù)成員;3)最后調(diào)用本類的構(gòu)造函數(shù)。6、哪幾種情況必須用到初始化成員列表?答:1)類的常量數(shù)據(jù)成員初始化;2)類的引用成員初始化;3)類的對(duì)象成員初始化,而該對(duì)象沒(méi)有無(wú)參構(gòu)造函數(shù);4)基類成員初始化,而基類沒(méi)有無(wú)參構(gòu)造函數(shù)。7、C+頭文件中通常會(huì)包含哪些內(nèi)容?答:類的定義、常量定義、函數(shù)聲明、全局變量聲明8、什么是常對(duì)象?答:常對(duì)象是指在任何場(chǎng)合都不能對(duì)其成員的值進(jìn)行修改的對(duì)象。9、什么叫抽象類?答:包含純虛函數(shù)的類,不能定義抽象類對(duì)象,可以定義抽象類的指針或引用,指向或引用派生類對(duì)象。10、同類對(duì)象間是怎樣實(shí)現(xiàn)數(shù)據(jù)共享的?答:通過(guò)類的靜態(tài)
26、數(shù)據(jù)成員來(lái)實(shí)現(xiàn)。靜態(tài)數(shù)據(jù)成員屬于類,而不為某個(gè)對(duì)象所私有,所有實(shí)例對(duì)象共享類的靜態(tài)數(shù)據(jù)成員。11、函數(shù)重載是什么意思?它與虛函數(shù)的概念有什么區(qū)別?答:1)函數(shù)重載是相同作用域內(nèi)存在多個(gè)同名的函數(shù),編譯系統(tǒng)在編譯階段通過(guò)函數(shù)參數(shù)個(gè)數(shù)、參數(shù)類型不同來(lái)區(qū)分該調(diào)用哪一個(gè)函數(shù),即實(shí)現(xiàn)的是靜態(tài)的多態(tài)性,但不能僅僅通過(guò)函數(shù)返回值不同來(lái)實(shí)現(xiàn)函數(shù)重載。2)虛函數(shù)在基類中通過(guò)使用關(guān)鍵字virtual來(lái)聲明一個(gè)函數(shù)為虛函數(shù),該函數(shù)的功能可能在將來(lái)的派生類中重新定義或者在基類的基礎(chǔ)之上進(jìn)行擴(kuò)展,系統(tǒng)只能在運(yùn)行階段才能動(dòng)態(tài)決定該調(diào)用哪一個(gè)函數(shù),所以實(shí)現(xiàn)的是動(dòng)態(tài)的多態(tài)性。12、函數(shù)重載與函數(shù)覆蓋的區(qū)別?答:1)函數(shù)重載是
27、在相同作用域內(nèi),存在多個(gè)同名的函數(shù),但函數(shù)參數(shù)或參數(shù)類型不同,調(diào)用函數(shù)時(shí)編譯器通過(guò)實(shí)參類型匹配某個(gè)函數(shù)版本,屬于靜態(tài)多態(tài)性;2)函數(shù)覆蓋指基類和派生類之間存在同名函數(shù),派生類中的函數(shù)隱藏了基類的同名函數(shù)的現(xiàn)象。13、構(gòu)造函數(shù)和析構(gòu)函數(shù)是否可以被重載,為什么答:構(gòu)造函數(shù)可以被重載,析構(gòu)函數(shù)不可以被重載。因?yàn)闃?gòu)造函數(shù)可以帶多個(gè)參數(shù),而析構(gòu)函數(shù)不能帶參數(shù)。14、分析正誤:抽象類不能產(chǎn)生實(shí)例,所以不需要有構(gòu)造函數(shù)。答:錯(cuò)。抽象類中可以包含數(shù)據(jù)成員,派生類對(duì)象初始化時(shí)需要通過(guò)抽象基類的構(gòu)造函數(shù)完成對(duì)其數(shù)據(jù)成員的初始化。15、一個(gè)類的構(gòu)造函數(shù)和析構(gòu)函數(shù)什么時(shí)候被調(diào)用,是否需要手工調(diào)用答:構(gòu)造函數(shù)在創(chuàng)建類對(duì)
28、象的時(shí)候被自動(dòng)調(diào)用,析構(gòu)函數(shù)在類對(duì)象生命期結(jié)束時(shí)。構(gòu)造函數(shù)和析構(gòu)函不需要手工調(diào)用,由系統(tǒng)自動(dòng)調(diào)用。16、構(gòu)造函數(shù)和析構(gòu)函數(shù)的調(diào)用順序?析構(gòu)函數(shù)為什么要定義為虛函數(shù)?答案:構(gòu)造函數(shù)的調(diào)用順序:基類構(gòu)造函數(shù)-對(duì)象成員構(gòu)造函數(shù)-派生類構(gòu)造函數(shù);析構(gòu)函數(shù)的調(diào)用順序與構(gòu)造函數(shù)相反:派生類析構(gòu)函數(shù)f對(duì)象成員析構(gòu)函數(shù)-基類析構(gòu)函數(shù)。析構(gòu)函數(shù)定義為虛函數(shù)是為了防止析構(gòu)不徹底,造成內(nèi)存泄漏。17、請(qǐng)說(shuō)出類中private,protect,public三種訪問(wèn)限制類型的區(qū)別答案:private是私有類型,只有本類中的成員函數(shù)才能訪問(wèn);protect是保護(hù)型的,本類和子類成員函數(shù)可以訪問(wèn);public是公有類型,本
29、類和子類成員函數(shù)可以訪問(wèn),類外部通過(guò)對(duì)象可以間接訪問(wèn)。18、Test是一種類類型,現(xiàn)要為其重載前置和后置+運(yùn)算符,寫出它們的原型。答:1)前置+:Test&operator+();2)后置+:Test&operator+(int)19、說(shuō)明組合和繼承在復(fù)用代碼方面的區(qū)別答:組合關(guān)系描述的是“有一種”關(guān)系,一個(gè)對(duì)象是另一個(gè)對(duì)象的一部分;繼承關(guān)系描述的“是一種”關(guān)系,實(shí)現(xiàn)對(duì)象的層次關(guān)系。20、指出Dog類定義中的錯(cuò)誤。#include<iostream>usingnamespacestd;classDogpublic:Dog()age=1;weight=10;Dog(i
30、nta,intw)age=a;weight=w;voidplay()constcout<<age<<endl;cout<<weight+<<endl;private:constintage;intweight;答:1)age為常數(shù)據(jù)成員,不能在構(gòu)造函數(shù)體內(nèi)賦值,只能通過(guò)初始化列表完成初始化;2)play為常成員函數(shù),不能修改數(shù)據(jù)成員的值。閱讀程序1、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classBasepublic:voiddisplay()cout<<“Basedisp
31、lay”<<endl;classDerived:publicBasepublic:voiddisplay()cout<<“Deriveddisplay”<<endl;voiddisplay(Base&rr)rr.display();intmain()Baseb;Derivedd;display(b);display(d);return0;2、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classPersonpublic:Person()cout<<"PersOn造函數(shù)!”&
32、lt;<endl;Person()cout<<"Person析構(gòu)!”<<endl;classStudent:publicPersonpublic:Student()cout<<"StUdentS數(shù)!”<<endl;Student()cout<<“StUdeW!”<<endl;classTeacher:publicPersonpublic:Teacher()cout<<"Teacher造函數(shù)!”<<endl;Teacher()cout<<"Te
33、acher析構(gòu)!”<<endl;intmain()Students;Teachert;return0;3、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classExampleprivate:inti;public:Example(intn)i=n;cout<<“Constructing.”<<endl;Example()cout<<“Destructing.”<<endl;intget_i()returni;intsqrt_it(Exampleo)returno.get_i()*
34、o.get_i();intmain()Examplex(10);cout<<x.get_i()<<endl;cout<<sqrt_it(x)<<endl;return0;4、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestprivate:intx;public:Test(intxx=0):x(xx)Test&operator+()x+;return*this;return temp;Testoperator+(int)Testtemp(*this);x+;intgetV
35、alue()constreturnx;intmain()Testt;cout<<t.getValue()<<endl;cout<<(t+).getValue()<<endl;cout<<(+t).getValue()<<endl;return0;5、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestpublic:Test()cout<<“Defaultconstructor.”<<endl;Test(constTest&t)
36、cout<<“Copyconstructor!”<<endl;voidfun(Testp)intmain()Testa;fun(a);return0;6、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classDogpublic:staticintnumber;Dog()number+;cout<<"NewDog"<<endl;Dog()number-;cout<<"ADogDie"<<endl;intDog:number=0;i
37、ntmain()Dogdog;Dog*pDog=newDog();deletepDog;cout<<Dog:number<<endl;return0;7、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classAnimalpublic:Report from Animal! ” <<endl; virtualvoidReport()cout<<classTiger:publicAnimalpublic:virtualvoidReport()cout<<“ReportfromTige
38、r!”<<endl;classMonkey:publicAnimalpublic:virtualvoidReport()cout<<“ReportfromMonkey!”<<endl;voidshow(Animal*p)p->Report();intmain()Tigertiger;Monkeymonkey;Animalanimal=tiger;show(&tiger);show(&monkey);show(&animal);return0;8、寫出程序輸出結(jié)果#include<iostream>usingnames
39、pacestd;classTestpublic:Test(intxx=1):x(xx)voidoutput()constcout<<"x:"<<x<<endl;private:intx;intmain()Testt;t.output();t=4;t.output();return0;9、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestpublic:Test()cout<<"DefaultConstructorn"Test(intxx):x
40、(xx)cout<<"IntConstructorn"Test(constTest&t):x(t.x)cout<<"CopyConstructorn"private:intx;Testt;intmain()cout<<"n"Testtt(t);return0;10、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classBaseprivate:intbase;public:Base(intb)base=b;cout<<“bas
41、e=”<<b<<endl;Base();classDerived:publicBaseprivate:Basebb;intderived;public:Derived(intd,intb,intc):bb(c),Base(b)derived=d;cout<<“derived=”<<derived<<endl;Derived();intmain()Derivedd(3,4,5);return0;11、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classMatrixdouble*e
42、lem;introw,col;public:Matrix(intr,intc)row=r;col=c;elem=newdoublerow*col;double&operator()(intx,inty)returnelemcol*(x-1)+y-1;double&operator()(intx,inty)constreturnelemcol*(x-1)+y-1;Matrix()deleteelem;intmain()Matrixm(5,8);inti;for(i=1;i<6;i+)m(i,1)=i+5;for(i=1;i<6;i+)cout<<m(i,1
43、)<<”,”;cout<<endl;return0;12、寫出程序輸出結(jié)果#include<iostream.h>classPointintx,y;public:Point(intx1=0,inty1=0):x(x1),y(y1)cout<<"Point:"<<x<<''<<y<<'n'Point()cout<<"Pointdestructor!n"classCirclePointcenter;/®!心位置
44、intradius;/半徑public:Circle(intcx,intcy,intr):center(cx,cy),radius(r)cout<<"Circleradius:"<<radius<<'n'Circle()cout<<"Circledestructor!n"intmain()Circlec(3,4,5);return0;13、寫出程序輸出結(jié)果#include<iostream.h>classBasepublic:Base(inti,intj)x0=i;y0=j;vo
45、idMove(intx,inty)x0+=x;y0+=y;voidShow()cout<<"Base("<<x0<<","<<y0<<")"<<endl;private:intx0,y0;classDerived:privateBasepublic:Derived(inti,intj,intm,intn):Base(i,j)x=m;y=n;voidShow()cout<<"Next("<<x<<",
46、"<<y<<")"<<endl;voidMove1()Move(2,3);voidShow1()Base:Show();private:intx,y;intmain()Baseb(1,2);b.Show();Derivedd(3,4,10,15);d.Move1();d.Show();d.Show1();return0;14、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestpublic:Test()cout<<“Hello:”<<+i&
47、lt;<endl;staticinti;intTest:i=0;intmain()Testt2;Test*p;p=newTest2;return0;15、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestClasspublic:TestClass(inta)aa=a;cout<<aa<<"Constructed!n"TestClass()cout<<aa<<"Destructed!n"private:intaa;TestClassAA
48、(3);intmain()cout<<"InMainFuction."<<endl;TestClassBB(5);return0;16、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestClasspublic:TestClass()cout<<"Constructed!n"TestClass()cout<<"Destructed!n"intmain()TestClasst1;TestClass*p;p=newTestCl
49、ass;deletep;return0;17、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestClasspublic:TestClass()cout<<"Constructed!n"value=10;TestClass()cout<<"Destructed!n"voidsetValue(intnewValue)value=newValue;intgetValue()constreturnvalue;private:intvalue;intmain()TestCl
50、asst1;cout<<t1.getValue()<<endl;TestClass&rt1=t1;rt1.setValue(20);cout<<t1.getValue()<<endl;TestClass*pt=&t1;pt->setValue(30);cout<<t1.getValue()<<endl;return0;#include<iostream>#include<string>usingnamespacestd;classPersonprivate:stringname
51、;intage;public:Person(stringname,intage);Person()cout<<"Bye!Mynameis"<<name<<",I'm"<<age<<"yearsold."<<endl;voidgrowup()age+;Person:Person(stringname,intage)this->name=name;this->age=age;cout<<"Hello,"<<
52、;name<<"iscomming!"<<endl;intmain()Personp("zhang",1);for(inti=0;i<90;+i)p.growup();return0;19、寫出程序輸出結(jié)果#include<iostream>usingnamespacestd;classTestClasspublic:TestClass(intnewValue=0)value=newValue;cout<<"Value:"<<value<<",Co
53、nstructed!n"value=rhs.value;cout<<"Value:"<<value<<",CopyConstructed!n"TestClass()cout<<"Value:"<<value<<",Destructed!n"voidsetValue(intnewValue)value=newValue;intgetValue()constreturnvalue;private:intvalue;TestClassfoo
54、Fun(TestClasst)t.setValue(20);returnt;intmain()TestClasst1(10),t2(t1),t3;t3=fooFun(t1);return0;#include<iostream>#include<string>usingnamespacestd;classMousepublic:Mouse(stringnewName);Mouse();stringgetName()returnname;staticintmouseNum;private:stringname;intMouse:mouseNum=0;Mouse:Mouse(
55、stringnewName):name(newName)cout<<name<<"isborn!n"mouseNum+;Mouse:Mouse()cout<<name<<"isgone.n"mouseNum-;classCatpublic:Cat(conststring&newName):name(newName)cout<<name<<"iscoming!n"voidcatchMouse(Mouse*pMouse);private:stringname;
56、voidCat:catchMouse(Mouse*pMouse)cout<<"Icatchyou!Ineverwanttoseeyouagain.<<pMouse->getName()<<"!"<<endl;deletepMouse;intmain()Catcat("BlackCatDetective");Mouse*pMouse1=newMouse("Micky");cout<<Mouse:mouseNum<<"mouseleft.n"Mouse*pMouse2=newMouse("Xiaohua");cout<<Mouse:mouseNum<<"mouseleft.n"cat.catchMouse(pMouse2);
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 三年級(jí)語(yǔ)文下冊(cè) 第二單元 口語(yǔ)交際:該不該實(shí)行班干部輪流制配套教學(xué)設(shè)計(jì) 新人教版
- 人音版 音樂(lè) 七年級(jí)下冊(cè) 第二單元 穿越竹林 教學(xué)設(shè)計(jì)
- 信息技術(shù)《插入聲音和影片》教學(xué)設(shè)計(jì) 海南海口市秀英區(qū)長(zhǎng)德中學(xué)-陳慧珠
- 七年級(jí)道德與法治下冊(cè) 第3單元 在集體中成長(zhǎng) 第8課 美好集體有我在 第1框 憧憬美好集體教學(xué)設(shè)計(jì) 新人教版
- 勞動(dòng)項(xiàng)目九《捐贈(zèng)舊衣服》教學(xué)設(shè)計(jì)-2023-2024學(xué)年勞動(dòng)六年級(jí)下冊(cè)人教版
- 九年級(jí)化學(xué)上冊(cè) 第6單元 課題2《二氧化碳制取的研究》教學(xué)設(shè)計(jì) (新版)新人教版
- 人教部編版一年級(jí)上冊(cè)語(yǔ)文園地三教案設(shè)計(jì)
- 七年級(jí)地理上冊(cè) 2.1地圖基本要素教學(xué)設(shè)計(jì) (新版)商務(wù)星球版
- 人教新目標(biāo)(Go for it)版英語(yǔ)七年級(jí)下 Unit2 What time do you go to school 教案
- 地產(chǎn)培訓(xùn)計(jì)劃反饋
- 醫(yī)院醫(yī)用耗材采購(gòu)管理方案
- 2025年八省聯(lián)考高考語(yǔ)文試卷評(píng)析及復(fù)習(xí)備考指導(dǎo)課件
- 《化妝品包裝材料相容性試驗(yàn)評(píng)估指南》
- 浙江省J12共同體聯(lián)盟校2024-2025學(xué)年八年級(jí)上學(xué)期期中語(yǔ)文試題
- 高校實(shí)驗(yàn)室耗材管理制度
- 2025年國(guó)家保密基本知識(shí)考試題庫(kù)及答案
- 企業(yè)管理咨詢服務(wù)合同與企業(yè)管理服務(wù)合同
- 《旅游市場(chǎng)營(yíng)銷》課程教案
- 《考慮碳排放權(quán)的LZ光伏發(fā)電企業(yè)價(jià)值評(píng)估案例研究》
- 2024年新疆區(qū)公務(wù)員錄用考試《行測(cè)》真題及答案解析
- 《測(cè)試反應(yīng)快慢》說(shuō)課稿 -2023-2024學(xué)年科學(xué)二年級(jí)下冊(cè)教科版
評(píng)論
0/150
提交評(píng)論