- 69
- 0
- 约1.95万字
- 约 36页
- 2020-04-19 发布于浙江
- 举报
C++程序设计基础课后答案--第五章
5.1 阅读下列程序,写出执行结果
1.#include iostream.h
???struct data
?? { int n ;
???? float score ;
??? } ;
????void main()
?? { data a[3] = { 1001,87,1002,72,1003,90 } , *p = a ;
?? ??cout (p++)-n endl ;
?? ??cout (p++)-n endl ;
?? ??cout p-n++ endl ;
?? ??cout (*p).n++ endl ;
?? }
2.#include iostream.h
?? struct employee
?? { char name[ 20 ] ;
???? char sex ;
?? ?} ;
? void fun( employee *p )
??{ if( (*p).sex == m )
????cout (*p).name endl ;
??}
? void main()
? { employee emp[5] = { Liming, m, Wangxiaoping, f, Luwei, m } ;
????int i ;
???? for( i=0; i3; i++ )
???? ? fun( emp+i ) ;
? }
3. #include iostream.h
??? struct node
??? { char * s ;
??????node * q ;
?????} ;
??? void main()
???? { node a[ ] = { { Mary, a+1 }, { Jack, a+2 }, { Jim, a } } ;
???????node *p = a ;
???????cout p-s endl ;
???????cout p-q-s endl ;
???????cout p-q-q-s endl ;
???????cout p-q-q-q-s endl ;
???? }
4.#include iostream.h
??? class A
?? { public :
???? ??int f1();
???? ??int f2();
???? ??void setx( int m ) { x = m ; cout x endl; }
???? ??void sety( int n ) { y = n ; cout y endl; }
???? ??int getx() { return x ; }
???? ??int gety() { return y ; }
???? private :
???? ??int x, y ;
?? };
?? int A::f1() { return x + y ; }
?? int A::f2() { return x - y ; }
?? void main()
???{ A a ;
???? a.setx( 10 ) ; a.sety( 5 ) ;
???? cout a.getx() \t a.gety() endl ;
???? cout a.f1() \t a.f2() endl ;
?? }
5. #include iostream.h
??? class T
?? { public :
???????T( int x, int y )
????? ? { a = x ; b = y ;
???????? ?cout 调用构造函数1. endl ;
???????? ?cout a \t b endl ;
????? ? }
???????T( T d )
????? ? { cout 调用构造函数2. endl ;
????? ? ??cout d.a \t d.b endl ;
????? ? }
??????~T() { cout 调用析构函数.endl; }
?????? int add( int x, int y = 10 ) { return x + y ; }
??? private :
??? ??int a, b ;
??? };
?? void main()
???{ T d1( 4, 8 ) ;
?????T d2( d1 ) ;
???? cout d2.add( 10 ) endl ;
??? }
6. #include iostream.h
??? class T
??? { public:
????? ??T(int x){ a=x; b+
原创力文档

文档评论(0)