西电软院《面向对象程序设计》课程复习(1)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

《C++面向对象程序设计》课程复习西安电子科技大学软件工程研究所李雁妮Email:yannili@mail.xidian.edu.cn1函数的基本问题1.对下面的函数findChar,改正你所发现的错误,并写出另一个函数find2Char,使之改用指针方式来访问cpString的元素,且功能与改正后的函数findChar等价。voidfindChar(constcharcpString[],constcharcChar){for(inti=0;cpString[i]!=‘\0‘;i++)if(cpString[i]=cChar){printf(Theindexis%d.\n,i);return;}printf(Inthestring\%s\having%dchar‘s,no‘%c‘hasbeenfound.\n,cpString,i,cChar);}21函数的基本问题voidfindChar(constcharcpString[],constcharcChar){inti;//作用域概念for(i=0;cpString[i]!=‘\0‘;i++)if(cpString[i]==cChar){//常见的操作符使用熟练程度//…}voidfind2Char(constcharcpString[],constcharcChar){inti=0;for(constchar*p=cpString;*p!=‘\0‘;p++,i++)//类型匹配概念;dereferencing;指针操作if(*p==cChar){//dereferencingprintf(Theindexis%d.\n,i);return;}printf(Inthestring\%s\having%dchar‘s,no‘%c‘hasbeenfound.\n,cpString,i,cChar);}31函数的基本问题2.阅读下面的程序,写出main的输出。inth(intiCount,constint*ipIndex,int&irObj){iCount%=2;if(*ipIndex=5)irObj+=iCount;return*ipIndex+irObj;}main(){inti,j=1,k=2,m;for(i=0;i10;i++,j++){m=h(i,&j,k);printf(i=%2d,j=%2d,k=%2d,m=%2d.\n,i,j,k,m);}}41函数的基本问题3.(1)Implementafunction:intatoi(constchar*str);whichconvertsastringstr(indecimalformat)toitscorrespondingintegerandreturntheinteger.(2)Writeatestprogramwhichtakeanintegralstringfromcommand-lineargument,andprintthecallingresultofatoi().51函数的基本问题#includecstdio#includeiostreamusingnamespacestd;intatoi(constchar*str){intresult=0,len=strlen(str);intsign=1;if(str[0]=='-'){str++;sign=-1;len--;}elseif(str[0]==‘+’){str++;sign=1;len--;}for(inti=0;ilen;i++){result=result*10+str[i]-'0';}returnsign*result;}61函数的基本问题intmain(intargc,char*argv[]){if(argc=1){coutMusthas1arguments!endl;return1;}printf(atoi(\%s\)=%d\n,argv[1],atoi(argv[1]));return0;}72Namespace基本问题namespacea{voidf();intg(doublex);}namespaceb{intf();doubley;}usingnamespacea;usinga::f();83类的基本问题1.ImplementaclassStack,whereastackisaLastIn,FirstOutlistofzeroormoreelements.TheStackclassshouldprovidefollowingmethods:(1)voidpush(constint&t);//insertanelementtoastack(2)intpop();//deleteanelementfromastackandreturnit(3)boolempty();//checkastackisemptyornot(4)boolfull();//checkastackisfullornot(5)intgetNumbers();//returnthecurrentnumberofelementsinastack93类的基本问题classStack{public:enum{MaxStack=5};voidStack(){top=-1;}voidpush(constint&t){if(full()){return;}arr[++top]=t;}intpop(){if(empty()){returndummy_val;}returnarr[top--];}boolempty(){returntop0;}boolfull(){returntop=MaxStack-1;}intgetNumbers(){returntop+1;}private:inttop,intarr[MaxStack];intdummy_val;};103类的基本问题2.AccordingtotheinterfaceofaDateclass,implementtheDateclass.classDate{//…public:Date(inty=1970,intm=1,intd=1);//构造器booleanisLeapYear(constDate&d);//判断对象d的年属性是否是闰年voidprint();//打印日期对象的数据成员值};113类的基本问题classDate{intyear,month,day;public:Date(inty=1970,intm=1,intd=1){year=y,month=m,day=d;}boolisLeapYear(constDate&d){if(d.year%400==0)returntrue;if(d.year%4==0&&d.year%100!=0)returntrue;returnfalse;}voidprint(){coutyear'/'month'/'dayendl;}};123类的基本问题已知类的定义如下:classBase{protected:intiBody;public:virtualvoidprintOn()=0;Base(inti=0):iBody(i){}};classSub1:publicBase{//…public:voidprintOn();Sub1(inti,char*s);};classSub2:publicBase{//…public:voidprintOn();Sub2(inti,shorts);};133类的基本问题试完成类Sub1和Sub2的定义和操作的实现代码,使之能符合下面程序及在注释中描述的运行结果的要求:main(){Sub1s1(1000,ThisisanobjectofSub1);Sub2s2(2000,10);s1.printOn();//此时显示出:1000:ThisisanobjectofSub1s2.printOn();//此时显示出:10and2000}143类的基本问题classSub1:publicBase{char*cpString;public:voidprintOn(){printf(%d:%s\n,iBody,cpString);}Sub1(inti,char*s):Base(i){cpString=newchar[strlen(s)+1];strcpy(cpString,s);}};classSub2:publicBase{shortsShort;public:voidprintOn(){printf(%dand%d\n,sShort,iBody);}Sub2(inti,shorts):Base(i),sShort(s){}};153类的基本问题Inacompany,therearetwotypesofpeople:employeeandmanager.Noticethatamanagerisalsoanemployee.Everyemployeehasfollowingbasicinformation:name,age,workYear(工作年限)anddepNo(部门号).Moreover,amanagerhasothersomeattributes:level(级别)andaarrayofemployeesmanaged.AobjectofclassEmployeeorManagerhasfollowingbehaviors://….PleasebaseontheaboveobjectsbehaviorstodefineandimplementclassEmployeeandManagerrespectively.163类的基本问题voidmain(){Employeee(“Jack”,25,3,“Development”);Managerm(“Tom”,30,5,“Development”,2);m.addMember(&m).addMember(&e);e.printOn();//displayalldatamemberinformationofJackm.printOn();//displayalldatamemberinformationofTomEmployee*p=&e;boolb=p-retire();//iftheobjecte’sageis55years,bistruep=&m;b=p-retire();//iftheobjectm’sageis60years,bistruecout“Total”Manager::count()“employee’sobjectcreated.”endl;//display:Total2employee’sobjectcreated.}173类的基本问题classEmployee{protected:charname[41];intage;intworkYear;chardepNo[41];staticintcnt;public:Employee(char*n,inta,inty,char*d):age(a),workYear(y){strncpy(name,n,40);strncpy(depNo,d,40);cnt++;}183类的基本问题~Employee(){cnt--;}voidprintOn(){coutName:nameendlage:ageendlYear:workYearendlDept:depNoendl;}virtualboolretire(){if(age=55

1 / 20
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功