C++高级编程练习题

上传人:豆*** 文档编号:201717426 上传时间:2023-04-20 格式:DOCX 页数:53 大小:44.99KB
收藏 版权申诉 举报 下载
C++高级编程练习题_第1页
第1页 / 共53页
C++高级编程练习题_第2页
第2页 / 共53页
C++高级编程练习题_第3页
第3页 / 共53页
资源描述:

《C++高级编程练习题》由会员分享,可在线阅读,更多相关《C++高级编程练习题(53页珍藏版)》请在装配图网上搜索。

1、1 C+语言基本及过程化程序设计1.1 基本概念1. 函数声明和函数定义有什么区别?答:1) 函数声明是函数的原型,强调函数如何被使用,不涉及函数的实现代码;2) 函数定义给出函数的实现代码。2. cost har *p; cha * co p;的区别答:) const位于星号的左侧,cont用来修饰指针所指向的变量,即指针指向为常量;2)cost位于星号的右侧,cns用来修饰指针自身,即指针自身是常量。3. delete与 delete区别答:deete只会调用一次析构函数,而elete会调用动态分派的多种对象的析构函数4. 解释堆和栈的区别答:1) 栈:由编译器自动分派释放,寄存函数的参数

2、、局部变量等。一般在超过作用域后由系统自动释放。2)堆:一般由程序员负责分派与释放,若程序员不释放,占用的内存直到程序结束才由S回收。5. 在什么时候需要使用“常引用”?答:如果既要运用引用提高程序的效率,又要保护传递给函数的数据不在函数中被变化,就应使用常引用。6. 全局变量和局部变量在内存中的区别。答:1) 全局变量储存在静态数据区,程序加载时分派并初始化,程序结束时释放;2)局部变量在栈中,进入变量作用域后分派,超过其作用域后释放;3) 全局变量不初始化会执行缺省初始化,如整型变量缺省初始化为0,局部变量不初始化不会执行缺省初始化,往往为垃圾值。7. 简述内存的分派方式。答:)静态存储区

3、,是在程序编译时就已经分派好的,在整个运营期间都存在,如全局变量、常量。)栈上分派,函数内的局部变量和形参在栈上分派。3)堆上分派,动态分派,用new分派内存,用dele来释放内存。8. 指针的几种典型应用状况。int *pn;-指针数组,每个元素均为指向整型数据的指针。it (p) n;-指向一维数组的指针,这个一维数组具有个整型数据。in *p();-返回指向整型指针的函数。it (*) ();-指向函数的指针,规定函数无参并返回整型。 9. 阐明0、0、0、“0”的区别答:表达整数常量,值为;表达ASII码值为0的字符常量;0表达ASCI码值为48的字符常量;“0”为字符串常量,其中涉及

4、0和0两个字符。10. 阐明下面程序中存在的问题includiotreamin main()int arr1, *ar;i i;fr(; pp;for(; a;+ar)otarr;retn 0;答:rr为数组名,相应地址不可修改,不能应用+arr运算。11. 有如下定义,请写出访问a23元素的不同措施nt a45;nt(*p)5 = a;答:a3、23、*(a23)、*(3)、*(*(a)3)、*(*(p+2)+3)1.2 阅读程序1. 写出下面程序的运营成果。nclde using nmespce sd;intmi()ouble numOe = 2.5;int umTwo 3;duble q

5、uotnt = un2;coutuotient: qutendl;uotien = numo/2;coQtent: quotientnd;eturn 0;2. 写出下面程序的运营成果。#includeusin asce std;int mai()ntnumbe = 03;intigit, te, hudres;git = nuber %1;tes =(nuber/10)%1;udes = (nber/100)%10;couHundreds: hudres, Tens:tens, Digit: digitenl;retun 0;3. 运营下面的程序3次,分别输入90、7、,写出每次程序执行的输出

6、成果。#icludeing nmespae d;int main()int rade;couad;if(rae85)coutExclent;elef(70=grde)cotusingnamspce sd;bol chck( int score, int base)if(ore = baseLine )retrn true;returnlse;ol heck(ntcore,i baseLne 60);in mn()int score=65;f(chek(scoe) = true)cotPasse!n;elsectFild!n;if( ceck(sre, 70) = ru)coutPssed!;e

7、lsecoutusingnameace td;int un(in );double fun(doublea);chr f(cha a);int n()ouf(3)edl;outun(3.6)nl;coutfun(A)enl;cotfu(g)end;retrn0;itfun(inta)return a/;ublefun(double a)rera/; caun(chr a)char reul=;if(aa& ausingnmspac std;intgcd(int m, in n)if(n=)rurn m;return c(n, n);i ain()cut1:gd(20,8)endl;out2:g

8、cd(36,64)sing nespac s;int an()ch wo50;outod;fo(it =0; word!=0; +)if(wordi=a& ord= )word= ;couUpper case:wodedl;retrn 0;8. 写出下面程序的运营成果,假定输入Hllo23Worl。#includeiosrausg naespac s;itmain()car word50;couwod;nt s=0;for(int=0; woi!0;+i)i(wordi0 |wordi9 )ordpos=wodi;+s;wrpos=;cotesu: wren;en 0;9. 写出下面程序的运营

9、成果。#ind uingnmesae std;it main() int i,j;or(i=;i5;i+) fr(ji;j) out *;otndl; retur 0;10. 写出下面程序的运营成果。#ncude ui namce td;intsum( n a, it, n c=3 )retur a+b;in main()it sum(inta, intb3,nt =4);coutum(2)dl;cut (,5)enl;cutum (2,,6)endl; return 0;11. 写出下面程序的运营成果。#iclud sng amesast;char elem(hr *s, int n)rur

10、n s;nt mai()char st=eoWrld;em(str,1)= A;otendl; rur 0;12. 写出下面程序的运营成果。#includ iostreamusing amesp std;int x=10; int main()it =5;cuxenl; cut:xen; ren ;13. 写出下面程序的运营成果。#include istreausng nameae std;vid g(inta,int *b)int *tm;tmp=b;ba; a=m;ut*a *bn;int main()in x(5),y(4);hg(&,&);cotyusin namespace std;

11、voidxhg(int &a,int &b)nt tmp;tmp=b; b; a=tp;cotabndl;intman()inx(5),y(4);xhg(x,y);coutx ysin aspaest;int ff(int*a,int si)if(size=1)reurn a0;tu asze-1+ff(,size-);nt mi()i a5=1,2,3,4,;cut“result:”si naspesd;vod f(ont stin&s,int )cout1)(s,n-);int min()(nmal,);utusignmsac std;int fuc(ntdata,it iz)nt a=a

12、a0;in daa0;f(n i;ia) =datai;if(daaib) b=datai;rern a-b;it main()nta=9,2,-1,8,,4;coufunc(a,7)edl;coutfc(a+2,4)nl;urn0;18. 写出下面程序的运营成果。#iclude incde usg nmespacs;idf(onst sting s,it n)outn1;i(n1)f(s,n1);t mi()(aial,);enl;f(hello,3);return;19. 写出下面程序的执行成果。#inlusing amesce std;it un(in intervl) in sum=0

13、,i=0; (i=0; i10; i+=interval) sm+i; retr um;intain()coutRut: fun(2);return 0;20. 写出下面程序的执行成果。#inclueiosamusig nespace st;double fuc( oule Data, itze);int mai()doble r=2, 3.8, , .4;coutResut: unc(array,)enl;cotesult: fc(array,)ed;return 0;ub fn( oube Daa , in size)ouleresut;int i;or(i=0;isie; +)resul

14、t+=pDtai;reut /= size;reurnreult;2 面向对象程序设计2.1 基本概念1、定义形式如下的类,C+编译器自动为该类产生的四个缺省函数是什么?写出其原型。cass MyClss pblic:voiS(int);pvate:nt x;;答:无参构造函数,拷贝构造函数,析构函数,赋值运算符函数。MyClas();MyClas(cons MyClas& );Myas();MCass& petr=(const Clas&);2、定义形式如下的类,写出C+编译器自动生成的拷贝构造函数和赋值运算符函数的定义。classMyClass pbic:vid SetX(in);pra:

15、int x;;答:Mylass(cont MyCas ) x=ax; Mylss MyCass::oerator=(const MyClas& ) =a.x; retu *thi; 3、拷贝构造函数在哪几种状况下会被调用?答:1)当类的一种对象去初始化该类的另一种对象时;)如果函数的形参是类的对象,调用函数进行形参和实参结合时;)如果函数的返回值是类对象,函数调用完毕返回时。、构造函数与一般成员函数相比有什么不同? 答:)构造函数是类的一种特殊成员函数,一般状况下,它是专门用来初始化数据成员的。)构造函数的名字必须与类名相似,它不具有任何返回类型。构造函数在创立对象时由系统自动调用。5、创立派

16、生类对象时,构造函数的调用顺序是什么?答:1)先调用基类构造函数;2)按定义顺序初始化对象数据成员;)最后调用本类的构造函数。、哪几种状况必须用到初始化成员列表?答:1)类的常量数据成员初始化;)类的引用成员初始化;3)类的对象成员初始化,而该对象没有无参构造函数;4)基类成员初始化,而基类没有无参构造函数。7、C+头文献中一般会涉及哪些内容?答:类的定义、常量定义、函数声明、全局变量声明、什么是常对象?答:常对象是指在任何场合都不能对其成员的值进行修改的对象。9、什么叫抽象类?答:涉及纯虚函数的类,不能定义抽象类对象,可以定义抽象类的指针或引用,指向或引用派生类对象。10、同类对象间是如何实

17、现数据共享的?答:通过类的静态数据成员来实现。静态数据成员属于类,而不为某个对象所私有,所有实例对象共享类的静态数据成员。1、函数重载是什么意思?它与虚函数的概念有什么区别?答:1) 函数重载是相似作用域内存在多种同名的函数,编译系统在编译阶段通过函数参数个数、参数类型不同来辨别该调用哪一种函数,即实现的是静态的多态性,但不能仅仅通过函数返回值不同来实现函数重载。2) 虚函数在基类中通过使用核心字virtu来声明一种函数为虚函数,该函数的功能也许在将来的派生类中重新定义或者在基类的基本之上进行扩展,系统只能在运营阶段才干动态决定该调用哪一种函数,因此实现的是动态的多态性。12、函数重载与函数覆

18、盖的区别?答:1)函数重载是在相似作用域内,存在多种同名的函数,但函数参数或参数类型不同,调用函数时编译器通过实参类型匹配某个函数版本,属于静态多态性;2)函数覆盖指基类和派生类之间存在同名函数,派生类中的函数隐藏了基类的同名函数的现象。13、构造函数和析构函数与否可以被重载,为什么?答:构造函数可以被重载,析构函数不可以被重载。由于构造函数可以带多种参数,而析构函数不能带参数。14、分析正误:抽象类不能产生实例,因此不需要有构造函数。答:错。抽象类中可以涉及数据成员,派生类对象初始化时需要通过抽象基类的构造函数完毕对其数据成员的初始化。15、一种类的构造函数和析构函数什么时候被调用,与否需要

19、手工调用?答:构造函数在创立类对象的时候被自动调用,析构函数在类对象生命期结束时。构造函数和析构函不需要手工调用,由系统自动调用。6、构造函数和析构函数的调用顺序?析构函数为什么要定义为虚函数?答案:构造函数的调用顺序:基类构造函数对象成员构造函数派生类构造函数;析构函数的调用顺序与构造函数相反:派生类析构函数对象成员析构函数基类析构函数。析构函数定义为虚函数是为了避免析构不彻底,导致内存泄漏。17、请说出类中private,prott,pblic三种访问限制类型的区别答案:private是私有类型,只有本类中的成员函数才干访问;proec是保护型的,本类和子类成员函数可以访问;publi是公

20、有类型,本类和子类成员函数可以访问,类外部通过对象可以间接访问。8、s是一种类类型,现要为其重载前置和后置+运算符,写出它们的原型。答:1)前置+:Test& oerator+();2)后置+:Tt oprtor+(it)1、阐明组合和继承在复用代码方面的区别答:组合关系描述的是“有一种”关系,一种对象是另一种对象的一部分;继承关系描述的“是一种”关系,实现对象的层次关系。2、指出og类定义中的错误。#ncudesin naspace std;clssogpbi:og() age=1; weght=10; Dog(int a, w)age=a; wigh=w; voidpay()cns cou

21、tgeend;cueight+ed;prive: cons nt ae;n wight;;答:1) age为常数据成员,不能在构造函数体内赋值,只能通过初始化列表完毕初始化;) pla为常成员函数,不能修改数据成员的值。2.2 阅读程序1、写出程序输出成果include sg namepae st;clssBase publc:void display() t“Bse disay”endl; ;cls Drivd:pui Bpbli:vod ispa() cout“eriveddispy”endl; ;vid dila(Base & r)rdily();it man()Base b;Deriv

22、ed d;dispay(b);diplay();rur 0;2、写出程序输出成果#ncue sn namesace std;cla ersonic:Pern() cout“Peson构造函数!”ndl;Pern() cout“Peon被析构!”en;;lass Studet : bl eonpublic:Studet() cou“tudent构造函数!”ndl;Sent()u“Student被析构!”endl;cass acer : ubl Pesonpublc:eae()cout“Tcher构造函数!”end;Tehr() cout“Teacher被析构!”endl;inmain()Sdet

23、 s;eacher t;reu;3、写出程序输出成果#include singnamespae std;classEaleprvate:nt;pbi:Example(it n) in;ou“Contruin”dl;Exmple() out“Destructng.”endl; int get_() eur i; ;intqrt_it(xaleo) return o.get()oge_();int mai()xample x(10);ctx.get()end;cotsrt_it(x)ndl;eturn 0;4、写出程序输出成果#inlude unaespae d;assTetprivate:nt

24、x;ublic:e(nt xx=0):(x)Tst& oprator+()x+;etrn*h;Tstortor+(nt)Tttemp(hs); x+;eurn ep;it etValu()cntrtrn ;in min()Tes ;coutt.getVau()end;cu(t+)tVaue()endl;cot(+t).getVaue()endl;rtr 0;、写出程序输出成果#incude ostramsingnaspce std;cla Testbic:est() cout“efaltcnsror.”edl; Test(coTest& t)cou“Copy nstruco!”nd;;void

25、 fun(Test p) it main()Test a;fun(a);rt 0;6、写出程序输出成果#inude ioseausng namespa d;clas Doublic:staic int number;Dg() ume+;outNew ogenl;o() nmbr-;cout og end;inDog:ber=;it mai()g do;Dog *pDeDo();elete pDo;coutDog::numrend;retun ;7、写出程序输出成果#clusing nmespace td;lass Anmapubic:v void Reprt() cout“Rpor fm im

26、al!”l; ;css Tie: publc Animalpubi:virtloid Reort() cout“ReortfroTiger!”enl; ;cls Moey : ubi nmlpubl:itualvoidReort()cut“epotfom Mokey!”Rport(); intmain()Tigerter;Mney mne;Amal animal=tiger;hw(&tiger);so(&money);show(&aial);retr 0;8、写出程序输出成果icludesi namesace td;css Testublic:Test(inxx=1):x(xx)oid tpu

27、t()contcoux:xend;private:int x;n mai()Test t;t.otpu();=4;output();eturn 0;9、写出程序输出成果nlue ui namespa st;cass Testpublc:Te()cotDefault Costrtrn;es(int xx):x(x)utIn Cotructr;Tet(cont Tes&t):(t.x)utCopyonstuctorn;priva:t ;Test t;ntman()ut-;Tt tt(t);rtur 0;0、写出程序输出成果icle stramusin namescestd;ass Baepriva

28、te:int bse;puic:Bas(in b) bs=b;cou“ase”bendl;Bae() ;las Drivd : pulc Basepriae:Baseb;i deied;pbic:erie(int,int b,intc) :bb() , as(b)derivedd;cut“drid=”derveendl;Drived() ;it ai()Dried d(,,5);eturn 0;11、写出程序输出成果#includeostemsn mesacestd;clssatrixdubl * elm;int ro,col;pulic:Matrix(it r,in c) r;col=c;e

29、mne obew *col;doue &ortor()(in x,iy)eturn eemcl *(x-1)+-1;oubl &orato()(it x,iny) contretur lem ol*(x-1)+y-1;Matri()det em; ;intmin()Matri m(5,);int i;or(i=1;6;i+)m(i,1)=i+;o(i=1;6;+)ctm(i,1)”,”;outend;rtn ;1、写出程序输出成果#include class Poin n x,y;pulic:Poit(ntx1=,int y1=0) :x(x1), y(y) cutint: yn;Point(

30、) coutPoit etrctor!n;;class ircePint enter;/圆心位置int raus;/半径pblic:Circl(ntcx,incy,in r):center(c,y),radus(r) outCircle aus:radsn;icle()outire destrct!n;int mai()Circl (3,5);etrn0;13、写出程序输出成果#incue clas aepblc:Base (i,int j) x0=; =j;void ove(int x,ny) 0+x;0+y;vod ho() cotBa(x,y0)enl;privt:intx0,0;;cl

31、aDerive: privatBaeublic:Deved(in ,t,int m,t n):Base(i,) xm; =n;vdShow()cutext(,y)end;vid oe1()Me(2,3);void Show()ase:Sow();pvat:int ,;it main( )Bse(1,2);b.Show();Derie d(,4,,5);d.Move();.Show();d.Sow();return0;14、写出程序输出成果#iclue using amepae sd;ss Tstpubc:Tst() cout“Helo: ”+indl;stati nt i;;intest:i=

32、0;int in()Tst2;Tet *p;p=ew Te;retur 0;15、写出程序输出成果#incude istremsi mespce d;ass stClsspubli:TetCass(int a)aa=a;utaa Cnstruced!n;TestClass()couta Dstructd!n;private:intaa;TesClas A();t ain()ouIn ainFuction.endl;TestClas BB();eurn0;6、写出程序输出成果#incld using namspaestd;las stCspublic:Tetlss() ctonstruce!n;

33、 TstClass()coutDesructed!; ;int main()Teslas t;TestCls *;p=ne slss;delte p;retur ;7、写出程序输出成果#incud iosreamusing naepacestd;lass TestClasspublic:Testlass() coutConsrctd!;vlu = 0;TestCss()cutDestuce!n;void setlue( twalue)val = newa;intValue()cost rtur vle; prvate:int alue;;n mn()TetClass t1;coutt1.etV

34、alu()end;TestCass &rt1 =t1;rt1.seVaue(0);coueVue()nl;TstClas pt=t1;tsetValu(0);cutt1.gtV()endl;rturn 0;、写出程序输出成果#incudeincue ug nmpace d;clssPernrivae:strigame;ntae;public:Psn(str am, age);Pson()cotBye! My nme is am,mage yers old.namenam;this-ag=ae;cotelo,me is ommig!enl;i ain()Persn (zhag,1);f(it i

35、=0;i90;+i).gowup();eturn 0;、写出程序输出成果#nclud usng nespaestd;class TestClasspubic:TestClass(it nVaue)vaue Value;outValue: ue, Cnstructed!n;stClass( s Testls & rs)v=hs.vale;coutVae:alue, Cop onstructe!;TstClass() outValu: value, estrctd!; vo setVlue( nt nwValue)value = neValue; nt getVlue()cos eturn val

36、u; piva:ntvalu;estClssfoFun( TtCas ).ealue(20);return;intmai()TestCas t1(1),t2(t1),t;t3=fooFun(t);reur ;0、写出程序输出成果#cud #icue sng namspace t;lass Mosepubl:ouse(strinnewNme );ous();strin getNe() rtrn nae; tatc int mousNm;priate:tringnme;;it ouse:oseNum 0;Moue::ose( sting nwNme ): name(ewNam)coutnameis

37、born!n;mouseNu+;ouse:os()cotame is gone.n;mouseNu-;cla tublic:Ca( cs strg& name): nam(ewNae)coutname iscmin!;vod cahMuse( Mouse *pMuse);priate:ri ae;void t:catouse(oe *pMous)ouI catch you!eve wan to see you agin. getNam()!endl;dle pMouse;nt min()Ct cat(Blac Ct Dtective);ouse *pMous1 = nwMouse(ky);co

38、utMouse::mouseum ouse left.;us *Mouse2= w Mouse(Xiaoa);coutouse::mouseNum o left.;catccMoue(pMoe);coutMouse:museNum mouse let.n;a.ctchMoue(Mose1);coutMoue::moeu mouseleft.n;rturn 0;3 模板、异常与输入输出流3.1 基本概念1、函数模板和模板函数的区别答:1)函数模板是用于在编译阶段生成函数的模板,是生成函数的框架;2)模板函数是编译阶段,根据函数调用时匹配的参数类型,实际生成的具体函数。. 辨别正误:编译器对函数模

39、板进行编译,生成特定数据类型函数的目的代码。答:错误。预解决器根据函数模板和调用函数时的实参类型生成相应的函数代码,编译器对生成的函数代码进行编译,编译器并不编译模板。3. 类模板和模板类的区别答:1)类模板是用于在编译阶段生成类定义和类成员函数定义的模板,是生成类的骨架。2)模板类是编译阶段,根据定义对象时指定的参数类型,生成的类代码。4. 简述C+中异常机制中的语句答:1)tr:包围有也许出问题的代码块;2)hrw:抛出异常;3)catch:捕获解决throw语句所抛出的异常3.2 阅读程序1、写出下面程序的运营成果#iclud sin amesacesd;eplteT arae(*pd,

40、t ) um=0;int i=0; wile(in)sum+di+;return sm/; in min()doubea=2.5,4.5,65,8.5;coutaeae(a, )edl;tur 0;2、写出程序输出成果ncl#include singusing aesce std;templtetypam vif(cnst T& a,constTb)couuing nampac std;assCExceptiopblic:virua id dsly()cout“数组访问越界!n”;lss CUprBundExceon :ublic Cxcepionpbi:void disay() cou“数组访问超过最大许可下标”ed; ;class LowBoundEeption:public CExetion pubic:void ipl() cot“数组访问不不小于最小许可下标”el;#deieN 3ineElement(it p,intindex)if(index=N) thrw CUpperBunExceptin();if(ndex) throCoweBoundExceptn();return pidx;ia()intaN=1,2,3;try ougetEement(a,2)edl;coutgeEle

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!