《c++试题库编程题.docx》由会员分享,可在线阅读,更多相关《c++试题库编程题.docx(22页珍藏版)》请在优知文库上搜索。
1、/2-5#include#include#includeusingnamespacestd;classTriangleinta,b,c;public:doubleArea()(doubles=(a+b+c)2;returnsqrt(s*(s-a)*(s-b)*(s-c););intPerimeterOreturna+b+c;)voidInput()cinabc;);voidmain()Triangletl;tl.Input();couttl.Area(),tl.Perimeter();)/2-3#includeinlineintmax(intajntb)(if(ab)returna;elser
2、eturnb;Jinlineintmax(inta,intb,intc)(returnmax(a,max(b,c);)voidmain()(intx=2l,y=15,z=22,d;d=max(x,max(y,z);coutd;)/2-4#includeintoverload(intn);charoverload(charn);voidmain()(intn;chars;CoUtn;coutn=,overload(n)endl;COUt”输入a,s;couta=;coutoverload(s)endl;)intoverload(intn)(returnn;)charoverload(charn)
3、(coutbendl;returnO;)/2-2#includeiostreamusingnamespacestd;inlinechartrans(charch);intmain()(charch;while(ch=getchar()!=n,)couttrans(ch);cout=aAfcch=,z,)returnch-32;elsereturnch+32;)/3-l#inc1udeusingstd:cout;usingstd:endl;intMin(inta,intb)(return(ab?a:b);)intMin(inta,intb,intc)return(Min(a,b)c?Min(a,
4、b):c);intMin(inta,intb,intc,intd)(return(Min(a,b,c)d?Min(a,b,c):d);)voidmain(void)(coutMIN(l,2)=,Min(l,2)endl;coutMIN(3,4,5)=,Min(3A5)endl;coutMIN(4,5,6,7)=,Min(4,5,6,7)endl;)/3-2#includeusingnamespacestd;voidArea(floatr)(coutthecirclesareais:3.14*r*rendl;)voidArea(floata,floatb)(couttherectangles(s
5、quare,s)areais:,a*bendl;)voidArea(floata,floatb,floath)(coutthetrapzoid,sareais:,(a+b)*h2endl;)voidmain()(Area(5);Area(3,3);Area(3,5,4);)/3-3#includeusingnamespacestd;voidSort(inta,intn)(boolflag;fbr(inti=0jn-l;i+)(flag=false;fbr(intj=l;jaj)inttemp=aj-l;aj-l=aj;ajj=temp;flag=true;)if(!flag)break;1fo
6、r(intm=O;mn;m+)coutam;coutendl;)voidSort(floatblJ,intn)(boolflag;fr(inti=0;in-l;i+)(flag=false;fbr(intj=l;jbUD(floattemp=bj-l;bj-l=bj;bj=temp;flag=true;)if(!flag)break;1for(intm=0;mn;m+)coutbm,;coutendl;)voidmain()(intal0=3,5,2,9,0,8,6,4,7;floatbl10=0.1,0.2,03A4,0.5,0.6,0.7,0.8,0.9,1.0);Sort(a,10);S
7、ort(b,10);)/3-4#includeusingnamespacestd;classRectanglefloata,b;public:RectangleO(coutinputthetwosideoftherectangle:ab;1Rectangle(floatx,floaty):a(x),b(y)floatCir()(return(2*(a+b);1floatArea()(return(a*b);11;voidmain()(RectangleRe;couttherectanglescircumferenceis:Re.Cir()endltherectanglesareais:Re.A
8、rea()endl;)/3-5#includeusingnamespacestd;classCircle(floatr;public:Circle()(coutinputtheradiusofthecircle:,r;1Circle(floatx):r(x)floatCir()(return2*3.14*r;)floatArea()(return3.l4*r*r;)1;voidmain()CircleC(5);couttheCirclescircumferenceisfC.Cir()endltheCirclesAreais:C.Area()endl;)/4-lclassstudent(intn
9、o;charname20;public:student();intgetno()const;char*getname();1;#includestudent:student()(no=l;strcpy(name,wang);)intStudentzrgetno()const(returnno;)char*student:getname()(returnname;)intmain()(studentsi;sl.getno();sl.getname();conststudents2;s2.getno();returnO;)/4-3#includeclassobject(floata;floatwe
10、ight;public:object(floatw);object()coutdestructweightendl;);classbox:publicobject(floatb;floatheight,widt;public:box(floatw,floath,floatwi):object(w),height(h),widt(wi)coutnconstructheightHendl;coutconstructwidtendl;-box()coutdestructheightendl;coutndestructwidtendl;1;object:object(floatw)(weight=w;
11、coutconstructweightendl;)intmain()(boxb(1.1,2.2,3.3);return0;)/4-4#includeiostreamusingnamespacestd;classbaseclass(public:baseclass()cout,a.cendl;Jvirtual-baseclass()coutadendl;);classderivedclass:publicbaseclass(public:derivedclass()coutb.cendl;)derivedclass()cout,b.dendl;);intmain()(derivedclassob
12、j;baseclass*p;P=&obj;return0;/4-5#includeiostream#includeusingnamespacestd;classDoument(char*name;public:Doument(char*na)(name=newcharstrlen(na)+l;strcpy(name,na);1virtualvoidprint()COnStcout姓名:,nameendl;);classBookipublicDoument(intpagecount;char*name;public:Book(intp,char*na):Doument(na)(p=pagecount;name=newcharstrlen(na)+l;strcpy(name,na);)voidprint()ConStcout页码:pagecount姓名:nameendl;1;voiddisp(Doument&i)(i.print();)intmain()(Bookb(23,“小明”);disp(b);returnO;)/5