C++程序分析题
C++程序分析题
1、#include void main( ) { int *a; int *&p=a; int b=10; p=&b; cout<<*a; } 输出为:10 2、#include template Tf(T*a,T*b,int n){ Ts=(T)0; for(int i=0;i return s; } void main() { double c [5]={1.1,2.2,3.3,4.4,5.5},d[5]={10.0,100.0,1000.0}; cout< 输出为:3531 3、#include void main() { for(int i=0;i<4;i++) cout< } 输出为: 0 0*0 0***0 0*****0 4、运行下面的程序,写出当输入25,60时的输出结果。 #include class goods{ private: static int totalWeight; int weight; public: goods(int w) { weigh=w; totalWeight+=w; } goods(goods& gd) { weight=gd.weight; totalWeight+=weight; } ~goods() { totalWeight-=weight; } int getwg() { return weight; } static int getTotal() { return totalWeight; } }; int goods::totalWeight=0; void main() { int w; cout<<″The initial weight of goods:″< goods g1(w); cin>>w; //输入60 goods g2(w); cout<<″The total weight of goods:″< 输出为:The initial weight of goods:0 25 60 (这是输入数据) The total weight of goods:85 5、#include class A{ public: A( ){ } virtual void func( ){cout<<″Destructor A″< }; class B:public A{ public: B( ){ } void func(){cout<<″Destructor B″< }; void main( ) { B b; A&a=b; } 输出为:.Destructor B Destructor A 6、#include class My Class { public: int number; void set(int i); }; int number=3; void MyClass::set (int i) { number=i; } void main() { MyClass my1; int number=10; my1.set(5); cout< cout< cout< 输出为:.5 10 3 7、给出下面程序的输出结果。 #include template class Sample {T n; public: Sample(T i){n=i;} int operator==(Sample &); }; template int Sample {if(n==s.n) return 1; else return 0;} void main( ) {Sample cout<<″s1与s2的数据成员″<<(sl ==s2 ?″相等″:″不相等″)< cout<<″s3与s4的数据成员″<<(s3==s4 ? ″相等″:″不相等″)< S3与S4数据成员相等 8、给出下面程序的输出结果。 #include using namespace std; template T max(T ml,T m2) {return(m1>m2)?ml:m2:} void main( ) { cout< 7 5 y a 9、给出下面程序的输出结果 #include using namespace std; class A { public: int x; A( ) { } A(int a){x=a;} int get(int a){return x+a;} }; void main( ) { A a(8); int(A::*p)(int); p=A::get; cout<<(a.*p)(5)< cout<<(pi- >*P)(7)< 15 10、给出下面程序的输出结果。 include #include using namespace std; class Book { char*title; char*author; int numsold; public: Book( ){ } Book(const char*strl,const char*str2,const int num) { int len=strlen(strl); title=new char[len+1]; strcpy(title,str1); len=strlen(str2); author=new char[len+1]; strcpy(author,str2); numsold=num; } void setbook(const char*str1,const char*str2,const int num) { int len=strlen(str1); title=new char[len+1]; strcpy(title,str1); len=strlen(str2); author=new char[len+1]; strcpy(author,str2); numsold=num; } ~Book( ) { delete title; delete author; } void print(ostream&output) { output<<″书名:″< }; void main( ) { Book obj1(″数据结构″,″严蔚敏″,200),obj2; obj1.print(cout); obj2.setbook(″C++语言程序设计″,″李春葆″,210); obj2.print(cout); } 月销售量:200 书名:c++程序设计 作者:李春葆 月销售量:210 11、# include void SB(char ch) { switch(ch){ case 'A': case 'a': cout <<\"well!\"; break; case 'B': case 'b': cout <<\"good!\"; break; case 'C': case 'c': cout <<\"pass!\"; break; default: cout <<\"nad!\"; break; } } void main() { char a1='b',a2='C',a3='f'; SB(a1);SB(a2);SB(a3);SB('A'); cout < 输出结果: good! pass! bad! well! 12、# include # include void main() { char *a[5]={\"student\ char *p1,*p2; p1=p2=a[0]; for (int i=0; i<5; i++) { if (strcmp(a[i],p1)>0) p1=a[i]; if (strcmp(a[i],p2)<0) p2=a[i]; } cout < 输出结果:worker cadre 13、# include int a=5; void main() { int a=10,b=20; cout <{ int a=0,b=0; for (int i=1; i<6; i++) { a+=i; b+=a; } cout <} cout <} 输出结果:10 20 15 35 5 10 20 14、# include int LB(int *a,int n) { int s=1; for (int i=0; i return s; } void main() { int a[]={1,2,3,4,5,6,7,8}; int b=LB(a,5)+LB(&a[3],3); cout <<\"b=\"<} 输出结果:b=240 15、# include # include struct Worker{ char name[15]; // 姓名 int age; // 年龄 float pay; // 工资 }; void main() { Worker x; char *t=\"liouting\"; int d=38; float f=493; strcpy(x.name,t); x.age=d; x.pay=f; cout < 输出结果:liouting 38 493 16、# include class A { int a; public: A(int aa=0) { a=aa; } ~A() { cout <<\"Destructor A!\"<}; class B:public A { int b; public: B(int aa=0,int bb=0):A(aa) { b=bb; } ~B() { cout <<\"Destructor B!\"<}; void main() { B x(5),y(6,7); // 后定义的变量将先被释放 } 输出结果: Destructor B! 7 Destructor Destructor Destructor A! 6 B! 0 A! 5 因篇幅问题不能全部显示,请点此查看更多更全内容