您好,欢迎访问三七文档
C++程序分析题1、#includeiostream.hvoidmain(){int*a;int*&p=a;intb=10;p=&b;cout*a;}输出为:102、#includeiostream.htemplateclassTTf(T*a,T*b,intn){Ts=(T)0;for(inti=0;in;i++)s+=a[i]*b[i];returns;}voidmain(){doublec[5]={1.1,2.2,3.3,4.4,5.5},d[5]={10.0,100.0,1000.0};coutf(c,d,5)endl;}输出为:35313、#includeiomanip.hvoidmain(){for(inti=0;i4;i++)coutendlsetfill(′′)setw(4-i)′0′setfill(′*′)setw(i+i)(i0?′0′:′′);}输出为:00*00***00*****04、运行下面的程序,写出当输入25,60时的输出结果。#includeiostream.hclassgoods{private:staticinttotalWeight;intweight;public:goods(intw){weigh=w;totalWeight+=w;}goods(goods&gd){weight=gd.weight;totalWeight+=weight;}~goods(){totalWeight-=weight;}intgetwg(){returnweight;}staticintgetTotal(){returntotalWeight;}};intgoods::totalWeight=0;voidmain(){intw;cout″Theinitialweightofgoods:″goods::getTotal()endl;cinw;//输入25goodsg1(w);cinw;//输入60goodsg2(w);cout″Thetotalweightofgoods:″goods::getTotal()endl;}输出为:Theinitialweightofgoods:02560(这是输入数据)Thetotalweightofgoods:855、#includeiostream.hclassA{public:A(){}virtualvoidfunc(){cout″DestructorA″endl;}~A(){func();}};classB:publicA{public:B(){}voidfunc(){cout″DestructorB″endl;}~B(){func();}};voidmain(){Bb;A&a=b;}输出为:.DestructorBDestructorA6、#includeiostream.hclassMyClass{public:intnumber;voidset(inti);};intnumber=3;voidMyClass::set(inti){number=i;}voidmain(){MyClassmy1;intnumber=10;my1.set(5);coutmy1.numberend1;my1.set(number);coutmy1.numberendl;my1.set(::number);coutmy1.number;}输出为:.51037、给出下面程序的输出结果。#includeiostream.htemplateclassTclassSample{Tn;public:Sample(Ti){n=i;}intoperator==(Sample&);};templateclassTintSampleT::operator==(Sample&s){if(n==s.n)return1;elsereturn0;}voidmain(){Sampleintsl(2),s2(3);.cout″s1与s2的数据成员″(sl==s2?″相等″:″不相等″)endl;Sampledoubles3(2.5),s4(2.5);cout″s3与s4的数据成员″(s3==s4?″相等″:″不相等″)endl;}S1与S2的数据成员不相等S3与S4数据成员相等8、给出下面程序的输出结果。#includeiostreamusingnamespacestd;templateclassTTmax(Tml,Tm2){return(m1m2)?ml:m2:}voidmain(){coutmax(1,7)″\t″max(2.0,5.0)endl;coutmax(′y′,′b′″\t″max(″A″,″a″)endl;}75ya9、给出下面程序的输出结果#includeiostreamusingnamespacestd;classA{public:intx;A(){}A(inta){x=a;}intget(inta){returnx+a;}};voidmain(){Aa(8);int(A::*p)(int);p=A::get;cout(a.*p)(5)endl;A*pi=&a;cout(pi-*P)(7)endl;}131510、给出下面程序的输出结果。includeiostream#includestringusingnamespacestd;classBook{char*title;char*author;intnumsold;public:Book(){}Book(constchar*strl,constchar*str2,constintnum){intlen=strlen(strl);title=newchar[len+1];strcpy(title,str1);len=strlen(str2);author=newchar[len+1];strcpy(author,str2);numsold=num;}voidsetbook(constchar*str1,constchar*str2,constintnum){intlen=strlen(str1);title=newchar[len+1];strcpy(title,str1);len=strlen(str2);author=newchar[len+1];strcpy(author,str2);numsold=num;}~Book(){deletetitle;deleteauthor;}voidprint(ostream&output){output″书名:″titleendl;output″作者:″authorendl;output″月销售量:″numsoldendl;}.};voidmain(){Bookobj1(″数据结构″,″严蔚敏″,200),obj2;obj1.print(cout);obj2.setbook(″C++语言程序设计″,″李春葆″,210);obj2.print(cout);}月销售量:200书名:c++程序设计作者:李春葆月销售量:21011、#includeiostream.hvoidSB(charch){switch(ch){case'A':case'a':coutwell!;break;case'B':case'b':coutgood!;break;case'C':case'c':coutpass!;break;default:coutnad!;break;}}voidmain(){chara1='b',a2='C',a3='f';SB(a1);SB(a2);SB(a3);SB('A');coutendl;}输出结果:good!pass!bad!well!12、#includeiostream.h#includestring.hvoidmain(){char*a[5]={student,worker,cadre,soldier,peasant};char*p1,*p2;p1=p2=a[0];for(inti=0;i5;i++){if(strcmp(a[i],p1)0)p1=a[i];if(strcmp(a[i],p2)0)p2=a[i];}coutp1''p2endl;}输出结果:workercadre13、#includeiostream.hinta=5;voidmain(){inta=10,b=20;couta''bendl;{inta=0,b=0;for(inti=1;i6;i++){a+=i;b+=a;}couta''b''::aendl;}couta''bendl;}输出结果:102015355102014、#includeiomanip.hintLB(int*a,intn){ints=1;for(inti=0;in;i++)s*=*a++;returns;}voidmain(){inta[]={1,2,3,4,5,6,7,8};intb=LB(a,5)+LB(&a[3],3);coutb=bendl;}输出结果:b=24015、#includeiostream.h#includestring.hstructWorker{charname[15];//姓名intage;//年龄floatpay;//工资};voidmain(){Workerx;char*t=liouting;intd=38;floatf=493;strcpy(x.name,t);x.age=d;x.pay=f;coutx.name''x.age''x.payendl;}输出结果:liouting3849316、#includeiostream.hclassA{inta;public:A(intaa=0){a=aa;}~A(){coutDestructorA!aendl;}};classB:publicA{intb;public:B(intaa=0,intbb=0):A(aa){b=bb;}~B(){coutDestructorB!bendl;}};voidmain(){Bx(5),y(6,7);//后定义的变量将先被释放}输出结果:DestructorB!7DestructorA!6DestructorB!0DestructorA!5
本文标题:C++程序分析题
链接地址:https://www.777doc.com/doc-4613730 .html